Skip to content

Commit ed99f4c

Browse files
committed
Add tests for calling a custom annotate function that only implements VALUE annotations
1 parent d0c9943 commit ed99f4c

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

Lib/test/test_annotationlib.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from string.templatelib import Template
1111
import typing
1212
import unittest
13+
import unittest.mock
1314
from annotationlib import (
1415
Format,
1516
ForwardRef,
@@ -1206,6 +1207,74 @@ def evaluate(format, exc=NotImplementedError):
12061207
)
12071208

12081209

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+
12091278
class MetaclassTests(unittest.TestCase):
12101279
def test_annotated_meta(self):
12111280
class Meta(type):

0 commit comments

Comments
 (0)