File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 88import itertools
99import pickle
1010from string .templatelib import Template
11+ import types
1112import typing
1213import unittest
1314import 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
You can’t perform that action at this time.
0 commit comments