Skip to content

Commit 7c391dd

Browse files
authored
Fix crash on invalid callable property override (#17352)
Fixes #16896 Fix is simple, do not assume that an error context given by the caller of the override check for callable type is a method defining such type, because it may be a property.
1 parent 09e6a2b commit 7c391dd

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2279,7 +2279,7 @@ def erase_override(t: Type) -> Type:
22792279
):
22802280
arg_type_in_super = original.arg_types[i]
22812281

2282-
if isinstance(node, FuncDef):
2282+
if isinstance(node, FuncDef) and not node.is_property:
22832283
context: Context = node.arguments[i + len(override.bound_args)]
22842284
else:
22852285
context = node

test-data/unit/check-functions.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3302,3 +3302,25 @@ class C: # Note: Generic[T] missing
33023302

33033303
def nope(self, x: T) -> None:
33043304
self.x = x # E: Incompatible types in assignment (expression has type "T@nope", variable has type "T@bad_idea")
3305+
3306+
[case testNoCrashOnBadCallablePropertyOverride]
3307+
from typing import Callable, Union
3308+
3309+
class C: ...
3310+
class D: ...
3311+
3312+
A = Callable[[C], None]
3313+
B = Callable[[D], None]
3314+
3315+
class Foo:
3316+
@property
3317+
def method(self) -> Callable[[int, Union[A, B]], None]:
3318+
...
3319+
3320+
class Bar(Foo):
3321+
@property
3322+
def method(self) -> Callable[[int, A], None]: # E: Argument 2 of "method" is incompatible with supertype "Foo"; supertype defines the argument type as "Union[Callable[[C], None], Callable[[D], None]]" \
3323+
# N: This violates the Liskov substitution principle \
3324+
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
3325+
...
3326+
[builtins fixtures/property.pyi]

0 commit comments

Comments
 (0)