-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-144030: add check that argument is callable to Python version of functools.lru_cache #144031
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
base: main
Are you sure you want to change the base?
gh-144030: add check that argument is callable to Python version of functools.lru_cache #144031
Conversation
sobolevn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit :)
Co-authored-by: sobolevn <mail@sobolevn.me>
vstinner
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Misc/NEWS.d/next/Library/2026-01-19-12-48-59.gh-issue-144030.7OK_gB.rst
Outdated
Show resolved
Hide resolved
| return decorating_function | ||
|
|
||
| def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo): | ||
| if not callable(user_function): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this check in lru_wrapper directly? it should only be done in def decorating_function() I think.
We are doing the check twice otherwise (when we do not call the decorator with ())
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that means we are doing the "is it callable" already twice then. in practice, cpython never uses the pure python version of this code, and the "is it callable" check happens in the C version of _lru_cache_wrapper. my change is just mirroring what the C code is already doing.
unless there is a huge push for it, I kind of don't want to do a big refactoring of the C/Python code here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no big refactoring actually. It is just about moving your check in another function (a few lines above). Considering the LRU wrapper cache function is already an implementation detail, I expect the caller to check for callability, not the wrapper itself.
…OK_gB.rst Co-authored-by: AN Long <aisk@users.noreply.github.com>
The C version has this check, the Python version doesn't so far. This PR makes the behaviours agree again
functools.lru_cachedoes not check that argument is callable #144030