Skip to content

Commit 2e4b927

Browse files
committed
Test functools wrapped as annotate function
1 parent e6bc7a0 commit 2e4b927

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

Lib/test/test_annotationlib.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import itertools
99
import pickle
1010
from string.templatelib import Template, Interpolation
11+
import random
1112
import typing
1213
import sys
1314
import unittest
@@ -1648,7 +1649,6 @@ def format(format, second, /, *, third, __Format=Format,
16481649
self.assertEqual(annotations, {"x": Format.VALUE * 5 * 6})
16491650

16501651
def test_callable_cache_annotate_forwardref_value_fallback(self):
1651-
import random
16521652
# If Format.STRING and Format.VALUE_WITH_FAKE_GLOBALS are not
16531653
# supported fall back to Format.VALUE and convert to strings
16541654
@functools.cache
@@ -1672,6 +1672,28 @@ def format(format, /, __Format=Format,
16721672
new_anns = annotationlib.call_annotate_function(format, Format.FORWARDREF)
16731673
self.assertEqual(annotations, new_anns)
16741674

1675+
def test_callable_wrapped_annotate_forwardref_value_fallback(self):
1676+
# If Format.STRING and Format.VALUE_WITH_FAKE_GLOBALS are not
1677+
# supported fall back to Format.VALUE and convert to strings
1678+
def multiple_format(fn):
1679+
inputs = {"x": int}
1680+
@functools.wraps(fn)
1681+
def format(format, /, __Format=Format,
1682+
__NotImplementedError=NotImplementedError):
1683+
if format == __Format.VALUE:
1684+
return {**inputs, **fn()}
1685+
else:
1686+
raise __NotImplementedError(format)
1687+
1688+
return format
1689+
1690+
annotations = annotationlib.call_annotate_function(
1691+
multiple_format(lambda: {"y": str}),
1692+
Format.FORWARDREF,
1693+
)
1694+
1695+
self.assertEqual(annotations, {"x": int, "y": str})
1696+
16751697
def test_callable_object_annotate_string_fakeglobals(self):
16761698
# If Format.STRING is not supported but Format.VALUE_WITH_FAKE_GLOBALS is
16771699
# prefer that over Format.VALUE

0 commit comments

Comments
 (0)