Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix incompatible overrides of overloaded methods in concrete subclasses#14017

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
hauntsaninja merged 7 commits intopython:masterfromhauntsaninja:generic-self
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from1 commit
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
NextNext commit
Fix incompatible overrides of overloaded methods in concrete subclasses
Fixes#14002
  • Loading branch information
@hauntsaninja
hauntsaninja committedNov 5, 2022
commit5c90d1e88e3c714f4ac45c1296079927f1875377
13 changes: 13 additions & 0 deletionsmypy/checker.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1868,6 +1868,19 @@ 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 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 yourF test below, I think the override is actually compatible: the base class says nothing about what the method should return if self is anA[bytes].

Copy link
CollaboratorAuthor

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 aCallable[..., 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
51 changes: 51 additions & 0 deletionstest-data/unit/check-selftype.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -128,6 +128,57 @@ reveal_type(cast(A, C()).copy()) # N: Revealed type is "__main__.A"

[builtins fixtures/bool.pyi]

[case testSelfTypeOverrideCompatibility]
from typing import overload, TypeVar, Generic

T = TypeVar("T", str, int)

class A(Generic[T]):
@overload
def f(self: A[int]) -> int: ...
@overload
def f(self: A[str]) -> str: ...
def f(self): ...

class B(A[T]):
@overload
def f(self: A[int]) -> int: ...
@overload
def f(self: A[str]) -> str: ...
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]): # E: Value of type variable "T" of "A" cannot be "bytes"
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

[case testSelfTypeSuper]
from typing import TypeVar, cast

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp