Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With PEP 468, the lru cache not longer needs to sort keyword args #73389

Closed
rhettinger opened this issue Jan 8, 2017 · 6 comments
Closed

With PEP 468, the lru cache not longer needs to sort keyword args #73389

rhettinger opened this issue Jan 8, 2017 · 6 comments
Assignees
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@rhettinger
Copy link
Contributor

BPO 29203
Nosy @rhettinger, @serhiy-storchaka
Files
  • lru_468.diff
  • lru_cache_pep468.patch
  • 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 = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2017-01-09.07:21:45.489>
    created_at = <Date 2017-01-08.10:01:29.924>
    labels = ['3.7', 'type-bug', 'library']
    title = 'With PEP 468, the lru cache not longer needs to sort keyword args'
    updated_at = <Date 2017-01-09.07:27:42.854>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2017-01-09.07:27:42.854>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2017-01-09.07:21:45.489>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2017-01-08.10:01:29.924>
    creator = 'rhettinger'
    dependencies = []
    files = ['46210', '46213']
    hgrepos = []
    issue_num = 29203
    keywords = ['patch']
    message_count = 6.0
    messages = ['284971', '284972', '284973', '284984', '285013', '285014']
    nosy_count = 3.0
    nosy_names = ['rhettinger', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue29203'
    versions = ['Python 3.6', 'Python 3.7']

    @rhettinger
    Copy link
    Contributor Author

    Since the ordering of keyword arguments is now guaranteed, the LRU cache doesn't need to sort any longer.

    The will give a small change in behavior that I don't care about. A call f(a=1, b=2) would now be cached separately from f(b=2, a=1). That won't arise often and isn't really different than the status quo where f(1, b=2) or f(1, 2) are already cached separately. Overall it is a net win by saving the sorting step on every call.

    @rhettinger rhettinger added performance Performance or resource usage 3.7 (EOL) end of life labels Jan 8, 2017
    @rhettinger rhettinger added the stdlib Python modules in the Lib dir label Jan 8, 2017
    @serhiy-storchaka
    Copy link
    Member

    I think this is a bug and it should be fixed in 3.6. Currenly lru_cache breaks PEP-468 (Preserving Keyword Argument Order).

    >>> from functools import lru_cache
    >>> @lru_cache()
    ... def f(**kwargs):
    ...     return list(kwargs.items())
    ... 
    >>> f(a=1, b=2)
    [('a', 1), ('b', 2)]
    >>> f(b=2, a=1)
    [('a', 1), ('b', 2)]

    C implementation should be changed too.

    @rhettinger
    Copy link
    Contributor Author

    I think this is a bug and it should be fixed in 3.6.

    I concur. Do you care to whip-up a patch (it is late here).

    @serhiy-storchaka
    Copy link
    Member

    Here is a patch that fixes also C implementation and has tests.

    @serhiy-storchaka serhiy-storchaka added type-bug An unexpected behavior, bug, or error and removed performance Performance or resource usage labels Jan 8, 2017
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jan 9, 2017

    New changeset 48c750c26710 by Raymond Hettinger in branch '3.6':
    Issue bpo-29203: functools.lru_cache() now respects PEP-468
    https://hg.python.org/cpython/rev/48c750c26710

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jan 9, 2017

    New changeset cc47d385512b by Raymond Hettinger in branch 'default':
    Complete the merge for issue bpo-29203
    https://hg.python.org/cpython/rev/cc47d385512b

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants