8000 gh-132493: Avoid eager evaluation of annotations in `@reprlib.recursi… · python/cpython@dbee142 · GitHub
[go: up one dir, main page]

Skip to content

Commit dbee142

Browse files
gh-132493: Avoid eager evaluation of annotations in @reprlib.recursive_repr() (#133411)
1 parent b936ccd commit dbee142

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

Lib/reprlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def wrapper(self):
2828
wrapper.__doc__ = getattr(user_function, '__doc__')
2929
wrapper.__name__ = getattr(user_function, '__name__')
3030
wrapper.__qualname__ = getattr(user_function, '__qualname__')
31-
wrapper.__annotations__ = getattr(user_function, '__annotations__', {})
31+
wrapper.__annotate__ = getattr(user_function, '__annotate__', None)
3232
wrapper.__type_params__ = getattr(user_function, '__type_params__', ())
3333
wrapper.__wrapped__ = user_function
3434
return wrapper

Lib/test/test_reprlib.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Nick Mathewson
44
"""
55

6+
import annotationlib
67
import sys
78
import os
89
import shutil
@@ -11,7 +12,7 @@
1112
import unittest
1213
import textwrap
1314

14-
from test.support import verbose
15+
from test.support import verbose, EqualToForwardRef
1516
from test.support.os_helper import create_empty_file
1617
from reprlib import repr as r # Don't shadow builtin repr
1718
from reprlib import Repr
@@ -829,5 +830,19 @@ def __repr__[T: str](self, default: T = '') -> str:
829830
self.assertEqual(type_params[0].__name__, 'T')
830831
self.assertEqual(type_params[0].__bound__, str)
831832

833+
def test_annotations(self):
834+
class My:
835+
@recursive_repr()
836+
def __repr__(self, default: undefined = ...):
837+
return default
838+
839+
annotations = annotationlib.get_annotations(
840+
My.__repr__, format=annotationlib.Format.FORWARDREF
841+
)
842+
self.assertEqual(
843+
annotations,
844+
{'default': EqualToForwardRef("undefined", owner=My.__repr__)}
845+
)
846+
832847
if __name__ == "__main__":
833848
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Avoid eagerly evaluating annotations in functions decorated with
2+
:func:`reprlib.recursive_repr`.

0 commit comments

Comments
 (0)
0