From a7f7d55adfed0eef7697830a5e5f8753d72e9b6f Mon Sep 17 00:00:00 2001 From: palakkhinvasara Date: Sat, 18 Jul 2026 19:43:23 +0530 Subject: [PATCH 1/7] oushinbg --- Lib/test/test_unittest/testmock/testpatch.py | 6 ++++++ Lib/unittest/mock.py | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_unittest/testmock/testpatch.py b/Lib/test/test_unittest/testmock/testpatch.py index bd85fdcfc472a61..3d25cb721b0ffd4 100644 --- a/Lib/test/test_unittest/testmock/testpatch.py +++ b/Lib/test/test_unittest/testmock/testpatch.py @@ -23,6 +23,7 @@ builtin_string = 'builtins' PTModule = sys.modules[__name__] +lazy from json import dumps MODNAME = '%s.PTModule' % __name__ @@ -2101,5 +2102,10 @@ def test(_): test() + def test_patch_autospec_lazy_import(self): + with patch(f"{__name__}.dumps", autospec=True) as mock_dumps: + mock_dumps({}) + mock_dumps.assert_called_once_with({}) + if __name__ == '__main__': unittest.main() diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 49011faaa51eaed..0cb766d3188fb9a 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -31,6 +31,7 @@ import sys import builtins import pkgutil +import types from inspect import iscoroutinefunction import threading from annotationlib import Format @@ -1469,7 +1470,9 @@ def get_original(self): except (AttributeError, KeyError): original = getattr(target, name, DEFAULT) else: - local = True + local = True + if isinstance(original, types.LazyImportType): + original = original.resolve() if name in _builtins and isinstance(target, ModuleType): self.create = True From d78481336fc91a337e35ed0031556c3b2f196a34 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 18 Jul 2026 14:20:15 +0000 Subject: [PATCH 2/7] =?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 --- .../2026-07-18-14-20-13.gh-issue-153888.K9Otcl.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-07-18-14-20-13.gh-issue-153888.K9Otcl.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-18-14-20-13.gh-issue-153888.K9Otcl.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-18-14-20-13.gh-issue-153888.K9Otcl.rst new file mode 100644 index 000000000000000..7d5f3e8c4299a67 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-18-14-20-13.gh-issue-153888.K9Otcl.rst @@ -0,0 +1,2 @@ +Fix :func:`unittest.mock.patch` with ``autospec``, ``spec``, or ``spec_set`` +when used with lazy-imported objects. From 2f6706bc18eca542f4f8210274babe666c3e8884 Mon Sep 17 00:00:00 2001 From: palakkhinvasara Date: Sat, 18 Jul 2026 20:11:52 +0530 Subject: [PATCH 3/7] pushing --- Lib/test/test_unittest/testmock/testpatch.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_unittest/testmock/testpatch.py b/Lib/test/test_unittest/testmock/testpatch.py index 3d25cb721b0ffd4..763bc43d671841c 100644 --- a/Lib/test/test_unittest/testmock/testpatch.py +++ b/Lib/test/test_unittest/testmock/testpatch.py @@ -23,7 +23,7 @@ builtin_string = 'builtins' PTModule = sys.modules[__name__] -lazy from json import dumps +lazy from json import dumps as lazy_dumps MODNAME = '%s.PTModule' % __name__ @@ -2103,7 +2103,7 @@ def test(_): def test_patch_autospec_lazy_import(self): - with patch(f"{__name__}.dumps", autospec=True) as mock_dumps: + with patch(f"{__name__}.lazy_dumps", autospec=True) as mock_dumps: mock_dumps({}) mock_dumps.assert_called_once_with({}) From 116d6f1bec31cab58f6e73eb1d0d67c9a37d8f1e Mon Sep 17 00:00:00 2001 From: palakkhinvasara Date: Sat, 18 Jul 2026 20:23:09 +0530 Subject: [PATCH 4/7] pushing --- .../2026-07-18-14-20-13.gh-issue-153888.K9Otcl.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename Misc/NEWS.d/next/{Core_and_Builtins => Library}/2026-07-18-14-20-13.gh-issue-153888.K9Otcl.rst (99%) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-18-14-20-13.gh-issue-153888.K9Otcl.rst b/Misc/NEWS.d/next/Library/2026-07-18-14-20-13.gh-issue-153888.K9Otcl.rst similarity index 99% rename from Misc/NEWS.d/next/Core_and_Builtins/2026-07-18-14-20-13.gh-issue-153888.K9Otcl.rst rename to Misc/NEWS.d/next/Library/2026-07-18-14-20-13.gh-issue-153888.K9Otcl.rst index 7d5f3e8c4299a67..278c9560cec216e 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-18-14-20-13.gh-issue-153888.K9Otcl.rst +++ b/Misc/NEWS.d/next/Library/2026-07-18-14-20-13.gh-issue-153888.K9Otcl.rst @@ -1,2 +1,2 @@ -Fix :func:`unittest.mock.patch` with ``autospec``, ``spec``, or ``spec_set`` +Fix :func:`unittest.mock.patch` with ``autospec``, ``spec``, or ``spec_set`` when used with lazy-imported objects. From dd1842c87ddcd58e23d7f2d9a059f58b995c677a Mon Sep 17 00:00:00 2001 From: palakkhinvasara Date: Sat, 18 Jul 2026 20:25:24 +0530 Subject: [PATCH 5/7] pushing --- Lib/test/test_unittest/testmock/testpatch.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_unittest/testmock/testpatch.py b/Lib/test/test_unittest/testmock/testpatch.py index 763bc43d671841c..4e7578591855f1f 100644 --- a/Lib/test/test_unittest/testmock/testpatch.py +++ b/Lib/test/test_unittest/testmock/testpatch.py @@ -2103,6 +2103,8 @@ def test(_): def test_patch_autospec_lazy_import(self): + self.assertIn("lazy_dumps", globals()) + with patch(f"{__name__}.lazy_dumps", autospec=True) as mock_dumps: mock_dumps({}) mock_dumps.assert_called_once_with({}) From 226f2a0599728d20dc990e23f66ef3ae0ab774ec Mon Sep 17 00:00:00 2001 From: palakkhinvasara Date: Sat, 18 Jul 2026 20:30:00 +0530 Subject: [PATCH 6/7] pushing --- Lib/test/test_unittest/testmock/testpatch.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_unittest/testmock/testpatch.py b/Lib/test/test_unittest/testmock/testpatch.py index 4e7578591855f1f..2cd9a3b560e8225 100644 --- a/Lib/test/test_unittest/testmock/testpatch.py +++ b/Lib/test/test_unittest/testmock/testpatch.py @@ -23,7 +23,7 @@ builtin_string = 'builtins' PTModule = sys.modules[__name__] -lazy from json import dumps as lazy_dumps +lazy from json import dumps as lazy_dumps # noqa: F401 MODNAME = '%s.PTModule' % __name__ @@ -2104,7 +2104,7 @@ def test(_): def test_patch_autospec_lazy_import(self): self.assertIn("lazy_dumps", globals()) - + with patch(f"{__name__}.lazy_dumps", autospec=True) as mock_dumps: mock_dumps({}) mock_dumps.assert_called_once_with({}) From 59a848aad2e8ecbd3d10f0dced0e7bfb9c6b6c5c Mon Sep 17 00:00:00 2001 From: palakkhinvasara Date: Mon, 20 Jul 2026 18:02:53 +0530 Subject: [PATCH 7/7] pushing --- Lib/test/test_unittest/testmock/testpatch.py | 2 +- Lib/unittest/mock.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_unittest/testmock/testpatch.py b/Lib/test/test_unittest/testmock/testpatch.py index 2cd9a3b560e8225..c41c70bc89baa63 100644 --- a/Lib/test/test_unittest/testmock/testpatch.py +++ b/Lib/test/test_unittest/testmock/testpatch.py @@ -13,6 +13,7 @@ from test.support.import_helper import DirsOnSysPath from test.test_importlib.util import uncache +lazy from json import dumps as lazy_dumps # noqa: F401 from unittest.mock import ( NonCallableMock, CallableMixin, sentinel, MagicMock, Mock, NonCallableMagicMock, patch, _patch, @@ -23,7 +24,6 @@ builtin_string = 'builtins' PTModule = sys.modules[__name__] -lazy from json import dumps as lazy_dumps # noqa: F401 MODNAME = '%s.PTModule' % __name__ diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 0cb766d3188fb9a..5f270e7a5ea804f 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -1470,9 +1470,9 @@ def get_original(self): except (AttributeError, KeyError): original = getattr(target, name, DEFAULT) else: - local = True - if isinstance(original, types.LazyImportType): - original = original.resolve() + local = True + if isinstance(original, types.LazyImportType): + original = original.resolve() if name in _builtins and isinstance(target, ModuleType): self.create = True