From 1b490e6fb15a66bd036c1e4c833d3a1b4d6ae921 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Tue, 24 Mar 2026 11:51:42 +0200 Subject: [PATCH 1/3] Failing test: lazy_imports=none or PYTHON_LAZY_IMPORTS=none should override __lazy_modules__ --- Lib/test/test_lazy_import/__init__.py | 43 +++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Lib/test/test_lazy_import/__init__.py b/Lib/test/test_lazy_import/__init__.py index a4180f05dbbafc..328f2906f90159 100644 --- a/Lib/test/test_lazy_import/__init__.py +++ b/Lib/test/test_lazy_import/__init__.py @@ -1088,6 +1088,49 @@ def test_env_var_lazy_imports_none_disables_all_lazy(self): self.assertEqual(result.returncode, 0, f"stderr: {result.stderr}") self.assertIn("EAGER", result.stdout) + def test_cli_lazy_imports_none_disables_dunder_lazy_modules(self): + """-X lazy_imports=none should override __lazy_modules__.""" + code = textwrap.dedent(""" + import sys + __lazy_modules__ = ["json"] + import json + if 'json' in sys.modules: + print("EAGER") + else: + print("LAZY") + """) + result = subprocess.run( + [sys.executable, "-X", "lazy_imports=none", "-c", code], + capture_output=True, + text=True, + ) + self.assertEqual(result.returncode, 0, f"stderr: {result.stderr}") + self.assertIn("EAGER", result.stdout) + + def test_env_var_lazy_imports_none_disables_dunder_lazy_modules(self): + """PYTHON_LAZY_IMPORTS=none should override __lazy_modules__.""" + code = textwrap.dedent(""" + import sys + __lazy_modules__ = ["json"] + import json + if 'json' in sys.modules: + print("EAGER") + else: + print("LAZY") + """) + import os + + env = os.environ.copy() + env["PYTHON_LAZY_IMPORTS"] = "none" + result = subprocess.run( + [sys.executable, "-c", code], + capture_output=True, + text=True, + env=env, + ) + self.assertEqual(result.returncode, 0, f"stderr: {result.stderr}") + self.assertIn("EAGER", result.stdout) + def test_cli_overrides_env_var(self): """Command-line option should take precedence over environment variable.""" # PEP 810: -X lazy_imports takes precedence over PYTHON_LAZY_IMPORTS From 7444b0c348e4a4b11c86f055d9822b5112abb8e6 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Tue, 24 Mar 2026 12:03:27 +0200 Subject: [PATCH 2/3] Ensure lazy_imports=none or PYTHON_LAZY_IMPORTS=none override __lazy_modules__ --- Python/ceval.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/ceval.c b/Python/ceval.c index cb25012ceda92c..2f9195529f2ceb 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -3086,7 +3086,7 @@ _PyEval_LazyImportName(PyThreadState *tstate, PyObject *builtins, break; } - if (!lazy) { + if (!lazy && PyImport_GetLazyImportsMode() != PyImport_LAZY_NONE) { // See if __lazy_modules__ forces this to be lazy. lazy = check_lazy_import_compatibility(tstate, globals, name, level); if (lazy < 0) { From 2ebe2553b48142c273c48d606a6e0fa28850d966 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Tue, 24 Mar 2026 13:07:00 +0200 Subject: [PATCH 3/3] Add blurb --- .../2026-03-24-13-06-52.gh-issue-146369.6wDI6S.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-03-24-13-06-52.gh-issue-146369.6wDI6S.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-24-13-06-52.gh-issue-146369.6wDI6S.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-24-13-06-52.gh-issue-146369.6wDI6S.rst new file mode 100644 index 00000000000000..191b7627ed4e56 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-24-13-06-52.gh-issue-146369.6wDI6S.rst @@ -0,0 +1,2 @@ +Ensure ``-X lazy_imports=none``` and ``PYTHON_LAZY_IMPORTS=none``` override +``__lazy_modules__``. Patch by Hugo van Kemenade.