]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-132493: Avoid eager evaluation of annotations in `@reprlib.recursive_repr()` ...
authorJelle Zijlstra <jelle.zijlstra@gmail.com>
Tue, 6 May 2025 02:20:11 +0000 (19:20 -0700)
committerGitHub <noreply@github.com>
Tue, 6 May 2025 02:20:11 +0000 (19:20 -0700)
Lib/reprlib.py
Lib/test/test_reprlib.py
Misc/NEWS.d/next/Library/2025-05-04-16-37-28.gh-issue-132493.5yjZ75.rst [new file with mode: 0644]

index 19dbe3a07eb618d22e33b8a2626b6241be75ed5e..441d1be4bdede2c826749228c329318c7f0e66ad 100644 (file)
@@ -28,7 +28,7 @@ def recursive_repr(fillvalue='...'):
         wrapper.__doc__ = getattr(user_function, '__doc__')
         wrapper.__name__ = getattr(user_function, '__name__')
         wrapper.__qualname__ = getattr(user_function, '__qualname__')
-        wrapper.__annotations__ = getattr(user_function, '__annotations__', {})
+        wrapper.__annotate__ = getattr(user_function, '__annotate__', None)
         wrapper.__type_params__ = getattr(user_function, '__type_params__', ())
         wrapper.__wrapped__ = user_function
         return wrapper
index ffeb1fba7b80c61094a321a3a70f4bdcaf425525..ffad35092f9916a5b1bae1bad7d7f8fb55e8ceee 100644 (file)
@@ -3,6 +3,7 @@
   Nick Mathewson
 """
 
+import annotationlib
 import sys
 import os
 import shutil
@@ -11,7 +12,7 @@ import importlib.util
 import unittest
 import textwrap
 
-from test.support import verbose
+from test.support import verbose, EqualToForwardRef
 from test.support.os_helper import create_empty_file
 from reprlib import repr as r # Don't shadow builtin repr
 from reprlib import Repr
@@ -829,5 +830,19 @@ class TestRecursiveRepr(unittest.TestCase):
         self.assertEqual(type_params[0].__name__, 'T')
         self.assertEqual(type_params[0].__bound__, str)
 
+    def test_annotations(self):
+        class My:
+            @recursive_repr()
+            def __repr__(self, default: undefined = ...):
+                return default
+
+        annotations = annotationlib.get_annotations(
+            My.__repr__, format=annotationlib.Format.FORWARDREF
+        )
+        self.assertEqual(
+            annotations,
+            {'default': EqualToForwardRef("undefined", owner=My.__repr__)}
+        )
+
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2025-05-04-16-37-28.gh-issue-132493.5yjZ75.rst b/Misc/NEWS.d/next/Library/2025-05-04-16-37-28.gh-issue-132493.5yjZ75.rst
new file mode 100644 (file)
index 0000000..8d0b483
--- /dev/null
@@ -0,0 +1,2 @@
+Avoid eagerly evaluating annotations in functions decorated with
+:func:`reprlib.recursive_repr`.