Skip to content

Commit

Permalink
Fix crash on invalid callable property override (#17352)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ilevkivskyi committed Jun 9, 2024
1 parent 09e6a2b commit 7c391dd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2279,7 +2279,7 @@ def erase_override(t: Type) -> Type:
):
arg_type_in_super = original.arg_types[i]

if isinstance(node, FuncDef):
if isinstance(node, FuncDef) and not node.is_property:
context: Context = node.arguments[i + len(override.bound_args)]
else:
context = node
Expand Down
22 changes: 22 additions & 0 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -3302,3 +3302,25 @@ class C: # Note: Generic[T] missing

def nope(self, x: T) -> None:
self.x = x # E: Incompatible types in assignment (expression has type "T@nope", variable has type "T@bad_idea")

[case testNoCrashOnBadCallablePropertyOverride]
from typing import Callable, Union

class C: ...
class D: ...

A = Callable[[C], None]
B = Callable[[D], None]

class Foo:
@property
def method(self) -> Callable[[int, Union[A, B]], None]:
...

class Bar(Foo):
@property
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]]" \
# N: This violates the Liskov substitution principle \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
...
[builtins fixtures/property.pyi]

0 comments on commit 7c391dd

Please sign in to comment.