-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Closed as duplicate of#89687
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
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__ import annotations # <--- this triggers the problem
from dataclasses import dataclass
class LocalDep:
pass
@dataclass
class Parent:
dep1: LocalDep
child.py
from dataclasses import dataclass
from typing import get_type_hints
from parent import Parent
@dataclass
class Child(Parent):
pass
print(get_type_hints(Child))
print(get_type_hints(Child.__init__))
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
jelebruh, Majajashka, tuukkamustonen, ApostolFet and vasyapeteckin
Metadata
Metadata
Assignees
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error