-
-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
Expose the value passed of typed passed to functools.lru_cache #82746
Comments
In some circumstances, it's useful to be able in inspect the parameters with which an instance of functools.lru_cache was instantiated. It's currently possible to recover the cache's maxsize via the .cache_info() method, but there's no way to recover the value passed for This came up in the context of cloudpickle, a library that tries to extend pickle to support more types (in particular, interactively-defined functions and classes) for use-cases like cluster computing. It's currently not possible to pickle an lru-cache decorated function that's defined in main (which includes, e.g. a Jupyter Notebook). We can almost fix this with a pure library solution (see cloudpipe/cloudpickle#309), but we're currently blocked by the fact that there's no way to recover the value that was passed for For more discussion, see the above linked PR, along with cloudpipe/cloudpickle#178. |
I think it's a good idea to expose the full arguments that people use in lru_cache() |
I have already make a PR for this issue but here's a problem. add a new field to cache_info maybe cause some special problem for the third-party libs what're dependent the cache_info |
I'm fine with adding a new function: >>> f.cache_parameters()
{'maxsize': 200, 'typed'=False} |
I think add new function would be a better way I will make a new PR |
It is easy: diff --git a/Lib/functools.py b/Lib/functools.py
index 3192bd02d9..52c07db749 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -499,7 +499,9 @@ def lru_cache(maxsize=128, typed=False):
# The user_function was passed in directly via the maxsize argument
user_function, maxsize = maxsize, 128
wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
- return update_wrapper(wrapper, user_function)
+ func = update_wrapper(wrapper, user_function)
+ func.cache_parameters = lambda: {'maxsize': maxsize, 'typed': typed}
+ return func
elif maxsize is not None:
raise TypeError(
'Expected first argument to be an integer, a callable, or None') But there are many design questions. Why method instead of just attribute? func.cache_parameters = {'maxsize': maxsize, 'typed': typed} Or maybe just add the "typed" attribute? func.typed = typed Also I consider adding the more general "make_key" parameter to lru_cache(). The "typed" parameter would just specify the default value for "make_key". |
I think to modify in lru_cache should be good in normal circumstance But here's maybe some people modify the wrapped object that underlying in lru_cache So I prefer to add a new function in _functools.c? |
I have already make a new function like this static PyObject *
lru_cache_cache_parameters(lru_cache_object *self)
{
PyObject *cache_parameters = PyDict_New();
PyDict_SetItemString(cache_parameters, "maxsize", PyLong_FromSsize_t(self->maxsize));
PyDict_SetItemString(cache_parameters, "typed", self->typed == 0 ? Py_False : Py_True);
return cache_parameters;
} |
The rule used in the lru_cache implementation is: do not write in C that can be written in Python. |
Yes, you are right, we shouldn't consider about the unstandard using way. I will update my PR BTW, what're means for the "make_key" parameter? |
Scott, thank you for the suggestion. |
Raymond, thanks for fixing many errors for my patch! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: