Skip to content

Allow unhashable arguments when lru_cache is disabled - #16626

Merged
srittau merged 2 commits into
python:mainfrom
tamird:functools-uncached-arguments
Oct 27, 2026
Merged

srittau merged 2 commits into
python:mainfrom
tamird:functools-uncached-arguments

Conversation

@tamird

@tamird tamird commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

A zero-size LRU cache calls the wrapped function without hashing its arguments, so allow lru_cache(0)(len)([]) while keeping the hash check for enabled caches and unknown sizes.

Prepared with Codex.

tamird and others added 2 commits October 27, 2026 14:35
A zero-size cache calls the original function without constructing a key.
Calling lru_cache(0)(len)([]) therefore succeeds, but the wrapper required
every argument to be Hashable.

Default the wrapper's argument type to Hashable and use object when
maxsize is literally zero or False. Preserve that type when copying the
wrapper, and make its own receiver positional-only so a wrapped function
can receive a keyword named self. Unknown sizes retain the hash check.
@github-actions

Copy link
Copy Markdown
Contributor

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

@srittau
srittau merged commit 17cbf25 into python:main Oct 27, 2026
88 checks passed
@tamird
tamird deleted the functools-uncached-arguments branch October 27, 2026 19:19
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.

2 participants