]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-109818: `reprlib.recursive_repr` copies `__type_params__` (#109819)
authorNikita Sobolev <mail@sobolevn.me>
Thu, 28 Sep 2023 02:26:42 +0000 (05:26 +0300)
committerGitHub <noreply@github.com>
Thu, 28 Sep 2023 02:26:42 +0000 (19:26 -0700)
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 840dd0e20132b120a87f452e2108c447f018a3d7..05bb1a0eb017951d30c106ac674f56ca631aadcd 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__', ())
         wrapper.__wrapped__ = user_function
         return wrapper
 
index 502287b620d0662bc7d047f5dc20ffa20083c8b0..3e93b561c143d8db3a4eda6db7961a3b77d433b3 100644 (file)
@@ -774,5 +774,16 @@ class TestRecursiveRepr(unittest.TestCase):
 
         self.assertIs(X.f, X.__repr__.__wrapped__)
 
+    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.