[issue44926] typing.get_type_hints() raises for type aliases with forward references

Maximilian Hils report at bugs.python.org
Mon Aug 16 14:06:27 EDT 2021


Maximilian Hils <python-bugs at maximilianhils.com> added the comment:

> For your specific use case (where the user is using Python 3.6), you could pass in globalns and localns to get_type_hints as a temporary workaround. Off the top of my head:
get_type_hints(func2, globalns=foo.__dict__) might work. Would that work for your library?

I guess the hard part is knowing that the type annotation comes from `foo`. In the example here we can of course hardcode it, but that doesn't work in the general case or for pdoc, the documentation generator I'm working on (https://pdoc.dev). I have experimented quite a bit with walking the AST to figure out where type aliases are imported from to then re-executing ForwardRefs with that globalns. Long story short, trying to reverse-engineer __forward_module__ quickly becomes a tangled hot mess where you need to adjust for import aliases, reimports, and so on.

----------

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


More information about the Python-bugs-list mailing list