You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If we have two classes split into two files with ForwardRefs, then you cannot get hints on __init__ method of child
parent.py:
from __future__ importannotations# <--- this triggers the problemfromdataclassesimportdataclassclassLocalDep:
pass@dataclassclassParent:
dep1: LocalDep
I've expected to have two last lines printing the same
Actually, first print gives a correct output, but the second call raises an error
Traceback (most recent call last):
File "/home/tishka17/src/dishka/tmp/dtc/child.py", line 16, in <module>
print(get_type_hints(Child.__init__))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/typing.py", line 2414, in get_type_hints
hints[name] = _eval_type(value, globalns, localns)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/typing.py", line 395, in _eval_type
return t._evaluate(globalns, localns, recursive_guard)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/typing.py", line 905, in _evaluate
eval(self.__forward_code__, globalns, localns),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<string>", line 1, in <module>
NameError: name 'LocalDep' is not defined
As far as I udnerstood, we inside dataclass logic it copies__init__ method annotations and that leads to an error. We do not copy class attrs (fields), so hints are retrieved from parent class directly (get_type_hints knows about MRO)
CPython versions tested on:
3.11, 3.13, 3.14
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered:
Bug report
Bug description:
If we have two classes split into two files with ForwardRefs, then you cannot get hints on
__init__
method of childparent.py
:child.py
I've expected to have two last lines printing the same
Actually, first print gives a correct output, but the second call raises an error
As far as I udnerstood, we inside
dataclass
logic it copies__init__
method annotations and that leads to an error. We do not copy class attrs (fields), so hints are retrieved from parent class directly (get_type_hints
knows about MRO)CPython versions tested on:
3.11, 3.13, 3.14
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered: