Skip to content
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

Fix incompatible overrides of overloaded methods in concrete subclasses #14017

Merged
merged 7 commits into from
Nov 10, 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
17 changes: 17 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1868,6 +1868,23 @@ def check_method_override_for_base_with_name(
original_class_or_static = False # a variable can't be class or static

if isinstance(original_type, FunctionLike):
active_self_type = self.scope.active_self_type()
if isinstance(original_type, Overloaded) and active_self_type:
# If we have an overload, filter to overloads that match the self type.
# This avoids false positives for concrete subclasses of generic classes,
# see testSelfTypeOverrideCompatibility for an example.
# It's possible we might want to do this as part of bind_and_map_method
filtered_items = [
item
for item in original_type.items
if not item.arg_types or is_subtype(active_self_type, item.arg_types[0])
]
# If we don't have any filtered_items, maybe it's always a valid override
# of the superclass? However if you get to that point you're in murky type
# territory anyway, so we just preserve the type and have the behaviour match
# that of older versions of mypy.
if filtered_items:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are no filtered_items, shouldn't we always treat the override as compatible? e.g. in your F test below, I think the override is actually compatible: the base class says nothing about what the method should return if self is an A[bytes].

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wasn't sure about this, but it seemed like a weird situation so I chose to fail. Feels sketchy to introduce a Callable[..., Any] ... I'll add a comment and if it ever comes up we can reconsider

original_type = Overloaded(filtered_items)
original_type = self.bind_and_map_method(base_attr, original_type, defn.info, base)
if original_node and is_property(original_node):
original_type = get_property_type(original_type)
Expand Down
86 changes: 86 additions & 0 deletions test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,92 @@ class B2(A[T]):
def f(self: A[bytes]) -> bytes: ...
def f(self): ...

class C(A[int]):
def f(self) -> int: ...

class D(A[str]):
def f(self) -> int: ... # E: Signature of "f" incompatible with supertype "A" \
# N: Superclass: \
# N: @overload \
# N: def f(self) -> str \
# N: Subclass: \
# N: def f(self) -> int

class E(A[T]):
def f(self) -> int: ... # E: Signature of "f" incompatible with supertype "A" \
# N: Superclass: \
# N: @overload \
# N: def f(self) -> int \
# N: @overload \
# N: def f(self) -> str \
# N: Subclass: \
# N: def f(self) -> int


class F(A[bytes]):
# Note there's an argument to be made that this is actually compatible with the supertype
def f(self) -> bytes: ... # E: Signature of "f" incompatible with supertype "A" \
# N: Superclass: \
# N: @overload \
# N: def f(self) -> int \
# N: @overload \
# N: def f(self) -> str \
# N: Subclass: \
# N: def f(self) -> bytes

class G(A):
def f(self): ...

class H(A[int]):
def f(self): ...

class I(A[int]):
def f(*args): ...

class J(A[int]):
def f(self, arg) -> int: ... # E: Signature of "f" incompatible with supertype "A" \
# N: Superclass: \
# N: @overload \
# N: def f(self) -> int \
# N: Subclass: \
# N: def f(self, arg: Any) -> int

[builtins fixtures/tuple.pyi]

[case testSelfTypeOverrideCompatibilityTypeVar-xfail]
from typing import overload, TypeVar, Union

AT = TypeVar("AT", bound="A")

class A:
@overload
def f(self: AT, x: int) -> AT: ...
@overload
def f(self, x: str) -> None: ...
@overload
def f(self: AT) -> bytes: ...
def f(*a, **kw): ...

class B(A):
@overload # E: Signature of "f" incompatible with supertype "A" \
# N: Superclass: \
# N: @overload \
# N: def f(self, x: int) -> B \
# N: @overload \
# N: def f(self, x: str) -> None \
# N: @overload \
# N: def f(self) -> bytes \
# N: Subclass: \
# N: @overload \
# N: def f(self, x: int) -> B \
# N: @overload \
# N: def f(self, x: str) -> None
def f(self, x: int) -> B: ...
@overload
def f(self, x: str) -> None: ...
def f(*a, **kw): ...
[builtins fixtures/dict.pyi]

[case testSelfTypeSuper]
from typing import TypeVar, cast

Expand Down