classification
Title: lrucache should reject maxsize as a function
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.5, Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: jason.coombs, jwilk, python-dev, rhettinger, serhiy.storchaka
Priority: low Keywords: patch

Created on 2014-08-11 19:53 by jason.coombs, last changed 2014-08-13 04:59 by rhettinger. This issue is now closed.

Files
File name Uploaded Description Edit
lru.diff rhettinger, 2014-08-12 04:35 Add type check for maxsize review
Messages (4)
msg225202 - (view) Author: Jason R. Coombs (jason.coombs) * (Python committer) Date: 2014-08-11 19:53
In https://bitbucket.org/jaraco/backports.functools_lru_cache/issue/1/python-2-attributeerror-int-object-has-no, a user was confused when he tried to use the lrucache decorator incorrectly, passing the wrapped function directly to lrucache. Consider:

    @lrucache
    def expensive(param):
        pass

One can even get away with profiling that now decorated function:

    for x in range(10000):
        expensive(x)

The test will run without error, but it's not doing what the user thinks it's doing. In the first section, it's creating a decorator, and in the second section, it's wrapping an int in that decorator, but because the wrapper is never called, an error is never raised.

I propose adding a simple check that if maxsize is callable, raise a TypeError. That would prevent unintentional misuse and subtle non-failure with minimal impact on performance.
msg225241 - (view) Author: Roundup Robot (python-dev) Date: 2014-08-12 19:45
New changeset 780693490c84 by Raymond Hettinger in branch '3.4':
Issue 22184: Early detection and reporting of missing lru_cache parameters
http://hg.python.org/cpython/rev/780693490c84
msg225251 - (view) Author: Jason R. Coombs (jason.coombs) * (Python committer) Date: 2014-08-13 00:45
Awesome. Thanks for the quick implementation.
msg225254 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-08-13 04:59
> Awesome. Thanks for the quick implementation.

Happy to do it.  Thanks for the clean bug report.
History
Date User Action Args
2014-08-13 04:59:43rhettingersetmessages: + msg225254
2014-08-13 00:45:10jason.coombssetmessages: + msg225251
2014-08-12 19:46:12rhettingersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2014-08-12 19:45:36python-devsetnosy: + python-dev
messages: + msg225241
2014-08-12 18:37:49jwilksetnosy: + jwilk
2014-08-12 06:12:02serhiy.storchakasetnosy: + serhiy.storchaka
2014-08-12 04:35:26rhettingersetfiles: + lru.diff
keywords: + patch
stage: patch review
type: behavior
versions: + Python 3.4
2014-08-12 04:15:42rhettingersetpriority: normal -> low
assignee: rhettinger
versions: + Python 3.5
2014-08-11 19:53:01jason.coombscreate