Ivan Levkivskyi <[email protected]> added the comment:
> I'm not sure what can be done with this. The problem is that the decorator
> doesn't know what's in the caller's namespace. The type being added is
> "typing.Any". If the caller doesn't import typing, then get_type_hints will
> fail (as demonstrated here).
IIUC the main problem is that get_type_hints() fails even if typing is
imported. I would expect this to work (just repeating the original example in a
more compact form):
import dataclasses
import typing
A = dataclasses.make_dataclass('A', ['a_var'])
typing.get_type_hints(A) # This currently crashes
Interestingly, if I use a very similar call that it works:
>>> typing.get_type_hints(A, globalns=globals())
{'a_var': typing.Any}
So the core of the issue is that the globals are identified incorrectly, and
indeed if I look at the generated class it looks wrong:
>>> A.__module__
'types' # Should be '__main__'
I think we should fix the ``__module__`` attribute of the dynamically generated
dataclasses (for example the way it is done for named tuples).
Btw, https://github.com/python/cpython/pull/14166 may potentially fix the
``__module__`` attribute here too.
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue37948>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com