You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
fromfunctoolsimportcachefromtypingimportSelfclassMyProvider:
@classmethod@cachedefget_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)
classMyProvider:
@classmethoddefget_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)
classMyProvider:
@classmethod@cachedefget_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)
The text was updated successfully, but these errors were encountered:
Title
@cache breaks type checking when used with @classmethod and typing.Self
Bug description
Using
@functools.cache
on a@classmethod
that returnstyping.Self
breaks type inference in mypy and IntelliSense. mypy either falls back toAny
or fails type inference entirely.Illustrative repro
Without @cache (works as expected)
With @cache and concrete return type (also works)
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)
The text was updated successfully, but these errors were encountered: