This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author levkivskyi
Recipients arne, eric.smith, levkivskyi
Date 2019-11-17.20:28:54
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1574022535.51.0.849914227032.issue37948@roundup.psfhosted.org>
In-reply-to
Content
> 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.
History
Date User Action Args
2019-11-17 20:28:55levkivskyisetrecipients: + levkivskyi, eric.smith, arne
2019-11-17 20:28:55levkivskyisetmessageid: <1574022535.51.0.849914227032.issue37948@roundup.psfhosted.org>
2019-11-17 20:28:55levkivskyilinkissue37948 messages
2019-11-17 20:28:55levkivskyicreate