Skip to content

[cachetools] Fix TTLCache/TLRUCache constructor type inference - #16251

Open
ekanshul wants to merge 1 commit into
python:mainfrom
ekanshul:cachetools-timed-cache-self-typevars
Open

[cachetools] Fix TTLCache/TLRUCache constructor type inference#16251
ekanshul wants to merge 1 commit into
python:mainfrom
ekanshul:cachetools-timed-cache-self-typevars

Conversation

@ekanshul

Copy link
Copy Markdown
Contributor

Closes #15798

Use the class type variables instead of fresh _KT2/_VT2 in the self annotation of the first TTLCache.__init__ and TLRUCache.__init__ overloads (with the usual #11780 pyright ignore, as in builtins.dict), so type checkers can infer the key and value types; adds test cases for the issue examples.

Agent used: Claude Code

The first overload of TTLCache.__init__ and TLRUCache.__init__ annotated
self with fresh type variables (_KT2, _VT2) instead of the class-scoped
_KT and _VT. Those method-scoped type variables are never solved from
the arguments, so pyright infers TTLCache[_KT2, _VT2, float] and mypy
infers TTLCache[Never, Never, float] even when the target type is
declared. Use the class type variables in the self annotation (with the
usual python#11780 pyright ignore, as in builtins.dict) and add test cases.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cachetools: Subclassing TTLCache[KT, VT] loses generic type inference on instantiation

1 participant