|
45 | 45 | import tokenize |
46 | 46 | import token |
47 | 47 | import types |
| 48 | +import typing |
48 | 49 | import warnings |
49 | 50 | import functools |
50 | 51 | import builtins |
@@ -1877,7 +1878,10 @@ def _signature_is_functionlike(obj): |
1877 | 1878 | code = getattr(obj, '__code__', None) |
1878 | 1879 | defaults = getattr(obj, '__defaults__', _void) # Important to use _void ... |
1879 | 1880 | kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here |
1880 | | - annotations = getattr(obj, '__annotations__', None) |
| 1881 | + try: |
| 1882 | + annotations = _get_type_hints(obj) |
| 1883 | + except AttributeError: |
| 1884 | + annotations = None |
1881 | 1885 |
|
1882 | 1886 | return (isinstance(code, types.CodeType) and |
1883 | 1887 | isinstance(name, str) and |
@@ -2118,6 +2122,16 @@ def p(name_node, default_node, default=empty): |
2118 | 2122 |
|
2119 | 2123 | return cls(parameters, return_annotation=cls.empty) |
2120 | 2124 |
|
| 2125 | +def _get_type_hints(func): |
| 2126 | + try: |
| 2127 | + return typing.get_type_hints(func) |
| 2128 | + except Exception: |
| 2129 | + # First, try to use the get_type_hints to resolve |
| 2130 | + # annotations. But for keeping the behavior intact |
| 2131 | + # if there was a problem with that (like the namespace |
| 2132 | + # can't resolve some annotation) continue to use |
| 2133 | + # string annotations |
| 2134 | + return func.__annotations__ |
2121 | 2135 |
|
2122 | 2136 | def _signature_from_builtin(cls, func, skip_bound_arg=True): |
2123 | 2137 | """Private helper function to get signature for |
@@ -2161,7 +2175,8 @@ def _signature_from_function(cls, func, skip_bound_arg=True): |
2161 | 2175 | positional = arg_names[:pos_count] |
2162 | 2176 | keyword_only_count = func_code.co_kwonlyargcount |
2163 | 2177 | keyword_only = arg_names[pos_count:pos_count + keyword_only_count] |
2164 | | - annotations = func.__annotations__ |
| 2178 | + annotations = _get_type_hints(func) |
| 2179 | + |
2165 | 2180 | defaults = func.__defaults__ |
2166 | 2181 | kwdefaults = func.__kwdefaults__ |
2167 | 2182 |
|
|
0 commit comments