Message 306016 - Python tracker

Message306016

Author ataraxy
Recipients ataraxy
Date 2017-11-10.10:20:30
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1510309230.43.0.213398074469.issue32001@psf.upfronthosting.co.za>
In-reply-to
Content
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?
History
Date User Action Args
2017-11-10 10:20:30ataraxysetrecipients: + ataraxy
2017-11-10 10:20:30ataraxysetmessageid: <1510309230.43.0.213398074469.issue32001@psf.upfronthosting.co.za>
2017-11-10 10:20:30ataraxylinkissue32001 messages
2017-11-10 10:20:30ataraxycreate