GeezerVison is showing github.com — Calling inspect.signature with an AsyncMock instance raises an exception · Issue #96127 · python/cpython
Calling inspect.signature with an AsyncMock instance raises an exception #96127Description
Bug report
Starting in Python 3.10.6, the following code raises an exception:
from inspect import signature
from unittest.mock import AsyncMock
mock = AsyncMock()
signature(mock)
- No exception is raised when
Mock is used.
- The exception does not occur with Python 3.10.5.
Your environment
- CPython versions tested on: 3.10.4, 3.10.5, 3.10.6 (Only occurs in 3.10.6)
- Operating system and architecture: Windows 10, Linux (Official Docker Image)
Linked PRs
Activitygraingert commented on Aug 23, 2022 tirkarthi commented on Aug 23, 2022 This could be due to commit 4261b6b
(myenv) ➜ cpython git:(main) ./python
Python 3.12.0a0 (heads/main:575f8880bf, Aug 23 2022, 19:59:51) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> from unittest.mock import AsyncMock
>>> inspect.signature(AsyncMock())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/karthikeyan/stuff/python/cpython/Lib/inspect.py", line 3272, in signature
return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/karthikeyan/stuff/python/cpython/Lib/inspect.py", line 3020, in from_callable
return _signature_from_callable(obj, sigcls=cls,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/karthikeyan/stuff/python/cpython/Lib/inspect.py", line 2507, in _signature_from_callable
return _signature_from_function(sigcls, obj,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/karthikeyan/stuff/python/cpython/Lib/inspect.py", line 2351, in _signature_from_function
positional = arg_names[:pos_count]
~~~~~~~~~^^^^^^^^^^^^
TypeError: 'Mock' object is not subscriptable
>>>
(myenv) ➜ cpython git:(main) git log Lib/unittest/mock.py
(myenv) ➜ cpython git:(main) git checkout 4261b6bffc0b8bb5c6d4d80578a81b7520f4aefc~1 Lib/unittest/mock.py
Updated 1 path from ec0ed6ea8a
(myenv) ➜ cpython git:(main) ✗ ./python
Python 3.12.0a0 (heads/main:575f8880bf, Aug 23 2022, 19:59:51) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> from unittest.mock import AsyncMock
>>> inspect.signature(AsyncMock())
<Signature (*args, **kwargs)>
sobolevn commented on Aug 27, 2022 @graingert yes, your link was very useful. Please, take a look at my proposed soultion :) Reacted by Thomas Grainger and Iván || HacKan cjw296 commented on Jan 3, 2023 AlexWaygood commented on Jan 3, 2023 hauntsaninja commented on Jan 20, 2023 Looks like this has been fixed, thanks all! mdczaplicki commented on Feb 7, 2023 I'm on 3.11.1 and this is happening.
def _signature_from_function(cls, func, skip_bound_arg=True,
globals=None, locals=None, eval_str=False):
"""Private helper: constructs Signature for the given python function."""
is_duck_function = False
if not isfunction(func):
if _signature_is_functionlike(func):
is_duck_function = True
else:
# If it's not a pure Python function, and not a duck type
# of pure function:
raise TypeError('{!r} is not a Python function'.format(func))
s = getattr(func, "__text_signature__", None)
if s:
return _signature_fromstr(cls, func, s, skip_bound_arg)
Parameter = cls._parameter_cls
# Parameter information.
func_code = func.__code__
pos_count = func_code.co_argcount
arg_names = func_code.co_varnames
posonly_count = func_code.co_posonlyargcount
> positional = arg_names[:pos_count]
E TypeError: 'Mock' object is not subscriptabletirkarthi commented on Feb 7, 2023 This commit is present on main branch and 3.12 latest alpha. I guess this is not backported. Reacted by Marek Czaplicki mdczaplicki commented on Feb 7, 2023 Workaround:
from inspect import signature
from unittest.mock import AsyncMock, MagicMock
temp_mock = AsyncMock()
mock = MagicMock(return_value=temp_mock())
signature(mock) arhadthedev commented on Feb 7, 2023 @sobolevn should the PR be backported to 3.11? arhadthedev commented on Feb 8, 2023 arhadthedev commented on Feb 8, 2023 Reacted by Marek Czaplicki Reacted by Marek Czaplicki arhadthedev commented on Feb 8, 2023 AlexWaygood commented on Feb 8, 2023
The fix will be available in 3.11.3 and 3.10.11 this April.
@arhadthedev where is the 3.10 backport? arhadthedev commented on Feb 8, 2023 I confused it with my other issue that was backported to both versions. Thank you for pointing out, edited. bedevere-bot commented on Aug 29, 2023
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|