Skip to content

Commit

Permalink
Replace hard crash with typecheck error when subclass method has the …
Browse files Browse the repository at this point in the history
…same name as type alias (python#13015)

Work on python#5425

Co-authored-by: Wesley Wright <[email protected]>
  • Loading branch information
2 people authored and Gobot1234 committed Aug 12, 2022
1 parent 0173723 commit 5001d97
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,8 @@ def check_method_override_for_base_with_name(
else:
original_type = NoneType()
else:
assert False, str(base_attr.node)
# Will always fail to typecheck below, since we know the node is a method
original_type = NoneType()
if isinstance(original_node, (FuncDef, OverloadedFuncDef)):
original_class_or_static = original_node.is_class or original_node.is_static
elif isinstance(original_node, Decorator):
Expand Down
28 changes: 28 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -7300,3 +7300,31 @@ def identity_wrapper(func: FuncT) -> FuncT:
def foo(self: Any) -> str:
return ""

[case testParentClassWithTypeAliasAndSubclassWithMethod]
from typing import Any, Callable, TypeVar

class Parent:
foo = Callable[..., int]
class bar:
pass
import typing as baz
foobar = TypeVar("foobar")

class Child(Parent):
def foo(self, val: int) -> int: # E: Signature of "foo" incompatible with supertype "Parent"
return val
def bar(self, val: str) -> str: # E: Signature of "bar" incompatible with supertype "Parent"
return val
def baz(self, val: float) -> float: # E: Signature of "baz" incompatible with supertype "Parent"
return val
def foobar(self) -> bool: # E: Signature of "foobar" incompatible with supertype "Parent"
return False

x: Parent.foo = lambda: 5
y: Parent.bar = Parent.bar()
z: Parent.baz.Any = 1
child = Child()
a: int = child.foo(1)
b: str = child.bar("abc")
c: float = child.baz(3.4)
d: bool = child.foobar()

0 comments on commit 5001d97

Please sign in to comment.