This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: With PEP 468, the lru cache not longer needs to sort keyword args
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: python-dev, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2017-01-08 10:01 by rhettinger, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
lru_468.diff rhettinger, 2017-01-08 10:01 review
lru_cache_pep468.patch serhiy.storchaka, 2017-01-08 11:52 review
Messages (6)
msg284971 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-01-08 10:01
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.
msg284972 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-01-08 10:25
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.
msg284973 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-01-08 10:35
> 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).
msg284984 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-01-08 11:52
Here is a patch that fixes also C implementation and has tests.
msg285013 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2017-01-09 01:29
New changeset 48c750c26710 by Raymond Hettinger in branch '3.6':
Issue #29203:  functools.lru_cache() now respects PEP 468
https://hg.python.org/cpython/rev/48c750c26710
msg285014 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2017-01-09 02:04
New changeset cc47d385512b by Raymond Hettinger in branch 'default':
Complete the merge for issue #29203
https://hg.python.org/cpython/rev/cc47d385512b
History
Date User Action Args
2022-04-11 14:58:41adminsetgithub: 73389
2017-01-09 07:27:42serhiy.storchakasetstage: patch review -> resolved
2017-01-09 07:21:45rhettingersetstatus: open -> closed
resolution: fixed
2017-01-09 02:04:38python-devsetmessages: + msg285014
2017-01-09 01:29:38python-devsetnosy: + python-dev
messages: + msg285013
2017-01-08 11:52:03serhiy.storchakasetfiles: + lru_cache_pep468.patch
type: performance -> behavior
messages: + msg284984

versions: + Python 3.6
2017-01-08 10:35:38rhettingersetmessages: + msg284973
2017-01-08 10:25:24serhiy.storchakasetmessages: + msg284972
2017-01-08 10:01:29rhettingercreate