Title: @cache breaks type checking when used with @classmethod and typing.Self · Issue #19117 · python/mypy · GitHub
Open Graph Title: @cache breaks type checking when used with @classmethod and typing.Self · Issue #19117 · python/mypy
X Title: @cache breaks type checking when used with @classmethod and typing.Self · Issue #19117 · python/mypy
Description: 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...
Open Graph Description: 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 Int...
X Description: 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 Int...
Opengraph URL: https://github.com/python/mypy/issues/19117
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"@cache breaks type checking when used with @classmethod and typing.Self","articleBody":"**Title**\n@cache breaks type checking when used with @classmethod and typing.Self\n\n### Bug description\nUsing `@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.\n\n### Illustrative repro\n```python\nfrom functools import cache\nfrom typing import Self\n\nclass MyProvider:\n @classmethod\n @cache\n def get_by_name(cls, name: str) -> Self:\n ...\n\nprovider = MyProvider.get_by_name(\"foo\")\nreveal_type(provider) # mypy: Revealed type is \"Any\"\n```\n\n**Without @cache (works as expected)**\n```python\nclass MyProvider:\n @classmethod\n def get_by_name(cls, name: str) -> Self:\n ...\n\nprovider = MyProvider.get_by_name(\"foo\")\nreveal_type(provider) # mypy: Revealed type is \"MyProvider\"\n```\n\n**With @cache and concrete return type (also works)**\n```python\nclass MyProvider:\n @classmethod\n @cache\n def get_by_name(cls, name: str) -> MyProvider:\n ...\n\nprovider = MyProvider.get_by_name(\"foo\")\nreveal_type(provider) # mypy: Revealed type is \"MyProvider\"\n```\n\n**Summary**\nThis 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.\n\n**Environment**\nPython: 3.11 (per pyproject.toml)\nmypy: 1.8+ (as specified in poetry.lock)\nFlags: --strict\nEditor: VSCode (optional, but IntelliSense also affected)\n\n**Related**\nPossibly related: https://github.com/python/typeshed/issues/11280 (but that focuses on general decorator signature issues — this case seems specific to Self + @classmethod + @cache)","author":{"url":"https://github.com/divya-e47","@type":"Person","name":"divya-e47"},"datePublished":"2025-05-19T23:57:28Z","url":"https://github.com/python/mypy/issues/19117"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c6fdd310-d79a-ce32-d29b-86fa27720179 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E000:30501:3B0D39:503DC1:6B002AFE |
| html-safe-nonce | 11e86b752a2abf3819d52c349bd20d664d2c7f850041f8a117db9dae4368bd33 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMDAwOjMwNTAxOjNCMEQzOTo1MDNEQzE6NkIwMDJBRkUiLCJ2aXNpdG9yX2lkIjoiNzI4NDUxMDIzODAwNTI3NTM5MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3488257e21522aa360c2ba18e49c2c0fba0d1c264393601f6d8ed17ce8bf42a9 |
| hovercard-subject-tag | issue:3075257265 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/19117/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a9eb3d1e271b5cf83232df856a4d98dfdfd6f23990a59e57d282188a92c51b09/python/mypy/issues/19117 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a9eb3d1e271b5cf83232df856a4d98dfdfd6f23990a59e57d282188a92c51b09/python/mypy/issues/19117 |
| og:image:alt | 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 Int... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | divya-e47 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width