Skip to content

Follow-up after #19025: test and cleanup #19294

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
5 changes: 1 addition & 4 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -1227,9 +1227,6 @@ def analyze_class_attribute_access(
is_classmethod = (is_decorated and cast(Decorator, node.node).func.is_class) or (
isinstance(node.node, SYMBOL_FUNCBASE_TYPES) and node.node.is_class
)
is_staticmethod = (is_decorated and cast(Decorator, node.node).func.is_static) or (
isinstance(node.node, SYMBOL_FUNCBASE_TYPES) and node.node.is_static
)
t = get_proper_type(t)
is_trivial_self = False
if isinstance(node.node, Decorator):
Expand All @@ -1247,7 +1244,7 @@ def analyze_class_attribute_access(
original_vars=original_vars,
is_trivial_self=is_trivial_self,
)
if is_decorated and not is_staticmethod:
if is_decorated:
t = expand_self_type_if_needed(
t, mx, cast(Decorator, node.node).var, itype, is_class=is_classmethod
)
Expand Down
37 changes: 37 additions & 0 deletions test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -2260,3 +2260,40 @@ class Check:
reveal_type(Check.foo()) # N: Revealed type is "def () -> __main__.Check"
reveal_type(Check().foo()) # N: Revealed type is "__main__.Check"
[builtins fixtures/tuple.pyi]

[case testSelfInClassmethodWithOtherSelfMethod]
from typing import Any, Callable, Self, TypeVar

_C = TypeVar("_C", bound=Callable[..., Any])

def identity(func: _C, /) -> _C:
return func

class A:
def meth(self) -> Self: ...

@classmethod
def other_meth(cls) -> Self:
reveal_type(cls.meth) # N: Revealed type is "def [Self <: __main__.A] (self: Self`1) -> Self`1"
reveal_type(A.meth) # N: Revealed type is "def [Self <: __main__.A] (self: Self`2) -> Self`2"
return cls().meth()

class B:
@identity
def meth(self) -> Self: ...

@classmethod
def other_meth(cls) -> Self:
reveal_type(cls.meth) # N: Revealed type is "def [Self <: __main__.B] (self: Self`6) -> Self`6"
reveal_type(B.meth) # N: Revealed type is "def [Self <: __main__.B] (self: Self`7) -> Self`7"
return cls().meth()

class C:
@classmethod
def other_meth(cls) -> Self: ...

def meth(self) -> Self:
reveal_type(self.other_meth) # N: Revealed type is "def () -> Self`0"
reveal_type(type(self).other_meth) # N: Revealed type is "def () -> Self`0"
return self.other_meth()
[builtins fixtures/tuple.pyi]