Skip to content

Commit 203bd64

Browse files
authored
Replace hard crash with typecheck error when subclass method has the same name as type alias (#13015)
Work on #5425 Co-authored-by: Wesley Wright <[email protected]>
1 parent e046e20 commit 203bd64

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

mypy/checker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1600,7 +1600,8 @@ def check_method_override_for_base_with_name(
16001600
else:
16011601
original_type = NoneType()
16021602
else:
1603-
assert False, str(base_attr.node)
1603+
# Will always fail to typecheck below, since we know the node is a method
1604+
original_type = NoneType()
16041605
if isinstance(original_node, (FuncDef, OverloadedFuncDef)):
16051606
original_class_or_static = original_node.is_class or original_node.is_static
16061607
elif isinstance(original_node, Decorator):

test-data/unit/check-classes.test

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7300,3 +7300,31 @@ def identity_wrapper(func: FuncT) -> FuncT:
73007300
def foo(self: Any) -> str:
73017301
return ""
73027302

7303+
[case testParentClassWithTypeAliasAndSubclassWithMethod]
7304+
from typing import Any, Callable, TypeVar
7305+
7306+
class Parent:
7307+
foo = Callable[..., int]
7308+
class bar:
7309+
pass
7310+
import typing as baz
7311+
foobar = TypeVar("foobar")
7312+
7313+
class Child(Parent):
7314+
def foo(self, val: int) -> int: # E: Signature of "foo" incompatible with supertype "Parent"
7315+
return val
7316+
def bar(self, val: str) -> str: # E: Signature of "bar" incompatible with supertype "Parent"
7317+
return val
7318+
def baz(self, val: float) -> float: # E: Signature of "baz" incompatible with supertype "Parent"
7319+
return val
7320+
def foobar(self) -> bool: # E: Signature of "foobar" incompatible with supertype "Parent"
7321+
return False
7322+
7323+
x: Parent.foo = lambda: 5
7324+
y: Parent.bar = Parent.bar()
7325+
z: Parent.baz.Any = 1
7326+
child = Child()
7327+
a: int = child.foo(1)
7328+
b: str = child.bar("abc")
7329+
c: float = child.baz(3.4)
7330+
d: bool = child.foobar()

0 commit comments

Comments
 (0)