[issue43463] typing.get_type_hints with TYPE_CHECKING imports / getting hints for single argument

Larry Hastings report at bugs.python.org
Fri Apr 16 12:20:23 EDT 2021


Larry Hastings <larry at hastings.org> added the comment:

> Hey Larry, it would seem that PEP 649 as currently specified would make it impossible to access annotations via the inspect module in cases where x.__annotations__ raises (because one of the annotations references an undefined variable).

That's true.  If PEP 649 is accepted, inspect.signature() might want to catch NameError when examining __annotations__ on the object.  Though I'm not sure what it should do when the exception is raised.


> I really think that we need *some* way of accessing partial annotations. Even just leaving the failing key out of __annotations__ (but keeping other keys if their annotation works) would be better than failing to return an __annotations__ dict at all.

Unfortunately I don't agree--"errors should never pass silently."  Silently omitting the failed annotation seems like it would be a bad experience.  What if the value you needed from the annotation was the one that was omitted?  Now you have a mystery obscuring your existing problem.

There is a PR against PEP 649 specifically to suppress NameErrors:

    https://github.com/larryhastings/co_annotations/pull/3

I haven't merged the PR as I don't agree with it.

----------

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


More information about the Python-bugs-list mailing list