Skip to content

Bind self-types in checkmember for decorated classmethods #19025

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -1203,9 +1203,15 @@ def analyze_class_attribute_access(
t = get_proper_type(t)
if isinstance(t, FunctionLike) and is_classmethod:
t = check_self_arg(t, mx.self_type, False, mx.context, name, mx.msg)
result = add_class_tvars(
t = add_class_tvars(
t, isuper, is_classmethod, is_staticmethod, mx.self_type, original_vars=original_vars
)
if is_decorated and not is_staticmethod:
t = expand_self_type_if_needed(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand why we need to put this self type expansion here. Specifically: this branch is only for decorators. Shouldn't whatever is applying the decorators handle self types instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function handles lookup on classes, we bind self types here for everything that can reasonably contain them as this is the latest moment possible (see Var branch above). Consider a decorator that preserves function signature applied to a def method(self) -> Self. Applying the decorator still yields a Self-returning callable, which is the best representation until we actually want to show it or apply it to something, when Self loses its semantic meaning. Otherwise, for example, that method will be impossible to cleanly override in subclasses.

Copy link
Collaborator

@A5rocks A5rocks May 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright that makes sense then. I didn't realize when this function was called. (fyi I'm unable to resolve things, so you'll have to.)

t, mx, cast(Decorator, node.node).var, itype, is_class=is_classmethod
)

result = t
# __set__ is not called on class objects.
if not mx.is_lvalue:
result = analyze_descriptor_access(result, mx)
Expand Down
41 changes: 41 additions & 0 deletions test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -2221,3 +2221,44 @@ class B:
class C(A, B): # OK: both methods take Self
pass
[builtins fixtures/tuple.pyi]

[case testSelfInFuncDecoratedClassmethod]
from collections.abc import Callable
from typing import Self, TypeVar

T = TypeVar("T")

def debug(make: Callable[[type[T]], T]) -> Callable[[type[T]], T]:
return make

class Foo:
@classmethod
@debug
def make(cls) -> Self:
return cls()

class Bar(Foo): ...

reveal_type(Foo.make()) # N: Revealed type is "__main__.Foo"
reveal_type(Foo().make()) # N: Revealed type is "__main__.Foo"
reveal_type(Bar.make()) # N: Revealed type is "__main__.Bar"
reveal_type(Bar().make()) # N: Revealed type is "__main__.Bar"
[builtins fixtures/tuple.pyi]

[case testSelfInClassDecoratedClassmethod]
from typing import Callable, Generic, TypeVar, Self

T = TypeVar("T")

class W(Generic[T]):
def __init__(self, fn: Callable[..., T]) -> None: ...
def __call__(self) -> T: ...

class Check:
@W
def foo(self) -> Self:
...

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]