bpo-38605: Make postponed evaluation of annotations default by isidentical · Pull Request #20434 · python/cpython

@isidentical

isidentical Oct 4, 2020

Author Member

Actually, there is not much we can do for the exceptions since there is a code path that is going to eval.

(.venv) (Python 3.10.0a0) [ 12:31ÖÖ ]  [ isidentical@desktop:~/cpython/cpython(bpo-38605✗) ]
 $ python t.py  
(a: 'b()')
(.venv) (Python 3.10.0a0) [ 12:31ÖÖ ]  [ isidentical@desktop:~/cpython/cpython(bpo-38605✗) ]
 $ ./python t.py
Traceback (most recent call last):
  File "/home/isidentical/cpython/cpython/t.py", line 4, in <module>
    print(inspect.signature(x))
  File "/home/isidentical/cpython/cpython/Lib/inspect.py", line 3133, in signature
    return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
  File "/home/isidentical/cpython/cpython/Lib/inspect.py", line 2882, in from_callable
    return _signature_from_callable(obj, sigcls=cls,
  File "/home/isidentical/cpython/cpython/Lib/inspect.py", line 2333, in _signature_from_callable
    return _signature_from_function(sigcls, obj,
  File "/home/isidentical/cpython/cpython/Lib/inspect.py", line 2178, in _signature_from_function
    annotations = _get_type_hints(func)
  File "/home/isidentical/cpython/cpython/Lib/inspect.py", line 2127, in _get_type_hints
    return typing.get_type_hints(func)
  File "/home/isidentical/cpython/cpython/Lib/typing.py", line 1420, in get_type_hints
    value = _eval_type(value, globalns, localns)
  File "/home/isidentical/cpython/cpython/Lib/typing.py", line 255, in _eval_type
    return t._evaluate(globalns, localns, recursive_guard)
  File "/home/isidentical/cpython/cpython/Lib/typing.py", line 501, in _evaluate
    eval(self.__forward_code__, globalns, localns),
  File "<string>", line 1, in <module>
  File "/home/isidentical/cpython/cpython/t.py", line 1, in b
    def b(): 1/0 
ZeroDivisionError: division by zero
(.venv) (Python 3.10.0a0) [ 12:31ÖÖ ]  [ isidentical@desktop:~/cpython/cpython(bpo-38605✗) ]
 $ cat t.py
def b(): 1/0 
def x(a: 'b()'): pass
import inspect
print(inspect.signature(x))