1010import unittest
1111import tempfile
1212import os
13+ import contextlib
1314
1415from test import support
1516from test .support .script_helper import assert_python_ok
@@ -37,8 +38,7 @@ def test_basic_unused(self):
3738 """Lazy imported module should not be loaded if never accessed."""
3839 import test .test_lazy_import .data .basic_unused
3940 self .assertNotIn ("test.test_lazy_import.data.basic2" , sys .modules )
40- self .assertIn ("test.test_lazy_import.data" , sys .lazy_modules )
41- self .assertEqual (sys .lazy_modules ["test.test_lazy_import.data" ], {"basic2" })
41+ self .assertIn ("test.test_lazy_import.data.basic2" , sys .lazy_modules )
4242
4343 def test_sys_lazy_modules (self ):
4444 try :
@@ -48,7 +48,7 @@ def test_sys_lazy_modules(self):
4848
4949 self .assertFalse ("test.test_lazy_import.data.basic2" in sys .modules )
5050 self .assertIn ("test.test_lazy_import.data" , sys .lazy_modules )
51- self .assertEqual ( sys . lazy_modules [ "test.test_lazy_import.data" ], { "basic2" } )
51+ self .assertIn ( "test.test_lazy_import.data.basic2" , sys . lazy_modules )
5252 test .test_lazy_import .data .basic_from_unused .basic2
5353 self .assertNotIn ("test.test_import.data" , sys .lazy_modules )
5454
@@ -441,10 +441,14 @@ def tearDown(self):
441441
442442 def test_lazy_import_pkg (self ):
443443 """lazy import of package submodule should load the package."""
444- import test .test_lazy_import .data .lazy_import_pkg
444+ out = io .StringIO ()
445+
446+ with contextlib .redirect_stdout (out ):
447+ import test .test_lazy_import .data .lazy_import_pkg
445448
446449 self .assertIn ("test.test_lazy_import.data.pkg" , sys .modules )
447450 self .assertIn ("test.test_lazy_import.data.pkg.bar" , sys .modules )
451+ self .assertIn ("BAR_MODULE_LOADED" , out .getvalue ())
448452
449453 def test_lazy_import_pkg_cross_import (self ):
450454 """Cross-imports within package should preserve lazy imports."""
@@ -569,8 +573,8 @@ def my_filter(name):
569573 self .assertIs (sys .get_lazy_imports_filter (), my_filter )
570574
571575 def test_lazy_modules_attribute_is_dict (self ):
572- """sys.lazy_modules should be a dict per PEP 810."""
573- self .assertIsInstance (sys .lazy_modules , dict )
576+ """sys.lazy_modules should be a set per PEP 810."""
577+ self .assertIsInstance (sys .lazy_modules , set )
574578
575579 @support .requires_subprocess ()
576580 def test_lazy_modules_tracks_lazy_imports (self ):
@@ -579,8 +583,7 @@ def test_lazy_modules_tracks_lazy_imports(self):
579583 import sys
580584 initial_count = len(sys.lazy_modules)
581585 import test.test_lazy_import.data.basic_unused
582- assert "test.test_lazy_import.data" in sys.lazy_modules
583- assert sys.lazy_modules["test.test_lazy_import.data"] == {"basic2"}
586+ assert "test.test_lazy_import.data.basic2" in sys.lazy_modules
584587 assert len(sys.lazy_modules) > initial_count
585588 print("OK")
586589 """ )
@@ -1029,15 +1032,14 @@ def test_module_added_to_lazy_modules_on_lazy_import(self):
10291032 lazy import test.test_lazy_import.data.basic2
10301033
10311034 # Should be in lazy_modules after lazy import
1032- assert "test.test_lazy_import.data" in sys.lazy_modules
1033- assert sys.lazy_modules["test.test_lazy_import.data"] == {"basic2"}
1035+ assert "test.test_lazy_import.data.basic2" in sys.lazy_modules
10341036 assert len(sys.lazy_modules) > initial_count
10351037
10361038 # Trigger reification
10371039 _ = test.test_lazy_import.data.basic2.x
10381040
10391041 # Module should still be tracked (for diagnostics per PEP 810)
1040- assert "test.test_lazy_import.data" not in sys.lazy_modules
1042+ assert "test.test_lazy_import.data.basic2 " not in sys.lazy_modules
10411043 print("OK")
10421044 """ )
10431045 result = subprocess .run (
@@ -1050,8 +1052,8 @@ def test_module_added_to_lazy_modules_on_lazy_import(self):
10501052
10511053 def test_lazy_modules_is_per_interpreter (self ):
10521054 """Each interpreter should have independent sys.lazy_modules."""
1053- # Basic test that sys.lazy_modules exists and is a dict
1054- self .assertIsInstance (sys .lazy_modules , dict )
1055+ # Basic test that sys.lazy_modules exists and is a set
1056+ self .assertIsInstance (sys .lazy_modules , set )
10551057
10561058 def test_lazy_module_without_children_is_tracked (self ):
10571059 code = textwrap .dedent ("""
@@ -1060,10 +1062,6 @@ def test_lazy_module_without_children_is_tracked(self):
10601062 assert "json" in sys.lazy_modules, (
10611063 f"expected 'json' in sys.lazy_modules, got {set(sys.lazy_modules)}"
10621064 )
1063- assert sys.lazy_modules["json"] == set(), (
1064- f"expected empty set for sys.lazy_modules['json'], "
1065- f"got {sys.lazy_modules['json']!r}"
1066- )
10671065 print("OK")
10681066 """ )
10691067 assert_python_ok ("-c" , code )
@@ -1932,7 +1930,7 @@ def create_lazy_imports(idx):
19321930 t.join()
19331931
19341932 assert not errors, f"Errors: {errors}"
1935- assert isinstance(sys.lazy_modules, dict ), "sys.lazy_modules is not a dict"
1933+ assert isinstance(sys.lazy_modules, set ), "sys.lazy_modules is not a dict"
19361934 print("OK")
19371935 """ )
19381936
0 commit comments