Skip to content

Commit 1bc700a

Browse files
committed
Test the call to the function with fake globals for the FORWARDREF format.
1 parent c19bcbd commit 1bc700a

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

Lib/test/test_annotationlib.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import itertools
99
import pickle
1010
from string.templatelib import Template
11+
import types
1112
import typing
1213
import unittest
1314
import unittest.mock
@@ -1248,16 +1249,28 @@ def test_user_annotate_value(self):
12481249
def test_user_annotate_forwardref(self):
12491250
annotate = self._annotate_mock()
12501251

1251-
with self.assertRaises(NotImplementedError):
1252-
annotations = annotationlib.call_annotate_function(
1253-
annotate,
1254-
Format.FORWARDREF,
1255-
)
1252+
new_annotate = None
1253+
functype = types.FunctionType
12561254

1257-
# The annotate function itself is not called the second time
1258-
# A new function built from the code is called instead
1255+
def functiontype_mock(*args, **kwargs):
1256+
nonlocal new_annotate
1257+
new_func = unittest.mock.MagicMock(wraps=functype(*args, **kwargs))
1258+
new_annotate = new_func
1259+
return new_func
1260+
1261+
with unittest.mock.patch("types.FunctionType", new=functiontype_mock):
1262+
with self.assertRaises(NotImplementedError):
1263+
annotations = annotationlib.call_annotate_function(
1264+
annotate,
1265+
Format.FORWARDREF,
1266+
)
1267+
1268+
# Test the direct call
12591269
annotate.assert_called_once_with(Format.FORWARDREF)
12601270

1271+
# Test the call on the function with fake globals
1272+
new_annotate.assert_called_once_with(Format.VALUE_WITH_FAKE_GLOBALS)
1273+
12611274
def test_user_annotate_string(self):
12621275
annotate = self._annotate_mock()
12631276

0 commit comments

Comments
 (0)