Update test_module from CPython 3.10.5
# Test the module type
import unittest
import weakref
-from test.support import gc_collect, requires_type_collecting
+from test.support import gc_collect
+from test.support import import_helper
from test.support.script_helper import assert_python_ok
import sys
@@ -24,7 +25,7 @@ class ModuleTests(unittest.TestCase):
# and __doc__ is None
foo = ModuleType.__new__(ModuleType)
self.assertTrue(foo.__dict__ is None)
- self.assertRaises(SystemError, dir, foo)
+ self.assertRaises(TypeError, dir, foo)
try:
s = foo.__name__
self.fail("__name__ = %s" % repr(s))
@@ -105,7 +106,6 @@ class ModuleTests(unittest.TestCase):
# TODO: RUSTPYTHON
@unittest.expectedFailure
- @requires_type_collecting
def test_clear_dict_in_ref_cycle(self):
destroyed = []
m = ModuleType("foo")
@@ -282,7 +282,6 @@ a = A(destroyed)"""
# TODO: RUSTPYTHON
@unittest.expectedFailure
- @requires_type_collecting
def test_module_finalization_at_shutdown(self):
# Module globals and builtins should still be available during shutdown
rc, out, err = assert_python_ok("-c", "from test import final_a")
@@ -304,6 +303,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):
+ ann_module4 = import_helper.import_fresh_module('test.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.
