From 1342951e82e804871a68bf520c7e1035fc515b81 Mon Sep 17 00:00:00 2001 From: Matthias Schoettle Date: Tue, 10 Mar 2026 10:50:01 -0400 Subject: [PATCH 1/5] `unittest.mock`: Update signature retrieval to use `String` annotation format --- Lib/unittest/mock.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 64a01a0b713c61..f44f057b3fdba6 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -34,6 +34,7 @@ import pkgutil from inspect import iscoroutinefunction import threading +from annotationlib import Format from dataclasses import fields, is_dataclass from types import CodeType, ModuleType, MethodType from unittest.util import safe_repr @@ -119,7 +120,7 @@ def _get_signature_object(func, as_instance, eat_self): else: sig_func = func try: - return func, inspect.signature(sig_func) + return func, inspect.signature(sig_func, annotation_format=Format.STRING) except ValueError: # Certain callable types are not supported by inspect.signature() return None From d64ded3198cda6d5e9681262c033b77a90b2956a Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 10 Mar 2026 14:57:16 +0000 Subject: [PATCH 2/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/2026-03-10-14-57-15.gh-issue-145754.YBL5Ko.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-03-10-14-57-15.gh-issue-145754.YBL5Ko.rst diff --git a/Misc/NEWS.d/next/Library/2026-03-10-14-57-15.gh-issue-145754.YBL5Ko.rst b/Misc/NEWS.d/next/Library/2026-03-10-14-57-15.gh-issue-145754.YBL5Ko.rst new file mode 100644 index 00000000000000..8b2b222bbff6c9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-03-10-14-57-15.gh-issue-145754.YBL5Ko.rst @@ -0,0 +1 @@ +Request signature during mock autospec with string annotation format From ae68a423da7d843f3cf2b2f14dfe659d72065d5e Mon Sep 17 00:00:00 2001 From: Matthias Schoettle Date: Tue, 10 Mar 2026 11:10:31 -0400 Subject: [PATCH 3/5] use forwardref instead --- Lib/unittest/mock.py | 2 +- .../Library/2026-03-10-14-57-15.gh-issue-145754.YBL5Ko.rst | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index f44f057b3fdba6..1cee67fa5d7094 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -120,7 +120,7 @@ def _get_signature_object(func, as_instance, eat_self): else: sig_func = func try: - return func, inspect.signature(sig_func, annotation_format=Format.STRING) + return func, inspect.signature(sig_func, annotation_format=Format.FORWARDREF) except ValueError: # Certain callable types are not supported by inspect.signature() return None diff --git a/Misc/NEWS.d/next/Library/2026-03-10-14-57-15.gh-issue-145754.YBL5Ko.rst b/Misc/NEWS.d/next/Library/2026-03-10-14-57-15.gh-issue-145754.YBL5Ko.rst index 8b2b222bbff6c9..7203b143f9e867 100644 --- a/Misc/NEWS.d/next/Library/2026-03-10-14-57-15.gh-issue-145754.YBL5Ko.rst +++ b/Misc/NEWS.d/next/Library/2026-03-10-14-57-15.gh-issue-145754.YBL5Ko.rst @@ -1 +1,3 @@ -Request signature during mock autospec with string annotation format +Request signature during mock autospec with forwardref annotation format. +This prevents runtime errors when a type referred in a type hint is +imported within a `TYPE_CHECKING` block. From 71caad235b60d401caea10614021564dd75ecd14 Mon Sep 17 00:00:00 2001 From: Matthias Schoettle Date: Tue, 10 Mar 2026 11:13:49 -0400 Subject: [PATCH 4/5] fix backticks in news entry --- .../Library/2026-03-10-14-57-15.gh-issue-145754.YBL5Ko.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2026-03-10-14-57-15.gh-issue-145754.YBL5Ko.rst b/Misc/NEWS.d/next/Library/2026-03-10-14-57-15.gh-issue-145754.YBL5Ko.rst index 7203b143f9e867..10539af74f8bfe 100644 --- a/Misc/NEWS.d/next/Library/2026-03-10-14-57-15.gh-issue-145754.YBL5Ko.rst +++ b/Misc/NEWS.d/next/Library/2026-03-10-14-57-15.gh-issue-145754.YBL5Ko.rst @@ -1,3 +1,3 @@ -Request signature during mock autospec with forwardref annotation format. +Request signature during mock autospec with ``FORWARDREF`` annotation format. This prevents runtime errors when a type referred in a type hint is -imported within a `TYPE_CHECKING` block. +imported within a ``TYPE_CHECKING`` block. From 5d9a3945d89b827e51f78a1a4a14418d0065dfa8 Mon Sep 17 00:00:00 2001 From: Matthias Schoettle Date: Tue, 10 Mar 2026 12:52:20 -0400 Subject: [PATCH 5/5] add test --- Lib/test/test_unittest/testmock/testmock.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Lib/test/test_unittest/testmock/testmock.py b/Lib/test/test_unittest/testmock/testmock.py index 386d53bf5a5c63..7b828e275d9a9a 100644 --- a/Lib/test/test_unittest/testmock/testmock.py +++ b/Lib/test/test_unittest/testmock/testmock.py @@ -1743,6 +1743,17 @@ def static_method(): pass mock_method.assert_called_once_with() self.assertRaises(TypeError, mock_method, 'extra_arg') + #Issue145754 + def test_create_autospec_type_hints_typechecking(self): + from typing import TYPE_CHECKING + if TYPE_CHECKING: + from typing import Tuple + + def foo(x: Tuple[int, ...]) -> None: + pass + + mock.create_autospec(foo) + #Issue21238 def test_mock_unsafe(self): m = Mock()