[issue43817] Add inspect.get_annotations()

Inada Naoki report at bugs.python.org
Sat Apr 24 03:56:55 EDT 2021


Inada Naoki <songofacandy at gmail.com> added the comment:

>
> The difference between eval_str=True and eval_str=ONLY_IF_STRINGIZED:
>
> def foo(a:int, b:"howdy howdy"): ...
>
> inspect.get_annotations(foo, eval_str=True) throws an exception.
> inspect.get_annotations(foo, eval_str=ONLY_IF_STRINGIZED) returns {'a': int, b: 'howdy howdy'}
>
> Type hints have a convention that string annotations are a "forward declaration" and should be eval()uated.  Annotations don't have such a convention--a string is a legal annotation, and is not required to be valid Python.
>

For such use case, ONLY_IF_STRINGIZED thorows an exception for `def
foo(a: "howdy howdy")` anyway.
In such cases, they should use `eval_str=False`, or `eval_str=True`
*and* `return_str_when_eval_failed=True` option.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43817>
_______________________________________


More information about the Python-bugs-list mailing list