diff options
author | larryhastings <larry@hastings.org> | 2021-04-30 03:09:08 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-30 03:09:08 (GMT) |
commit | 2f2b69855d6524e15d12c15ddc0adce629e7de84 (patch) | |
tree | aac6542c35708f45670fc5ae027a9fc1802e8498 /Lib/test/test_module.py | |
parent | dbe60ee09dc5a624cfb78dff61ecf050a5b3f105 (diff) | |
download | cpython-2f2b69855d6524e15d12c15ddc0adce629e7de84.zip cpython-2f2b69855d6524e15d12c15ddc0adce629e7de84.tar.gz cpython-2f2b69855d6524e15d12c15ddc0adce629e7de84.tar.bz2 |
bpo-43901: Lazy-create an empty annotations dict in all unannotated user classes and modules (#25623)
Change class and module objects to lazy-create empty annotations dicts on demand. The annotations dicts are stored in the object's `__dict__` for backwards compatibility.
Diffstat (limited to 'Lib/test/test_module.py')
-rw-r--r-- | Lib/test/test_module.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/Lib/test/test_module.py b/Lib/test/test_module.py index 1d44563..aa5ee49 100644 --- a/Lib/test/test_module.py +++ b/Lib/test/test_module.py @@ -286,6 +286,60 @@ a = A(destroyed)""" melon = Descr() self.assertRaises(RuntimeError, getattr, M("mymod"), "melon") + def test_lazy_create_annotations(self): + # module objects lazy create their __annotations__ dict on demand. + # the annotations dict is stored in module.__dict__. + # a freshly created module shouldn't have an annotations dict yet. + foo = ModuleType("foo") + for i in range(4): + self.assertFalse("__annotations__" in foo.__dict__) + d = foo.__annotations__ + self.assertTrue("__annotations__" in foo.__dict__) + self.assertEqual(foo.__annotations__, d) + self.assertEqual(foo.__dict__['__annotations__'], d) + if i % 2: + del foo.__annotations__ + else: + del foo.__dict__['__annotations__'] + + def test_setting_annotations(self): + foo = ModuleType("foo") + for i in range(4): + self.assertFalse("__annotations__" in foo.__dict__) + d = {'a': int} + foo.__annotations__ = d + self.assertTrue("__annotations__" in foo.__dict__) + self.assertEqual(foo.__annotations__, d) + self.assertEqual(foo.__dict__['__annotations__'], d) + if i % 2: + del foo.__annotations__ + else: + del foo.__dict__['__annotations__'] + + def test_annotations_getset_raises(self): + # module has no dict, all operations fail + foo = ModuleType.__new__(ModuleType) + with self.assertRaises(TypeError): + print(foo.__annotations__) + with self.assertRaises(TypeError): + foo.__annotations__ = {} + with self.assertRaises(TypeError): + del foo.__annotations__ + + # double delete + foo = ModuleType("foo") + foo.__annotations__ = {} + del foo.__annotations__ + with self.assertRaises(AttributeError): + del foo.__annotations__ + + def test_annotations_are_created_correctly(self): + from test import ann_module4 + self.assertTrue("__annotations__" in ann_module4.__dict__) + del ann_module4.__annotations__ + self.assertFalse("__annotations__" in ann_module4.__dict__) + + # frozen and namespace module reprs are tested in importlib. |