Issue 37948: get_type_hints fails if there are un-annotated fields in a dataclass (original) (raw)
When declaring a dataclass with make_dataclass, it is valid to omit type information for fields. annotations understands it and just adds typing.Any, but typing.get_type_hints fails with a cryptic error message:
import dataclasses import typing A = dataclasses.make_dataclass('A', ['a_var']) A.annotations {'a_var': 'typing.Any'} typing.get_type_hints(A) Traceback (most recent call last): File "", line 1, in File "/user/venvs/python_3.7/lib/python3.7/typing.py", line 973, in get_type_hints value = _eval_type(value, base_globals, localns) File "/user/venvs/python_3.7/lib/python3.7/typing.py", line 260, in _eval_type return t._evaluate(globalns, localns) File "/user/venvs/python_3.7/lib/python3.7/typing.py", line 464, in _evaluate eval(self.forward_code, globalns, localns), File "", line 1, in NameError: name 'typing' is not defined
Adding typing.Any explicitly is an obvious workaround:
B = dataclasses.make_dataclass('B', [('a_var', typing.Any)]) typing.get_type_hints(B) {'a_var': typing.Any}
There is already a bug filed regarding datalcasses and get_type_hints which might be related: https://bugs.python.org/issue34776
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).
The only thing I can think of is using a type that's in builtins. "object" springs to mine, but of course that's semantically incorrect.
Or, maybe I could use "dataclasses.sys.modules['typing'].Any". I don't currently import sys (I don't think), but this should be a cheap import. Then if typing.get_type_hints() is called, we know typing will have already been importing.
But what if "dataclasses" isn't in the caller's namespace? I guess if I could find some way to navigate to sys.modules from builtins, that would largely work, absent playing games with builtins.
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.