[issue32001] @lru_cache needs to be called with ()

Tom Hale report at bugs.python.org
Fri Nov 10 05:20:30 EST 2017


New submission from Tom Hale <tom at hale.ee>:

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_cache`](https://docs.python.org/3/library/functools.html#functools.lru_cache) gives the function definition:

>     @functools.lru_cache(maxsize=128, typed=False)

This says to me that `maxsize` is 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:

    if callable(maxsize):
        def decorating_function(user_function):
            wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
            return update_wrapper(wrapper, user_function)
        return decorating_function(max_size) # yes, max_size is the function in this case O:)


=====================================================================
=====================================================================

Would you accept a PR based on this solution?

----------
messages: 306016
nosy: ataraxy
priority: normal
severity: normal
status: open
title: @lru_cache needs to be called with ()

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32001>
_______________________________________


More information about the Python-bugs-list mailing list