Skip to content

@lru_cache needs to be called with () #76182

Closed

Description

@ataraxy
mannequin
BPO 32001
Nosy @rhettinger, @merwok, @serhiy-storchaka

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 = None
closed_at = <Date 2017-11-10.23:19:43.197>
created_at = <Date 2017-11-10.10:20:30.416>
labels = []
title = '@lru_cache needs to be called with ()'
updated_at = <Date 2017-11-10.23:19:43.196>
user = 'https://bugs.python.org/ataraxy'

bugs.python.org fields:

activity = <Date 2017-11-10.23:19:43.196>
actor = 'rhettinger'
assignee = 'none'
closed = True
closed_date = <Date 2017-11-10.23:19:43.197>
closer = 'rhettinger'
components = []
creation = <Date 2017-11-10.10:20:30.416>
creator = 'ataraxy'
dependencies = []
files = []
hgrepos = []
issue_num = 32001
keywords = []
message_count = 5.0
messages = ['306016', '306018', '306040', '306041', '306059']
nosy_count = 4.0
nosy_names = ['rhettinger', 'eric.araujo', 'serhiy.storchaka', 'ataraxy']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue32001'
versions = []

Activity

  1. ataraxy commented on Nov 10, 2017

    ataraxymannequin
    MannequinAuthor

    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 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?

  2. serhiy-storchaka commented on Nov 10, 2017

    @serhiy-storchaka
    Member

    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."

  3. merwok commented on Nov 10, 2017

    @merwok
    Member

    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.

  4. serhiy-storchaka commented on Nov 10, 2017

    @serhiy-storchaka
    Member

    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:

    1. The function initially was not a decorator, and now it can be used as a decorator, but we need to pass additional optional arguments.

    2. The decorator initially didn't take arguments, but we need to pass optional arguments now.

    3. The majority of usages of the decorator don't take arguments.

    Maybe needed to satisfy more than one of the above conditions.

  5. rhettinger commented on Nov 10, 2017

    @rhettinger
    Contributor

    Marking this as rejected. We specifically added a test and error message for this particular misuse of lru_cache.

  6. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions