Skip to content

Replace hard crash with typecheck error when subclass method has the same name as type alias #13015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()