From 52b542b47e2aa0d04278dfbc238953f975cfd3ca Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 18 Nov 2026 16:46:32 +0300 Subject: [PATCH] [3.15] gh-161660: Fix `test_json` with `PYTHON_LAZY_IMPORTS=all` (GH-161866) (cherry picked from commit b107dc253f2e788693d922e76c74501c91f7d42e) Co-authored-by: sobolevn --- Lib/test/support/import_helper.py | 8 +++++++- Lib/test/test_type_annotations.py | 6 +++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Lib/test/support/import_helper.py b/Lib/test/support/import_helper.py index d2061b88ea84b2..abe3c7c2841046 100644 --- a/Lib/test/support/import_helper.py +++ b/Lib/test/support/import_helper.py @@ -160,6 +160,7 @@ def multi_interp_extensions_check(enabled=True): def import_fresh_module(name, fresh=(), blocked=(), *, deprecated=False, usefrozen=False, + eager_imports=True, ): """Import and return a module, deliberately bypassing sys.modules. @@ -191,7 +192,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 _ignore_deprecated_imports(deprecated): + eager_imports_context = ( + force_eager_imports + if 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 879a571d06b4a0..02f3e8b9caddb4 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", + eager_imports=False, + ) self.assertEqual( partialexe.a.__annotations__, {"v1": int, "v2": int},