From fd952d86161d052ebebd69fb8d495ce31048e34e Mon Sep 17 00:00:00 2001 From: Amer Elsheikh Date: Thu, 9 Oct 2025 12:15:08 +0000 Subject: [PATCH 1/5] fix reload with lazy --- Lib/importlib/__init__.py | 5 +++++ Lib/test/test_importlib/test_lazy.py | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/Lib/importlib/__init__.py b/Lib/importlib/__init__.py index a7d57561ead046..694fea806f7944 100644 --- a/Lib/importlib/__init__.py +++ b/Lib/importlib/__init__.py @@ -97,6 +97,11 @@ def reload(module): The module must have been successfully imported before. """ + # If a LazyModule has not yet been materialized, reload is a no-op. + if importlib_util := sys.modules.get('importlib.util'): + if lazy_module_type := getattr(importlib_util, '_LazyModule', None): + if isinstance(module, lazy_module_type): + return module try: name = module.__spec__.name except AttributeError: diff --git a/Lib/test/test_importlib/test_lazy.py b/Lib/test/test_importlib/test_lazy.py index e48fad8898f0ef..335f0c2bc42205 100644 --- a/Lib/test/test_importlib/test_lazy.py +++ b/Lib/test/test_importlib/test_lazy.py @@ -224,6 +224,28 @@ def __delattr__(self, name): with self.assertRaises(AttributeError): del module.CONSTANT + def test_reload(self): + # Reloading a lazy module that hasn't been materialized is a no-op. + module = self.new_module() + sys.modules[TestingImporter.module_name] = module + self.assertIsInstance(module, util._LazyModule) + + # Change the source code to add a new attribute + TestingImporter.source_code = 'attr = 42\nnew_attr = 123\n__name__ = {!r}'.format(TestingImporter.mutated_name) + self.assertIsInstance(module, util._LazyModule) + + # Reload the module (should be a no-op since not materialized) + reloaded = importlib.reload(module) + self.assertIs(reloaded, module) + self.assertIsInstance(module, util._LazyModule) + + + # Access the new attribute (should trigger materialization, and new_attr should exist) + self.assertEqual(module.attr, 42) + self.assertNotIsInstance(module, util._LazyModule) + self.assertTrue(hasattr(module, 'new_attr')) + self.assertEqual(module.new_attr, 123) + if __name__ == '__main__': unittest.main() From e79e806c6a3d522f1bf1891ec16ecc19995491d9 Mon Sep 17 00:00:00 2001 From: Amer Elsheikh Date: Thu, 9 Oct 2025 14:43:01 +0000 Subject: [PATCH 2/5] Make "sys.modules.get('importlib.util')" with "import importlib.util" in test_lazy.py Without this `./python -m test test_importlib` fails at `test_lazy` although `./python -m test test_importlib.test_lazy` perfectyl succeeds. That is becasue some other tests likely manipulates `util` making the above two out of sync. --- Lib/test/test_importlib/test_lazy.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/test/test_importlib/test_lazy.py b/Lib/test/test_importlib/test_lazy.py index 335f0c2bc42205..4b034d2600ddb6 100644 --- a/Lib/test/test_importlib/test_lazy.py +++ b/Lib/test/test_importlib/test_lazy.py @@ -10,6 +10,9 @@ from test.support import threading_helper from test.test_importlib import util as test_util +# Make sure sys.modules[util] is in sync with the import. +# That is needed as other tests may reload util. +sys.modules['importlib.util'] = util class CollectInit: From 4d8c88408ed2a1c58546e7c03adbff3f413e5001 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 9 Oct 2025 15:46:19 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-10-09-15-46-18.gh-issue-139686.XwIZB2.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-10-09-15-46-18.gh-issue-139686.XwIZB2.rst diff --git a/Misc/NEWS.d/next/Library/2025-10-09-15-46-18.gh-issue-139686.XwIZB2.rst b/Misc/NEWS.d/next/Library/2025-10-09-15-46-18.gh-issue-139686.XwIZB2.rst new file mode 100644 index 00000000000000..00dd3447eebb9b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-10-09-15-46-18.gh-issue-139686.XwIZB2.rst @@ -0,0 +1 @@ +Make importlib.reload no-op for lazy modules. From e146f6a85e6519f32c32200b8f7e7b0a9cced50d Mon Sep 17 00:00:00 2001 From: Amer Elsheikh Date: Thu, 9 Oct 2025 20:31:49 +0000 Subject: [PATCH 4/5] resolve comments --- Lib/test/test_importlib/test_lazy.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_importlib/test_lazy.py b/Lib/test/test_importlib/test_lazy.py index 4b034d2600ddb6..e6b5a385265204 100644 --- a/Lib/test/test_importlib/test_lazy.py +++ b/Lib/test/test_importlib/test_lazy.py @@ -231,19 +231,17 @@ def test_reload(self): # Reloading a lazy module that hasn't been materialized is a no-op. module = self.new_module() sys.modules[TestingImporter.module_name] = module - self.assertIsInstance(module, util._LazyModule) - # Change the source code to add a new attribute + # Change the source code to add a new attribute. TestingImporter.source_code = 'attr = 42\nnew_attr = 123\n__name__ = {!r}'.format(TestingImporter.mutated_name) self.assertIsInstance(module, util._LazyModule) - # Reload the module (should be a no-op since not materialized) + # Reload the module (should be a no-op since not materialized). reloaded = importlib.reload(module) self.assertIs(reloaded, module) self.assertIsInstance(module, util._LazyModule) - - # Access the new attribute (should trigger materialization, and new_attr should exist) + # Access the new attribute (should trigger materialization, and new_attr should exist). self.assertEqual(module.attr, 42) self.assertNotIsInstance(module, util._LazyModule) self.assertTrue(hasattr(module, 'new_attr')) From a64ccd68eaeec5dc7162aec5d236b19ef485d1c3 Mon Sep 17 00:00:00 2001 From: Amer Elsheikh Date: Thu, 9 Oct 2025 20:36:04 +0000 Subject: [PATCH 5/5] one more dot --- Lib/test/test_importlib/test_lazy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_importlib/test_lazy.py b/Lib/test/test_importlib/test_lazy.py index e6b5a385265204..c6b26ad75b97f9 100644 --- a/Lib/test/test_importlib/test_lazy.py +++ b/Lib/test/test_importlib/test_lazy.py @@ -195,7 +195,7 @@ def test_lazy_self_referential_modules(self): sys.modules['json'] = module loader.exec_module(module) - # Trigger load with attribute lookup, ensure expected behavior + # Trigger load with attribute lookup, ensure expected behavior. test_load = module.loads('{}') self.assertEqual(test_load, {})