[issue43463] typing.get_type_hints with TYPE_CHECKING imports / getting hints for single argument
Florian Bruhin
report at bugs.python.org
Mon Mar 15 14:29:53 EDT 2021
Florian Bruhin <python.org at the-compiler.org> added the comment:
Ah, I wasn't aware of that, thanks for the pointer! So what inspect does internally is:
def _get_type_hints(func, **kwargs):
try:
return typing.get_type_hints(func, **kwargs)
except Exception:
# First, try to use the get_type_hints to resolve
# annotations. But for keeping the behavior intact
# if there was a problem with that (like the namespace
# can't resolve some annotation) continue to use
# string annotations
return func.__annotations__
Which means there's even some "prior art" there already falling back to a string when the annotation couldn't be resolved. Doing so in typing.get_type_hints on a per-argument basis would thus also make inspect more consistent:
Right now,
print(repr(inspect.signature(fun).parameters['b'].annotation))
in my example returns a string, but when changing the annotation for `a`, the returned annotation for `b` is now magically a `typing.Union` object.
(I personally would indeed expect inspect to resolve those annotations, but yeah, let's keep that in issue43355.)
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43463>
_______________________________________
More information about the Python-bugs-list
mailing list