Issue #29203: functools.lru_cache() now respects PEP 468
diff --git a/Lib/functools.py b/Lib/functools.py
index 45e5f87..030c91b 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -421,7 +421,7 @@
def _make_key(args, kwds, typed,
kwd_mark = (object(),),
fasttypes = {int, str, frozenset, type(None)},
- sorted=sorted, tuple=tuple, type=type, len=len):
+ tuple=tuple, type=type, len=len):
"""Make a cache key from optionally typed positional and keyword arguments
The key is constructed in a way that is flat as possible rather than
@@ -434,14 +434,13 @@
"""
key = args
if kwds:
- sorted_items = sorted(kwds.items())
key += kwd_mark
- for item in sorted_items:
+ for item in kwds.items():
key += item
if typed:
key += tuple(type(v) for v in args)
if kwds:
- key += tuple(type(v) for k, v in sorted_items)
+ key += tuple(type(v) for v in kwds.values())
elif len(key) == 1 and type(key[0]) in fasttypes:
return key[0]
return _HashedSeq(key)
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index 3a40861..27eb576 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -1306,6 +1306,16 @@
self.assertEqual(fib.cache_info(),
self.module._CacheInfo(hits=0, misses=0, maxsize=None, currsize=0))
+ def test_kwargs_order(self):
+ # PEP 468: Preserving Keyword Argument Order
+ @self.module.lru_cache(maxsize=10)
+ def f(**kwargs):
+ return list(kwargs.items())
+ self.assertEqual(f(a=1, b=2), [('a', 1), ('b', 2)])
+ self.assertEqual(f(b=2, a=1), [('b', 2), ('a', 1)])
+ self.assertEqual(f.cache_info(),
+ self.module._CacheInfo(hits=0, misses=2, maxsize=10, currsize=2))
+
def test_lru_cache_decoration(self):
def f(zomg: 'zomg_annotation'):
"""f doc string"""