Skip to content

Commit 4749787

Browse files
add a check for classmethods
1 parent 7848677 commit 4749787

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

mypy/semanal.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,8 @@ def is_expected_self_type(self, typ: Type, is_classmethod: bool) -> bool:
11911191
sym = self.lookup_qualified(typ.name, typ, suppress_errors=True)
11921192
if sym is not None and sym.fullname in TYPE_NAMES and typ.args:
11931193
return self.is_expected_self_type(typ.args[0], is_classmethod=False)
1194+
if sym is not None and sym.fullname in ANNOTATED_TYPE_NAMES and typ.args:
1195+
return self.is_expected_self_type(typ.args[0], is_classmethod=True)
11941196
return False
11951197
if isinstance(typ, TypeVarType):
11961198
return typ == self.type.self_type

test-data/unit/check-selftype.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,7 +1559,12 @@ class C:
15591559
def foo(self: Annotated[Self, "some_metadata"], x: int) -> None:
15601560
...
15611561

1562+
@classmethod
1563+
def bar(self: Annotated[type[Self], "some_metadata"], x: int) -> None:
1564+
...
1565+
15621566
reveal_type(C.foo) # N: Revealed type is "def [Self <: __main__.C] (self: Self`2, x: builtins.int)"
1567+
reveal_type(C.bar) # N: Revealed type is "def (x: builtins.int)"
15631568
[builtins fixtures/tuple.pyi]
15641569

15651570
[case testTypingSelfOverload]

0 commit comments

Comments
 (0)