|
| 1 | +import unittest |
| 2 | + |
| 3 | +class TypeAnnotationTests(unittest.TestCase): |
| 4 | + |
| 5 | + def test_lazy_create_annotations(self): |
| 6 | + # type objects lazy create their __annotations__ dict on demand. |
| 7 | + # the annotations dict is stored in type.__dict__. |
| 8 | + # a freshly created type shouldn't have an annotations dict yet. |
| 9 | + foo = type("Foo", (), {}) |
| 10 | + for i in range(3): |
| 11 | + self.assertFalse("__annotations__" in foo.__dict__) |
| 12 | + d = foo.__annotations__ |
| 13 | + self.assertTrue("__annotations__" in foo.__dict__) |
| 14 | + self.assertEqual(foo.__annotations__, d) |
| 15 | + self.assertEqual(foo.__dict__['__annotations__'], d) |
| 16 | + del foo.__annotations__ |
| 17 | + |
| 18 | + def test_setting_annotations(self): |
| 19 | + foo = type("Foo", (), {}) |
| 20 | + for i in range(3): |
| 21 | + self.assertFalse("__annotations__" in foo.__dict__) |
| 22 | + d = {'a': int} |
| 23 | + foo.__annotations__ = d |
| 24 | + self.assertTrue("__annotations__" in foo.__dict__) |
| 25 | + self.assertEqual(foo.__annotations__, d) |
| 26 | + self.assertEqual(foo.__dict__['__annotations__'], d) |
| 27 | + del foo.__annotations__ |
| 28 | + |
| 29 | + def test_annotations_getset_raises(self): |
| 30 | + # builtin types don't have __annotations__ (yet!) |
| 31 | + with self.assertRaises(AttributeError): |
| 32 | + print(float.__annotations__) |
| 33 | + with self.assertRaises(TypeError): |
| 34 | + float.__annotations__ = {} |
| 35 | + with self.assertRaises(TypeError): |
| 36 | + del float.__annotations__ |
| 37 | + |
| 38 | + # double delete |
| 39 | + foo = type("Foo", (), {}) |
| 40 | + foo.__annotations__ = {} |
| 41 | + del foo.__annotations__ |
| 42 | + with self.assertRaises(AttributeError): |
| 43 | + del foo.__annotations__ |
| 44 | + |
| 45 | + def test_annotations_are_created_correctly(self): |
| 46 | + class C: |
| 47 | + a:int=3 |
| 48 | + b:str=4 |
| 49 | + self.assertTrue("__annotations__" in C.__dict__) |
| 50 | + del C.__annotations__ |
| 51 | + self.assertFalse("__annotations__" in C.__dict__) |
| 52 | + |
| 53 | + def test_descriptor_still_works(self): |
| 54 | + class C: |
| 55 | + def __init__(self, name=None, bases=None, d=None): |
| 56 | + self.my_annotations = None |
| 57 | + |
| 58 | + @property |
| 59 | + def __annotations__(self): |
| 60 | + if not hasattr(self, 'my_annotations'): |
| 61 | + self.my_annotations = {} |
| 62 | + if not isinstance(self.my_annotations, dict): |
| 63 | + self.my_annotations = {} |
| 64 | + return self.my_annotations |
| 65 | + |
| 66 | + @__annotations__.setter |
| 67 | + def __annotations__(self, value): |
| 68 | + if not isinstance(value, dict): |
| 69 | + raise ValueError("can only set __annotations__ to a dict") |
| 70 | + self.my_annotations = value |
| 71 | + |
| 72 | + @__annotations__.deleter |
| 73 | + def __annotations__(self): |
| 74 | + if hasattr(self, 'my_annotations') and self.my_annotations == None: |
| 75 | + raise AttributeError('__annotations__') |
| 76 | + self.my_annotations = None |
| 77 | + |
| 78 | + c = C() |
| 79 | + self.assertEqual(c.__annotations__, {}) |
| 80 | + d = {'a':'int'} |
| 81 | + c.__annotations__ = d |
| 82 | + self.assertEqual(c.__annotations__, d) |
| 83 | + with self.assertRaises(ValueError): |
| 84 | + c.__annotations__ = 123 |
| 85 | + del c.__annotations__ |
| 86 | + with self.assertRaises(AttributeError): |
| 87 | + del c.__annotations__ |
| 88 | + self.assertEqual(c.__annotations__, {}) |
| 89 | + |
| 90 | + |
| 91 | + class D(metaclass=C): |
| 92 | + pass |
| 93 | + |
| 94 | + self.assertEqual(D.__annotations__, {}) |
| 95 | + d = {'a':'int'} |
| 96 | + D.__annotations__ = d |
| 97 | + self.assertEqual(D.__annotations__, d) |
| 98 | + with self.assertRaises(ValueError): |
| 99 | + D.__annotations__ = 123 |
| 100 | + del D.__annotations__ |
| 101 | + with self.assertRaises(AttributeError): |
| 102 | + del D.__annotations__ |
| 103 | + self.assertEqual(D.__annotations__, {}) |
0 commit comments