|
10 | 10 | from string.templatelib import Template |
11 | 11 | import typing |
12 | 12 | import unittest |
| 13 | +import unittest.mock |
13 | 14 | from annotationlib import ( |
14 | 15 | Format, |
15 | 16 | ForwardRef, |
@@ -1206,6 +1207,74 @@ def evaluate(format, exc=NotImplementedError): |
1206 | 1207 | ) |
1207 | 1208 |
|
1208 | 1209 |
|
| 1210 | +class TestCallAnnotateFunction(unittest.TestCase): |
| 1211 | + def _annotate_mock(self): |
| 1212 | + def annotate(format, /): |
| 1213 | + if format == Format.VALUE: |
| 1214 | + return {"x": str} |
| 1215 | + else: |
| 1216 | + raise NotImplementedError(format) |
| 1217 | + |
| 1218 | + annotate_mock = unittest.mock.MagicMock( |
| 1219 | + wraps=annotate |
| 1220 | + ) |
| 1221 | + |
| 1222 | + # Add missing magic attributes needed |
| 1223 | + required_magic = [ |
| 1224 | + "__builtins__", |
| 1225 | + "__closure__", |
| 1226 | + "__code__", |
| 1227 | + "__defaults__", |
| 1228 | + "__globals__", |
| 1229 | + "__kwdefaults__", |
| 1230 | + ] |
| 1231 | + |
| 1232 | + for attrib in required_magic: |
| 1233 | + setattr(annotate_mock, attrib, getattr(annotate, attrib)) |
| 1234 | + |
| 1235 | + return annotate_mock |
| 1236 | + |
| 1237 | + def test_user_annotate_value(self): |
| 1238 | + annotate = self._annotate_mock() |
| 1239 | + |
| 1240 | + annotations = annotationlib.call_annotate_function( |
| 1241 | + annotate, |
| 1242 | + Format.VALUE, |
| 1243 | + ) |
| 1244 | + |
| 1245 | + self.assertEqual(annotations, {"x": str}) |
| 1246 | + annotate.assert_called_once_with(Format.VALUE) |
| 1247 | + |
| 1248 | + def test_user_annotate_forwardref(self): |
| 1249 | + annotate = self._annotate_mock() |
| 1250 | + |
| 1251 | + with self.assertRaises(NotImplementedError): |
| 1252 | + annotations = annotationlib.call_annotate_function( |
| 1253 | + annotate, |
| 1254 | + Format.FORWARDREF, |
| 1255 | + ) |
| 1256 | + |
| 1257 | + annotate.assert_has_calls([ |
| 1258 | + unittest.mock.Call(Format.FORWARDREF), |
| 1259 | + unittest.mock.Call(Format.VALUE_WITH_FAKE_GLOBALS), |
| 1260 | + ]) |
| 1261 | + |
| 1262 | + def test_user_annotate_string(self): |
| 1263 | + annotate = self._annotate_mock() |
| 1264 | + |
| 1265 | + with self.assertRaises(NotImplementedError): |
| 1266 | + annotations = annotationlib.call_annotate_function( |
| 1267 | + annotate, |
| 1268 | + Format.STRING, |
| 1269 | + ) |
| 1270 | + |
| 1271 | + annotate.assert_has_calls([ |
| 1272 | + unittest.mock.Call(Format.STRING), |
| 1273 | + unittest.mock.Call(Format.VALUE_WITH_FAKE_GLOBALS), |
| 1274 | + ]) |
| 1275 | + |
| 1276 | + |
| 1277 | + |
1209 | 1278 | class MetaclassTests(unittest.TestCase): |
1210 | 1279 | def test_annotated_meta(self): |
1211 | 1280 | class Meta(type): |
|
0 commit comments