[New-bugs-announce] [issue37948] get_type_hints fails if there are un-annotated fields in a dataclass
Arne Recknagel
report at bugs.python.org
Mon Aug 26 02:29:12 EDT 2019
New submission from Arne Recknagel <arne.recknagel at hotmail.com>:
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
----------
messages: 350488
nosy: arne, eric.smith
priority: normal
severity: normal
status: open
title: get_type_hints fails if there are un-annotated fields in a dataclass
type: behavior
versions: Python 3.7, Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37948>
_______________________________________
More information about the New-bugs-announce
mailing list