Skip to content

Commit d6120e9

Browse files
authored
Fix bind_self() for overloaded class methods (#7757)
This PR fixes an obvious typo (currently `is_classmethod` is not passed to a nested call). I noticed this while working on #7739
1 parent ad85bc5 commit d6120e9

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

mypy/typeops.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ class B(A): pass
159159
from mypy.infer import infer_type_arguments
160160

161161
if isinstance(method, Overloaded):
162-
return cast(F, Overloaded([bind_self(c, original_type) for c in method.items()]))
162+
return cast(F, Overloaded([bind_self(c, original_type, is_classmethod)
163+
for c in method.items()]))
163164
assert isinstance(method, CallableType)
164165
func = method
165166
if not func.arg_types:

test-data/unit/check-selftype.test

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,3 +533,39 @@ class C:
533533
x: Union[A, C]
534534
reveal_type(x.same) # N: Revealed type is 'Union[builtins.int, def () -> __main__.C*]'
535535
[builtins fixtures/classmethod.pyi]
536+
537+
[case SelfTypeOverloadedClassMethod]
538+
from lib import Base
539+
from typing import overload, Tuple
540+
541+
class Sub(Base):
542+
@overload
543+
@classmethod
544+
def make(cls) -> Sub: ...
545+
@overload
546+
@classmethod
547+
def make(cls, num: int) -> Tuple[Sub, ...]: ...
548+
@classmethod
549+
def make(cls, num=1):
550+
...
551+
552+
class Other(Base): ...
553+
class Double(Sub): ...
554+
555+
reveal_type(Other.make()) # N: Revealed type is '__main__.Other*'
556+
reveal_type(Other.make(3)) # N: Revealed type is 'builtins.tuple[__main__.Other*]'
557+
reveal_type(Double.make()) # N: Revealed type is '__main__.Sub'
558+
reveal_type(Double.make(3)) # N: Revealed type is 'builtins.tuple[__main__.Sub]'
559+
[file lib.pyi]
560+
from typing import overload, TypeVar, Type, Tuple
561+
562+
T = TypeVar('T', bound=Base)
563+
564+
class Base:
565+
@overload
566+
@classmethod
567+
def make(cls: Type[T]) -> T: ...
568+
@overload
569+
@classmethod
570+
def make(cls: Type[T], num: int) -> Tuple[T, ...]: ...
571+
[builtins fixtures/classmethod.pyi]

0 commit comments

Comments
 (0)