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 arne
Recipients arne, eric.smith
Date 2019-08-26.06:29:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1566800952.19.0.982220665774.issue37948@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2019-08-26 06:29:12arnesetrecipients: + arne, eric.smith
2019-08-26 06:29:12arnesetmessageid: <1566800952.19.0.982220665774.issue37948@roundup.psfhosted.org>
2019-08-26 06:29:12arnelinkissue37948 messages
2019-08-26 06:29:11arnecreate