]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-109818: `reprlib.recursive_repr` copies `__type_params__` (… (#109999)
authorJelle Zijlstra <jelle.zijlstra@gmail.com>
Mon, 2 Oct 2023 15:41:26 +0000 (08:41 -0700)
committerGitHub <noreply@github.com>
Mon, 2 Oct 2023 15:41:26 +0000 (17:41 +0200)
[3.12] gh-109818: `reprlib.recursive_repr` copies `__type_params__` (GH-109819).
(cherry picked from commit f65f9e80fe741c894582a3e413d4e3318c1ed626)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Lib/reprlib.py
Lib/test/test_reprlib.py
Misc/NEWS.d/next/Library/2023-09-25-09-59-59.gh-issue-109818.dLRtT-.rst [new file with mode: 0644]

index a92b3e3dbb613a6b9baee8a8a7d0b6e3ecac3e96..a7b37630a4edb98c7482c1995b219611130c8c01 100644 (file)
@@ -29,6 +29,7 @@ def recursive_repr(fillvalue='...'):
         wrapper.__name__ = getattr(user_function, '__name__')
         wrapper.__qualname__ = getattr(user_function, '__qualname__')
         wrapper.__annotations__ = getattr(user_function, '__annotations__', {})
+        wrapper.__type_params__ = getattr(user_function, '__type_params__', ())
         return wrapper
 
     return decorating_function
index e7216d427200c1500e5b3a2af19bf1c21b73c4dd..4a896db2002047b8d839810518126db5c5dffa45 100644 (file)
@@ -765,5 +765,16 @@ class TestRecursiveRepr(unittest.TestCase):
         for name in assigned:
             self.assertIs(getattr(wrapper, name), getattr(wrapped, name))
 
+    def test__type_params__(self):
+        class My:
+            @recursive_repr()
+            def __repr__[T: str](self, default: T = '') -> str:
+                return default
+
+        type_params = My().__repr__.__type_params__
+        self.assertEqual(len(type_params), 1)
+        self.assertEqual(type_params[0].__name__, 'T')
+        self.assertEqual(type_params[0].__bound__, str)
+
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2023-09-25-09-59-59.gh-issue-109818.dLRtT-.rst b/Misc/NEWS.d/next/Library/2023-09-25-09-59-59.gh-issue-109818.dLRtT-.rst
new file mode 100644 (file)
index 0000000..184086a
--- /dev/null
@@ -0,0 +1,2 @@
+Fix :func:`reprlib.recursive_repr` not copying ``__type_params__`` from
+decorated function.