|
25 | 25 | import pydantic
|
26 | 26 |
|
27 | 27 | from ._types import NoneType
|
28 |
| -from ._utils import is_given, extract_type_arg, is_annotated_type, extract_type_var_from_base |
| 28 | +from ._utils import is_given, extract_type_arg, is_annotated_type, is_type_alias_type, extract_type_var_from_base |
29 | 29 | from ._models import BaseModel, is_basemodel
|
30 | 30 | from ._constants import RAW_RESPONSE_HEADER, OVERRIDE_CAST_TO_HEADER
|
31 | 31 | from ._streaming import Stream, AsyncStream, is_stream_class_type, extract_stream_chunk_type
|
@@ -126,9 +126,15 @@ def __repr__(self) -> str:
|
126 | 126 | )
|
127 | 127 |
|
128 | 128 | def _parse(self, *, to: type[_T] | None = None) -> R | _T:
|
| 129 | + cast_to = to if to is not None else self._cast_to |
| 130 | + |
| 131 | + # unwrap `TypeAlias('Name', T)` -> `T` |
| 132 | + if is_type_alias_type(cast_to): |
| 133 | + cast_to = cast_to.__value__ # type: ignore[unreachable] |
| 134 | + |
129 | 135 | # unwrap `Annotated[T, ...]` -> `T`
|
130 |
| - if to and is_annotated_type(to): |
131 |
| - to = extract_type_arg(to, 0) |
| 136 | + if cast_to and is_annotated_type(cast_to): |
| 137 | + cast_to = extract_type_arg(cast_to, 0) |
132 | 138 |
|
133 | 139 | if self._is_sse_stream:
|
134 | 140 | if to:
|
@@ -164,18 +170,12 @@ def _parse(self, *, to: type[_T] | None = None) -> R | _T:
|
164 | 170 | return cast(
|
165 | 171 | R,
|
166 | 172 | stream_cls(
|
167 |
| - cast_to=self._cast_to, |
| 173 | + cast_to=cast_to, |
168 | 174 | response=self.http_response,
|
169 | 175 | client=cast(Any, self._client),
|
170 | 176 | ),
|
171 | 177 | )
|
172 | 178 |
|
173 |
| - cast_to = to if to is not None else self._cast_to |
174 |
| - |
175 |
| - # unwrap `Annotated[T, ...]` -> `T` |
176 |
| - if is_annotated_type(cast_to): |
177 |
| - cast_to = extract_type_arg(cast_to, 0) |
178 |
| - |
179 | 179 | if cast_to is NoneType:
|
180 | 180 | return cast(R, None)
|
181 | 181 |
|
|
0 commit comments