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
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:
assignee=Noneclosed_at=<Date2017-11-10.23:19:43.197>created_at=<Date2017-11-10.10:20:30.416>labels= []
title='@lru_cache needs to be called with ()'updated_at=<Date2017-11-10.23:19:43.196>user='https://bugs.python.org/ataraxy'
However, it doesn't like being called without an argument:
Python 3.6.3 (default, Oct 24 2017, 14:48:20)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import functools
>>> @functools.lru_cache
... def f(): ...
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/functools.py", line 477, in lru_cache
raise TypeError('Expected maxsize to be an integer or None')
TypeError: Expected maxsize to be an integer or None
>>>
ifcallable(maxsize):
defdecorating_function(user_function):
wrapper=_lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
returnupdate_wrapper(wrapper, user_function)
returndecorating_function(max_size) # yes, max_size is the function in this case O:)=====================================================================
I'm -1. The beginner can make use lru_cache incorrectly the first time, but nobody will make this mistake twice. It is not worth to complicate the code (by the way, your code contains a bug) for saving from at most one mistake for all life.
"Special cases aren't special enough to break the rules."
I remember seeing code needed to make a decorator that works with and without parens and finding it quite obscure and confusing for a long time. When you understand the distinction between, things become clear and simple again. I like that the stdlib keeps that distinction.
We could change the doc to include an example without parameters.
We may also make sure to call lru_cache a decorator factory, with a glossary link or link to decorator reference doc.
Activity
ataraxy commentedon Nov 10, 2017
This comes from a question I raised on StackOverflow:
https://stackoverflow.com/q/47218313/5353461
I've copied the text below
=====================================================================
=====================================================================
The documentation for
lru_cachegives the function definition:This says to me that
maxsizeis optional.However, it doesn't like being called without an argument:
Python 3.6.3 (default, Oct 24 2017, 14:48:20) [GCC 7.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import functools >>> @functools.lru_cache ... def f(): ... ... Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.6/functools.py", line 477, in lru_cache raise TypeError('Expected maxsize to be an integer or None') TypeError: Expected maxsize to be an integer or None >>>Calling with an argument is fine:
>>> @functools.lru_cache(8) ... def f(): ... ... >>>Am I misreading the documentation?
=====================================================================
=====================================================================
The answer presented this solution:
=====================================================================
Would you accept a PR based on this solution?
serhiy-storchaka commentedon Nov 10, 2017
I'm -1. The beginner can make use lru_cache incorrectly the first time, but nobody will make this mistake twice. It is not worth to complicate the code (by the way, your code contains a bug) for saving from at most one mistake for all life.
"Special cases aren't special enough to break the rules."
merwok commentedon Nov 10, 2017
I remember seeing code needed to make a decorator that works with and without parens and finding it quite obscure and confusing for a long time. When you understand the distinction between, things become clear and simple again. I like that the stdlib keeps that distinction.
We could change the doc to include an example without parameters.
We may also make sure to call lru_cache a decorator factory, with a glossary link or link to decorator reference doc.
serhiy-storchaka commentedon Nov 10, 2017
One such decorator was added in 3.7: xmlrpc.server.register_function (see bpo-7769). I don't think lru_cache should follow this example.
There are few cases in which such obscure decorators are more or less appropriate:
The function initially was not a decorator, and now it can be used as a decorator, but we need to pass additional optional arguments.
The decorator initially didn't take arguments, but we need to pass optional arguments now.
The majority of usages of the decorator don't take arguments.
Maybe needed to satisfy more than one of the above conditions.
rhettinger commentedon Nov 10, 2017
Marking this as rejected. We specifically added a test and error message for this particular misuse of lru_cache.