|
3 | 3 | from __future__ import absolute_import, division, print_function |
4 | 4 |
|
5 | 5 | import copy |
6 | | -import inspect |
7 | 6 | import linecache |
8 | 7 | import sys |
9 | 8 | import warnings |
|
18 | 17 | PY2, |
19 | 18 | PY310, |
20 | 19 | PYPY, |
| 20 | + _AnnotationExtractor, |
21 | 21 | isclass, |
22 | 22 | iteritems, |
23 | 23 | metadata_proxy, |
@@ -2501,21 +2501,11 @@ def fmt_setter_with_converter( |
2501 | 2501 | if a.init is True: |
2502 | 2502 | if a.type is not None and a.converter is None: |
2503 | 2503 | annotations[arg_name] = a.type |
2504 | | - elif a.converter is not None and not PY2: |
| 2504 | + elif a.converter is not None: |
2505 | 2505 | # Try to get the type from the converter. |
2506 | | - sig = None |
2507 | | - try: |
2508 | | - sig = inspect.signature(a.converter) |
2509 | | - except (ValueError, TypeError): # inspect failed |
2510 | | - pass |
2511 | | - if sig: |
2512 | | - sig_params = list(sig.parameters.values()) |
2513 | | - if ( |
2514 | | - sig_params |
2515 | | - and sig_params[0].annotation |
2516 | | - is not inspect.Parameter.empty |
2517 | | - ): |
2518 | | - annotations[arg_name] = sig_params[0].annotation |
| 2506 | + t = _AnnotationExtractor(a.converter).get_first_param_type() |
| 2507 | + if t: |
| 2508 | + annotations[arg_name] = t |
2519 | 2509 |
|
2520 | 2510 | if attrs_to_validate: # we can skip this if there are no validators. |
2521 | 2511 | names_for_globals["_config"] = _config |
@@ -3135,36 +3125,20 @@ def pipe_converter(val): |
3135 | 3125 |
|
3136 | 3126 | return val |
3137 | 3127 |
|
3138 | | - if not PY2: |
3139 | | - if not converters: |
| 3128 | + if not converters: |
| 3129 | + if not PY2: |
3140 | 3130 | # If the converter list is empty, pipe_converter is the identity. |
3141 | 3131 | A = typing.TypeVar("A") |
3142 | 3132 | pipe_converter.__annotations__ = {"val": A, "return": A} |
3143 | | - else: |
3144 | | - # Get parameter type. |
3145 | | - sig = None |
3146 | | - try: |
3147 | | - sig = inspect.signature(converters[0]) |
3148 | | - except (ValueError, TypeError): # inspect failed |
3149 | | - pass |
3150 | | - if sig: |
3151 | | - params = list(sig.parameters.values()) |
3152 | | - if ( |
3153 | | - params |
3154 | | - and params[0].annotation is not inspect.Parameter.empty |
3155 | | - ): |
3156 | | - pipe_converter.__annotations__["val"] = params[ |
3157 | | - 0 |
3158 | | - ].annotation |
3159 | | - # Get return type. |
3160 | | - sig = None |
3161 | | - try: |
3162 | | - sig = inspect.signature(converters[-1]) |
3163 | | - except (ValueError, TypeError): # inspect failed |
3164 | | - pass |
3165 | | - if sig and sig.return_annotation is not inspect.Signature().empty: |
3166 | | - pipe_converter.__annotations__[ |
3167 | | - "return" |
3168 | | - ] = sig.return_annotation |
| 3133 | + else: |
| 3134 | + # Get parameter type from first converter. |
| 3135 | + t = _AnnotationExtractor(converters[0]).get_first_param_type() |
| 3136 | + if t: |
| 3137 | + pipe_converter.__annotations__["val"] = t |
| 3138 | + |
| 3139 | + # Get return type from last converter. |
| 3140 | + rt = _AnnotationExtractor(converters[-1]).get_return_type() |
| 3141 | + if rt: |
| 3142 | + pipe_converter.__annotations__["return"] = rt |
3169 | 3143 |
|
3170 | 3144 | return pipe_converter |
0 commit comments