|
| 1 | +import sys |
| 2 | +import pickle |
| 3 | +import typing |
| 4 | +try: |
| 5 | + import collections.abc as collections_abc |
| 6 | +except ImportError: |
| 7 | + import collections as collections_abc # type: ignore # PY32 and earlier |
| 8 | +from unittest import TestCase, main, skipUnless |
| 9 | +sys.path[0:0] = ['extensions'] |
| 10 | +from mypy_extensions import TypedDict |
| 11 | + |
| 12 | + |
| 13 | +class BaseTestCase(TestCase): |
| 14 | + |
| 15 | + def assertIsSubclass(self, cls, class_or_tuple, msg=None): |
| 16 | + if not issubclass(cls, class_or_tuple): |
| 17 | + message = '%r is not a subclass of %r' % (cls, class_or_tuple) |
| 18 | + if msg is not None: |
| 19 | + message += ' : %s' % msg |
| 20 | + raise self.failureException(message) |
| 21 | + |
| 22 | + def assertNotIsSubclass(self, cls, class_or_tuple, msg=None): |
| 23 | + if issubclass(cls, class_or_tuple): |
| 24 | + message = '%r is a subclass of %r' % (cls, class_or_tuple) |
| 25 | + if msg is not None: |
| 26 | + message += ' : %s' % msg |
| 27 | + raise self.failureException(message) |
| 28 | + |
| 29 | + |
| 30 | +PY36 = sys.version_info[:2] >= (3, 6) |
| 31 | + |
| 32 | +PY36_TESTS = """ |
| 33 | +Label = TypedDict('Label', [('label', str)]) |
| 34 | +
|
| 35 | +class Point2D(TypedDict): |
| 36 | + x: int |
| 37 | + y: int |
| 38 | +
|
| 39 | +class LabelPoint2D(Point2D, Label): ... |
| 40 | +""" |
| 41 | + |
| 42 | +if PY36: |
| 43 | + exec(PY36_TESTS) |
| 44 | + |
| 45 | + |
| 46 | +class TypedDictTests(BaseTestCase): |
| 47 | + |
| 48 | + def test_basics_iterable_syntax(self): |
| 49 | + Emp = TypedDict('Emp', {'name': str, 'id': int}) |
| 50 | + self.assertIsSubclass(Emp, dict) |
| 51 | + self.assertIsSubclass(Emp, typing.MutableMapping) |
| 52 | + self.assertNotIsSubclass(Emp, collections_abc.Sequence) |
| 53 | + jim = Emp(name='Jim', id=1) |
| 54 | + self.assertIs(type(jim), dict) |
| 55 | + self.assertEqual(jim['name'], 'Jim') |
| 56 | + self.assertEqual(jim['id'], 1) |
| 57 | + self.assertEqual(Emp.__name__, 'Emp') |
| 58 | + self.assertEqual(Emp.__module__, 'mypy.test.testextensions') |
| 59 | + self.assertEqual(Emp.__bases__, (dict,)) |
| 60 | + self.assertEqual(Emp.__annotations__, {'name': str, 'id': int}) |
| 61 | + |
| 62 | + def test_basics_keywords_syntax(self): |
| 63 | + Emp = TypedDict('Emp', name=str, id=int) |
| 64 | + self.assertIsSubclass(Emp, dict) |
| 65 | + self.assertIsSubclass(Emp, typing.MutableMapping) |
| 66 | + self.assertNotIsSubclass(Emp, collections_abc.Sequence) |
| 67 | + jim = Emp(name='Jim', id=1) # type: ignore # mypy doesn't support keyword syntax yet |
| 68 | + self.assertIs(type(jim), dict) |
| 69 | + self.assertEqual(jim['name'], 'Jim') |
| 70 | + self.assertEqual(jim['id'], 1) |
| 71 | + self.assertEqual(Emp.__name__, 'Emp') |
| 72 | + self.assertEqual(Emp.__module__, 'mypy.test.testextensions') |
| 73 | + self.assertEqual(Emp.__bases__, (dict,)) |
| 74 | + self.assertEqual(Emp.__annotations__, {'name': str, 'id': int}) |
| 75 | + |
| 76 | + def test_typeddict_errors(self): |
| 77 | + Emp = TypedDict('Emp', {'name': str, 'id': int}) |
| 78 | + self.assertEqual(TypedDict.__module__, 'mypy_extensions') |
| 79 | + jim = Emp(name='Jim', id=1) |
| 80 | + with self.assertRaises(TypeError): |
| 81 | + isinstance({}, Emp) |
| 82 | + with self.assertRaises(TypeError): |
| 83 | + isinstance(jim, Emp) |
| 84 | + with self.assertRaises(TypeError): |
| 85 | + issubclass(dict, Emp) |
| 86 | + with self.assertRaises(TypeError): |
| 87 | + TypedDict('Hi', x=1) |
| 88 | + with self.assertRaises(TypeError): |
| 89 | + TypedDict('Hi', [('x', int), ('y', 1)]) |
| 90 | + with self.assertRaises(TypeError): |
| 91 | + TypedDict('Hi', [('x', int)], y=int) |
| 92 | + |
| 93 | + @skipUnless(PY36, 'Python 3.6 required') |
| 94 | + def test_py36_class_syntax_usage(self): |
| 95 | + self.assertEqual(LabelPoint2D.__annotations__, {'x': int, 'y': int, 'label': str}) # noqa |
| 96 | + self.assertEqual(LabelPoint2D.__bases__, (dict,)) # noqa |
| 97 | + self.assertNotIsSubclass(LabelPoint2D, typing.Sequence) # noqa |
| 98 | + not_origin = Point2D(x=0, y=1) # noqa |
| 99 | + self.assertEqual(not_origin['x'], 0) |
| 100 | + self.assertEqual(not_origin['y'], 1) |
| 101 | + other = LabelPoint2D(x=0, y=1, label='hi') # noqa |
| 102 | + self.assertEqual(other['label'], 'hi') |
| 103 | + |
| 104 | + def test_pickle(self): |
| 105 | + global EmpD # pickle wants to reference the class by name |
| 106 | + EmpD = TypedDict('EmpD', name=str, id=int) |
| 107 | + jane = EmpD({'name': 'jane', 'id': 37}) |
| 108 | + for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 109 | + z = pickle.dumps(jane, proto) |
| 110 | + jane2 = pickle.loads(z) |
| 111 | + self.assertEqual(jane2, jane) |
| 112 | + self.assertEqual(jane2, {'name': 'jane', 'id': 37}) |
| 113 | + ZZ = pickle.dumps(EmpD, proto) |
| 114 | + EmpDnew = pickle.loads(ZZ) |
| 115 | + self.assertEqual(EmpDnew({'name': 'jane', 'id': 37}), jane) |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == '__main__': |
| 119 | + main() |
0 commit comments