|
1 | 1 | import inspect
|
2 |
| -from collections.abc import Awaitable, Callable, Iterable, Mapping, MutableMapping, Sequence |
| 2 | +from collections.abc import Awaitable, Callable, Iterable, Mapping, MutableMapping |
3 | 3 |
|
4 | 4 | import httpx
|
5 | 5 | import pydantic.fields
|
@@ -164,23 +164,21 @@ def process_response_envelope(typ: type[ResponseEnvelope]) -> Iterable[ResponseH
|
164 | 164 |
|
165 | 165 |
|
166 | 166 | def process_response_field(name: str, field: pydantic.fields.FieldInfo) -> ResponseHandler:
|
167 |
| - field_handlers: Sequence[ResponseHandler] = [ |
168 |
| - anno |
169 |
| - for anno in field.metadata |
170 |
| - if isinstance(anno, ResponseHandler) or (inspect.isclass(anno) and issubclass(anno, ResponseHandler)) |
171 |
| - ] |
172 |
| - if not len(field_handlers) == 1: |
| 167 | + field_handlers = [] |
| 168 | + for anno in field.metadata: |
| 169 | + if inspect.isclass(anno) and issubclass(anno, ResponseHandler): |
| 170 | + field_handlers.append(anno()) |
| 171 | + elif isinstance(anno, ResponseHandler): |
| 172 | + field_handlers.append(anno) |
| 173 | + if len(field_handlers) != 1: |
173 | 174 | raise TypeError(f'{name}: Expected exactly one ResponseHandler annotation')
|
174 | 175 | handler = field_handlers[0]
|
175 |
| - if inspect.isclass(handler) and issubclass(handler, ResponseHandler): |
176 |
| - handler_inst = typing.cast(type[ResponseHandler], handler)() |
177 |
| - else: |
178 |
| - handler_inst = handler |
179 |
| - if isinstance(handler_inst, NameTypeAwareAnnotation): |
| 176 | + |
| 177 | + if isinstance(handler, NameTypeAwareAnnotation): |
180 | 178 | field_type = field.annotation
|
181 | 179 | assert field_type
|
182 |
| - handler_inst.supply_formal(name, field_type) |
183 |
| - return handler_inst |
| 180 | + handler.supply_formal(name, field_type) |
| 181 | + return handler |
184 | 182 |
|
185 | 183 |
|
186 | 184 | def find_type(
|
|
0 commit comments