https://github.com/python/cpython/commit/7ba9580f00d3ae2c79cb43f5663725e6d786ef7c commit: 7ba9580f00d3ae2c79cb43f5663725e6d786ef7c branch: 3.14 author: Miss Islington (bot) <[email protected]> committer: JelleZijlstra <[email protected]> date: 2026-03-20T03:06:03Z summary:
[3.14] gh-145754: Update signature retrieval in unittest.mock to use forwardref annotation format (GH-145756) (#146191) gh-145754: Update signature retrieval in unittest.mock to use forwardref annotation format (GH-145756) (cherry picked from commit d357a7dbf38868844415ec1d5df80379ea5a2326) Co-authored-by: Matthias Schoettle <[email protected]> files: A Misc/NEWS.d/next/Library/2026-03-10-14-57-15.gh-issue-145754.YBL5Ko.rst M Lib/test/test_unittest/testmock/testmock.py M Lib/unittest/mock.py diff --git a/Lib/test/test_unittest/testmock/testmock.py b/Lib/test/test_unittest/testmock/testmock.py index 386d53bf5a5c63..764585ec5d5468 100644 --- a/Lib/test/test_unittest/testmock/testmock.py +++ b/Lib/test/test_unittest/testmock/testmock.py @@ -1743,6 +1743,13 @@ def static_method(): pass mock_method.assert_called_once_with() self.assertRaises(TypeError, mock_method, 'extra_arg') + # gh-145754 + def test_create_autospec_type_hints_typechecking(self): + def foo(x: Tuple[int, ...]) -> None: + pass + + mock.create_autospec(foo) + #Issue21238 def test_mock_unsafe(self): m = Mock() diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 545b0730d2e039..92b81d1584142e 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.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 new file mode 100644 index 00000000000000..7de81ac19c2efa --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-03-10-14-57-15.gh-issue-145754.YBL5Ko.rst @@ -0,0 +1,2 @@ +Request signature during mock autospec with ``FORWARDREF`` annotation format. +This prevents runtime errors when an annotation uses a name that is not defined at runtime. _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
