|
8 | 8 | import itertools |
9 | 9 | import pickle |
10 | 10 | from string.templatelib import Template |
| 11 | +import types |
11 | 12 | import typing |
12 | 13 | import unittest |
13 | 14 | import unittest.mock |
@@ -1248,30 +1249,49 @@ def test_user_annotate_value(self): |
1248 | 1249 | def test_user_annotate_forwardref(self): |
1249 | 1250 | annotate = self._annotate_mock() |
1250 | 1251 |
|
1251 | | - with self.assertRaises(NotImplementedError): |
| 1252 | + new_annotate = None |
| 1253 | + functype = types.FunctionType |
| 1254 | + |
| 1255 | + def functiontype(*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): |
1252 | 1262 | annotations = annotationlib.call_annotate_function( |
1253 | 1263 | annotate, |
1254 | 1264 | Format.FORWARDREF, |
1255 | 1265 | ) |
1256 | 1266 |
|
1257 | | - # The annotate function itself is not called the second time |
1258 | | - # A new function built from the code is called instead |
1259 | | - annotate.assert_called_once_with(Format.FORWARDREF) |
| 1267 | + # The call with Format.VALUE_WITH_FAKE_GLOBALS is not |
| 1268 | + # on the original function. |
| 1269 | + annotate.assert_has_calls([ |
| 1270 | + unittest.mock.call(Format.FORWARDREF), |
| 1271 | + unittest.mock.call(Format.VALUE), |
| 1272 | + ]) |
| 1273 | + |
| 1274 | + new_annotate.assert_called_once_with(Format.VALUE_WITH_FAKE_GLOBALS) |
| 1275 | + |
| 1276 | + self.assertEqual(annotations, {"x": str}) |
| 1277 | + |
1260 | 1278 |
|
1261 | 1279 | def test_user_annotate_string(self): |
1262 | 1280 | annotate = self._annotate_mock() |
1263 | 1281 |
|
1264 | | - with self.assertRaises(NotImplementedError): |
1265 | | - annotations = annotationlib.call_annotate_function( |
1266 | | - annotate, |
1267 | | - Format.STRING, |
1268 | | - ) |
| 1282 | + annotations = annotationlib.call_annotate_function( |
| 1283 | + annotate, |
| 1284 | + Format.STRING, |
| 1285 | + ) |
1269 | 1286 |
|
1270 | 1287 | annotate.assert_has_calls([ |
1271 | 1288 | unittest.mock.call(Format.STRING), |
1272 | 1289 | unittest.mock.call(Format.VALUE_WITH_FAKE_GLOBALS), |
| 1290 | + unittest.mock.call(Format.VALUE), |
1273 | 1291 | ]) |
1274 | 1292 |
|
| 1293 | + self.assertEqual(annotations, {"x": "str"}) |
| 1294 | + |
1275 | 1295 |
|
1276 | 1296 | class MetaclassTests(unittest.TestCase): |
1277 | 1297 | def test_annotated_meta(self): |
|
0 commit comments