Skip to content

Commit b68c545

Browse files
authored
Bind self to the class being defined when checking multiple inheritance (#18465)
Fixes #18458. When checking base class compatibility, the class being defined is not yet in scope. However, it should be equivalent to the class passed to `bind_and_map_method` with free typevars, as that's exactly what we are currently defining.
1 parent 9fffd9e commit b68c545

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

mypy/checker.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2232,8 +2232,8 @@ def bind_and_map_method(
22322232
is_class_method = sym.node.is_class
22332233

22342234
mapped_typ = cast(FunctionLike, map_type_from_supertype(typ, sub_info, super_info))
2235-
active_self_type = self.scope.active_self_type()
2236-
if isinstance(mapped_typ, Overloaded) and active_self_type:
2235+
active_self_type = fill_typevars(sub_info)
2236+
if isinstance(mapped_typ, Overloaded):
22372237
# If we have an overload, filter to overloads that match the self type.
22382238
# This avoids false positives for concrete subclasses of generic classes,
22392239
# see testSelfTypeOverrideCompatibility for an example.

test-data/unit/check-selftype.test

+19
Original file line numberDiff line numberDiff line change
@@ -2214,3 +2214,22 @@ class Test2:
22142214

22152215
reveal_type(Test2().method) # N: Revealed type is "def (foo: builtins.int, *, bar: builtins.str) -> builtins.bytes"
22162216
[builtins fixtures/tuple.pyi]
2217+
2218+
[case testSelfInMultipleInheritance]
2219+
from typing_extensions import Self
2220+
2221+
class A:
2222+
foo: int
2223+
def method(self: Self, other: Self) -> None:
2224+
self.foo
2225+
other.foo
2226+
2227+
class B:
2228+
bar: str
2229+
def method(self: Self, other: Self) -> None:
2230+
self.bar
2231+
other.bar
2232+
2233+
class C(A, B): # OK: both methods take Self
2234+
pass
2235+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)