Skip to content

Commit 88fdeaa

Browse files
sterliakovhauntsaninja
authored andcommitted
Prevent a crash when InitVar is redefined with a method in a subclass (#19453)
Fixes #19443. This case is too niche (and should be trivially avoidable), so just not crashing should be good enough. The value is indeed redefined, and trying to massage the plugin to move the `X-redefinition` back to `X` in names is not worth the effort IMO.
1 parent e44d14f commit 88fdeaa

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

mypy/checker.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2169,7 +2169,14 @@ def check_method_override_for_base_with_name(
21692169
else:
21702170
override_class_or_static = defn.func.is_class or defn.func.is_static
21712171
typ, _ = self.node_type_from_base(defn.name, defn.info, defn)
2172-
assert typ is not None
2172+
if typ is None:
2173+
# This may only happen if we're checking `x-redefinition` member
2174+
# and `x` itself is for some reason gone. Normally the node should
2175+
# be reachable from the containing class by its name.
2176+
# The redefinition is never removed, use this as a sanity check to verify
2177+
# the reasoning above.
2178+
assert f"{defn.name}-redefinition" in defn.info.names
2179+
return False
21732180

21742181
original_node = base_attr.node
21752182
# `original_type` can be partial if (e.g.) it is originally an

test-data/unit/check-dataclasses.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,3 +2666,19 @@ class PersonBad(TypedDict):
26662666
class JobBad:
26672667
person: PersonBad = field(default_factory=PersonBad) # E: Argument "default_factory" to "field" has incompatible type "type[PersonBad]"; expected "Callable[[], PersonBad]"
26682668
[builtins fixtures/dict.pyi]
2669+
2670+
[case testDataclassInitVarRedefinitionNoCrash]
2671+
# https://github.com/python/mypy/issues/19443
2672+
from dataclasses import InitVar, dataclass
2673+
2674+
class ClassA:
2675+
def value(self) -> int:
2676+
return 0
2677+
2678+
@dataclass
2679+
class ClassB(ClassA):
2680+
value: InitVar[int]
2681+
2682+
def value(self) -> int: # E: Name "value" already defined on line 10
2683+
return 0
2684+
[builtins fixtures/dict.pyi]

0 commit comments

Comments
 (0)