From c4309210a0e8e8e511ec4c0cbdec3ea237ba28bb Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 12 Nov 2026 13:16:57 +0300 Subject: [PATCH 1/3] gh-161660: Fix `test_json` with `PYTHON_LAZY_IMPORTS=all` --- Lib/test/support/import_helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/support/import_helper.py b/Lib/test/support/import_helper.py index 962f65d8dcd14a2..dac09235765c33b 100644 --- a/Lib/test/support/import_helper.py +++ b/Lib/test/support/import_helper.py @@ -187,7 +187,7 @@ def import_fresh_module(name, fresh=(), blocked=(), *, """ # NOTE: test_heapq, test_json and test_warnings include extra sanity checks # to make sure that this utility function is working as expected - with _ignore_deprecated_imports(deprecated): + with force_eager_imports(), _ignore_deprecated_imports(deprecated): # Keep track of modules saved for later restoration as well # as those which just need a blocking entry removed fresh = list(fresh) From 7e9769f994398d428f349cdfbe9b1ac44a6e1743 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 18 Nov 2026 13:15:23 +0300 Subject: [PATCH 2/3] Fix CI --- Lib/test/lazy_imports_all_exclude.txt | 2 -- Lib/test/support/import_helper.py | 8 +++++++- Lib/test/test_type_annotations.py | 6 +++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Lib/test/lazy_imports_all_exclude.txt b/Lib/test/lazy_imports_all_exclude.txt index e5519323e88e38a..f85ea5e4f301c04 100644 --- a/Lib/test/lazy_imports_all_exclude.txt +++ b/Lib/test/lazy_imports_all_exclude.txt @@ -20,10 +20,8 @@ test.test_inspect.test_inspect test___all__ test__interpreters test_crossinterp -test_datetime test_import test_importlib -test_json test_pkg test_profile test_profiling diff --git a/Lib/test/support/import_helper.py b/Lib/test/support/import_helper.py index dac09235765c33b..0d39e9a236be88a 100644 --- a/Lib/test/support/import_helper.py +++ b/Lib/test/support/import_helper.py @@ -156,6 +156,7 @@ def multi_interp_extensions_check(enabled=True): def import_fresh_module(name, fresh=(), blocked=(), *, deprecated=False, usefrozen=False, + force_eager_imports=True, ): """Import and return a module, deliberately bypassing sys.modules. @@ -187,7 +188,12 @@ def import_fresh_module(name, fresh=(), blocked=(), *, """ # NOTE: test_heapq, test_json and test_warnings include extra sanity checks # to make sure that this utility function is working as expected - with force_eager_imports(), _ignore_deprecated_imports(deprecated): + eager_imports_context = ( + force_eager_imports + if force_eager_imports + else contextlib.nullcontext + ) + with eager_imports_context(), _ignore_deprecated_imports(deprecated): # Keep track of modules saved for later restoration as well # as those which just need a blocking entry removed fresh = list(fresh) diff --git a/Lib/test/test_type_annotations.py b/Lib/test/test_type_annotations.py index 879a571d06b4a03..fc391ee2329d07f 100644 --- a/Lib/test/test_type_annotations.py +++ b/Lib/test/test_type_annotations.py @@ -153,7 +153,11 @@ class D(metaclass=C): self.assertEqual(D.__annotations__, {}) def test_partially_executed_module(self): - partialexe = import_helper.import_fresh_module("test.typinganndata.partialexecution") + # Results are different with and without lazy imports, we need both: + partialexe = import_helper.import_fresh_module( + "test.typinganndata.partialexecution", + force_eager_imports=False, + ) self.assertEqual( partialexe.a.__annotations__, {"v1": int, "v2": int}, From 94e175281429582f8c3ca2aa9ac9529203f1bbe9 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 18 Nov 2026 13:20:28 +0300 Subject: [PATCH 3/3] Fix CI --- Lib/test/support/import_helper.py | 4 ++-- Lib/test/test_type_annotations.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/support/import_helper.py b/Lib/test/support/import_helper.py index 0d39e9a236be88a..26790c6995e7810 100644 --- a/Lib/test/support/import_helper.py +++ b/Lib/test/support/import_helper.py @@ -156,7 +156,7 @@ def multi_interp_extensions_check(enabled=True): def import_fresh_module(name, fresh=(), blocked=(), *, deprecated=False, usefrozen=False, - force_eager_imports=True, + eager_imports=True, ): """Import and return a module, deliberately bypassing sys.modules. @@ -190,7 +190,7 @@ def import_fresh_module(name, fresh=(), blocked=(), *, # to make sure that this utility function is working as expected eager_imports_context = ( force_eager_imports - if force_eager_imports + if eager_imports else contextlib.nullcontext ) with eager_imports_context(), _ignore_deprecated_imports(deprecated): diff --git a/Lib/test/test_type_annotations.py b/Lib/test/test_type_annotations.py index fc391ee2329d07f..02f3e8b9caddb4c 100644 --- a/Lib/test/test_type_annotations.py +++ b/Lib/test/test_type_annotations.py @@ -156,7 +156,7 @@ def test_partially_executed_module(self): # Results are different with and without lazy imports, we need both: partialexe = import_helper.import_fresh_module( "test.typinganndata.partialexecution", - force_eager_imports=False, + eager_imports=False, ) self.assertEqual( partialexe.a.__annotations__,