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 aidan.b.clark
Recipients aidan.b.clark, slebedev
Date 2021-10-19.14:37:47
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1634654267.46.0.759337669411.issue45524@roundup.psfhosted.org>
In-reply-to
Content
[I believe this is fundamentally a dataclass version of https://bugs.python.org/issue41249]

When using `from __future__ import annotations`, calling get_type_hints on the constructor of a dataclass B which inherits from a dataclass A defined in another module will error if dataclass A has type hints which are not imported in the module where dataclass B is defined.

This is best shown by example, if you have foo.py:

```
from __future__ import annotations

import collections
import dataclasses

@dataclasses.dataclass
class A:
  x: collections.OrderedDict
```

and then in bar.py:

```
from __future__ import annotations

import foo
import dataclasses
import typing

@dataclasses.dataclass
class B(foo.A):
  pass

typing.get_type_hints(B)
```

the final line will raise "NameError: name 'collections' is not defined".


This code will not error if you do either of the following:
  * add `import collections` to bar.py.
  * remove the __future__ annotations import from both files.


I am not confident enough on the internals of dataclass to suggest a fix, but potentially a similar approach to that which solved the TypedDict equivalent https://bugs.python.org/issue41249 would work?
History
Date User Action Args
2021-10-19 14:37:47aidan.b.clarksetrecipients: + aidan.b.clark, slebedev
2021-10-19 14:37:47aidan.b.clarksetmessageid: <1634654267.46.0.759337669411.issue45524@roundup.psfhosted.org>
2021-10-19 14:37:47aidan.b.clarklinkissue45524 messages
2021-10-19 14:37:47aidan.b.clarkcreate