Skip to content

Commit 65c40d3

Browse files
committed
Add test to check if arguments to ref.evaluate() are mutated
1 parent e20da27 commit 65c40d3

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Lib/test/test_annotationlib.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,6 +1946,31 @@ class C:
19461946
global_alias = list
19471947
self.assertEqual(evaluated.evaluate(), list[int])
19481948

1949+
def test_fwdref_evaluate_argument_mutation(self):
1950+
class C[T]:
1951+
nonlocal alias
1952+
x: alias[T]
1953+
1954+
# Mutable arguments
1955+
globals_ = globals()
1956+
globals_copy = globals_.copy()
1957+
locals_ = locals()
1958+
locals_copy = locals_.copy()
1959+
1960+
# Evaluate the ForwardRef, ensuring we use __cell__ and type params
1961+
get_annotations(C, format=Format.FORWARDREF)["x"].evaluate(
1962+
globals=globals_,
1963+
locals=locals_,
1964+
type_params=C.__type_params__,
1965+
format=Format.FORWARDREF,
1966+
)
1967+
1968+
# Check if the passed in mutable arguments equal the originals
1969+
self.assertEqual(globals_, globals_copy)
1970+
self.assertEqual(locals_, locals_copy)
1971+
1972+
alias = list
1973+
19491974

19501975
class TestAnnotationLib(unittest.TestCase):
19511976
def test__all__(self):

0 commit comments

Comments
 (0)