Skip to content

Commit d357a7d

Browse files
authored
gh-145754: Update signature retrieval in unittest.mock to use forwardref annotation format (#145756)
1 parent 6b5511d commit d357a7d

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

Lib/test/test_unittest/testmock/testmock.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,13 @@ def static_method(): pass
17431743
mock_method.assert_called_once_with()
17441744
self.assertRaises(TypeError, mock_method, 'extra_arg')
17451745

1746+
# gh-145754
1747+
def test_create_autospec_type_hints_typechecking(self):
1748+
def foo(x: Tuple[int, ...]) -> None:
1749+
pass
1750+
1751+
mock.create_autospec(foo)
1752+
17461753
#Issue21238
17471754
def test_mock_unsafe(self):
17481755
m = Mock()

Lib/unittest/mock.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import pkgutil
3535
from inspect import iscoroutinefunction
3636
import threading
37+
from annotationlib import Format
3738
from dataclasses import fields, is_dataclass
3839
from types import CodeType, ModuleType, MethodType
3940
from unittest.util import safe_repr
@@ -119,7 +120,7 @@ def _get_signature_object(func, as_instance, eat_self):
119120
else:
120121
sig_func = func
121122
try:
122-
return func, inspect.signature(sig_func)
123+
return func, inspect.signature(sig_func, annotation_format=Format.FORWARDREF)
123124
except ValueError:
124125
# Certain callable types are not supported by inspect.signature()
125126
return None
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Request signature during mock autospec with ``FORWARDREF`` annotation format.
2+
This prevents runtime errors when an annotation uses a name that is not defined at runtime.

0 commit comments

Comments
 (0)