Skip to content

Commit 6403752

Browse files
authored
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 70d7881 commit 6403752

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
@@ -2188,7 +2188,14 @@ def check_method_override_for_base_with_name(
21882188
else:
21892189
override_class_or_static = defn.func.is_class or defn.func.is_static
21902190
typ, _ = self.node_type_from_base(defn.name, defn.info, defn)
2191-
assert typ is not None
2191+
if typ is None:
2192+
# This may only happen if we're checking `x-redefinition` member
2193+
# and `x` itself is for some reason gone. Normally the node should
2194+
# be reachable from the containing class by its name.
2195+
# The redefinition is never removed, use this as a sanity check to verify
2196+
# the reasoning above.
2197+
assert f"{defn.name}-redefinition" in defn.info.names
2198+
return False
21922199

21932200
original_node = base_attr.node
21942201
# `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)