Skip to content

@cache breaks type checking when used with @classmethod and typing.Self #19117

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

Closed
divya-e47 opened this issue May 19, 2025 · 1 comment
Closed
Labels
bug mypy got something wrong

Comments

@divya-e47
Copy link

Title
@cache breaks type checking when used with @classmethod and typing.Self

Bug description

Using @functools.cache on a @classmethod that returns typing.Self breaks type inference in mypy and IntelliSense. mypy either falls back to Any or fails type inference entirely.

Illustrative repro

from functools import cache
from typing import Self

class MyProvider:
    @classmethod
    @cache
    def get_by_name(cls, name: str) -> Self:
        ...

provider = MyProvider.get_by_name("foo")
reveal_type(provider)  # mypy: Revealed type is "Any"

Without @cache (works as expected)

class MyProvider:
    @classmethod
    def get_by_name(cls, name: str) -> Self:
        ...

provider = MyProvider.get_by_name("foo")
reveal_type(provider)  # mypy: Revealed type is "MyProvider"

With @cache and concrete return type (also works)

class MyProvider:
    @classmethod
    @cache
    def get_by_name(cls, name: str) -> MyProvider:
        ...

provider = MyProvider.get_by_name("foo")
reveal_type(provider)  # mypy: Revealed type is "MyProvider"

Summary
This shows that Self return types on cached classmethods are not handled correctly by mypy. Replacing Self with the concrete class is a workaround, but sacrifices subclassing flexibility.

Environment
Python: 3.11 (per pyproject.toml)
mypy: 1.8+ (as specified in poetry.lock)
Flags: --strict
Editor: VSCode (optional, but IntelliSense also affected)

Related
Possibly related: python/typeshed#11280 (but that focuses on general decorator signature issues — this case seems specific to Self + @classmethod + @cache)

@divya-e47 divya-e47 added the bug mypy got something wrong label May 19, 2025
@sterliakov
Copy link
Collaborator

Note that latest mypy does not produce Any and produces unbounded Self. There's a PR for the duplicate issue, it may eventually get merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants