[Python-checkins] bpo-45679: add `tuple` tests with `lru_cache` to `test_functools` (GH-29339)

rhettinger webhook-mailer at python.org
Fri Nov 5 15:52:55 EDT 2021


https://github.com/python/cpython/commit/60b5333fa936a7e7f078a82e0fa3752cc9b6c5fb
commit: 60b5333fa936a7e7f078a82e0fa3752cc9b6c5fb
branch: main
author: Nikita Sobolev <mail at sobolevn.me>
committer: rhettinger <rhettinger at users.noreply.github.com>
date: 2021-11-05T14:52:46-05:00
summary:

bpo-45679: add `tuple` tests with `lru_cache` to `test_functools` (GH-29339)

files:
A Misc/NEWS.d/next/Tests/2021-10-31-10-58-45.bpo-45701.r0LAUL.rst
M Lib/test/test_functools.py

diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index 7bc355ff7213e..08cf457cc17db 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -1495,6 +1495,33 @@ def square(x):
             self.assertEqual(square.cache_info().hits, 4)
             self.assertEqual(square.cache_info().misses, 4)
 
+    def test_lru_cache_typed_is_not_recursive(self):
+        cached = self.module.lru_cache(typed=True)(repr)
+
+        self.assertEqual(cached(1), '1')
+        self.assertEqual(cached(True), 'True')
+        self.assertEqual(cached(1.0), '1.0')
+        self.assertEqual(cached(0), '0')
+        self.assertEqual(cached(False), 'False')
+        self.assertEqual(cached(0.0), '0.0')
+
+        self.assertEqual(cached((1,)), '(1,)')
+        self.assertEqual(cached((True,)), '(1,)')
+        self.assertEqual(cached((1.0,)), '(1,)')
+        self.assertEqual(cached((0,)), '(0,)')
+        self.assertEqual(cached((False,)), '(0,)')
+        self.assertEqual(cached((0.0,)), '(0,)')
+
+        class T(tuple):
+            pass
+
+        self.assertEqual(cached(T((1,))), '(1,)')
+        self.assertEqual(cached(T((True,))), '(1,)')
+        self.assertEqual(cached(T((1.0,))), '(1,)')
+        self.assertEqual(cached(T((0,))), '(0,)')
+        self.assertEqual(cached(T((False,))), '(0,)')
+        self.assertEqual(cached(T((0.0,))), '(0,)')
+
     def test_lru_with_keyword_args(self):
         @self.module.lru_cache()
         def fib(n):
diff --git a/Misc/NEWS.d/next/Tests/2021-10-31-10-58-45.bpo-45701.r0LAUL.rst b/Misc/NEWS.d/next/Tests/2021-10-31-10-58-45.bpo-45701.r0LAUL.rst
new file mode 100644
index 0000000000000..89106cf0660f0
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2021-10-31-10-58-45.bpo-45701.r0LAUL.rst
@@ -0,0 +1,2 @@
+Add tests with ``tuple`` type with :func:`functools.lru_cache` to
+``test_functools``.



More information about the Python-checkins mailing list