-
-
Notifications
You must be signed in to change notification settings - Fork 31.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
get_type_hints fails if there are un-annotated fields in a dataclass #82129
Comments
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 "<input>", line 1, in <module>
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 "<string>", line 1, in <module>
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 |
It looks like #9518 will fix also this one. |
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. |
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 Btw, #14166 may potentially fix the |
PR 14166 does not fix this issue. |
Reproduced on 3.11.07a |
I ran into this when trying to make a quick dataclass to demo import dataclasses
import cattrs
A = dataclasses.make_dataclass('A', ['a_var'])
a = A("a_value")
cattrs.unstructure(a)
Initially I thought this was a cattrs bug, but on looking at the trace it uses In 3.13.0b4, 3.12, 3.11 (and earlier) this still reproduces with: import dataclasses
from typing import get_type_hints
A = dataclasses.make_dataclass('A', ['a_var'])
print(get_type_hints(A)) However, in 3.13.0b4, and 3.12 (but not 3.11 or earlier) if you plainly import dataclasses
import typing
A = dataclasses.make_dataclass('A', ['a_var'])
print(typing.get_type_hints(A)) I say "works" because it's evaluating the name 'typing' from the script file that defines the dataclass. Unlikely someone would do this, but I'd still consider this incorrect behaviour. import dataclasses
from typing import get_type_hints
class typing:
Any = int
A = dataclasses.make_dataclass('A', ['a_var'])
print(get_type_hints(A))
It also fails if A is defined in a file that doesn't import Some possible solutions:
|
I think that this is a proper solution. This will be fixed after |
If you want to avoid importing PEP649/749 could change things, but can/should this also be fixed for 3.13/3.12? |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
NameError
onget_type_hints
indataclasses
#122232__annotate__
method for dataclasses frommake_dataclass
#122262The text was updated successfully, but these errors were encountered: