Message404307
[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? |
|
Date |
User |
Action |
Args |
2021-10-19 14:37:47 | aidan.b.clark | set | recipients:
+ aidan.b.clark, slebedev |
2021-10-19 14:37:47 | aidan.b.clark | set | messageid: <1634654267.46.0.759337669411.issue45524@roundup.psfhosted.org> |
2021-10-19 14:37:47 | aidan.b.clark | link | issue45524 messages |
2021-10-19 14:37:47 | aidan.b.clark | create | |
|