Repository navigation
Calling inspect.signature with an AsyncMock instance raises an exception #96127
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Aug 19, 2022 Related? #94924
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)>- added a commit that references this issue
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@sobolevn - where is your proposed solution?
@sobolevn - where is your proposed solution?
- added a commit that references this issue
on Jan 7, 2023 - added a commit that references this issue
on Jan 7, 2023 Looks like this has been fixed, thanks all!
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 subscriptable
This commit is present on main branch and 3.12 latest alpha. I guess this is not backported.
Reacted by Marek CzaplickiWorkaround:
from inspect import signature from unittest.mock import AsyncMock, MagicMock temp_mock = AsyncMock() mock = MagicMock(return_value=temp_mock()) signature(mock)
@sobolevn should the PR be backported to 3.11?
4 remaining items
- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Feb 7, 2023 @sobolevn ping?
@mdczaplicki @tirkarthi The backporting is done. The fix will be available in 3.11.3
and 3.10.11this April.Reacted by Marek CzaplickiReacted by Marek CzaplickiFixed by gh-96335.
The fix will be available in 3.11.3 and 3.10.11 this April.
@arhadthedev where is the 3.10 backport?
I confused it with my other issue that was backported to both versions. Thank you for pointing out, edited.
Reacted by Alex WaygoodGH-108652 is a backport of this pull request to the 3.10 branch.
Bug report
Starting in Python 3.10.6, the following code raises an exception:
Mockis used.Your environment
Linked PRs
inspect.signaturecall on mocks #96335inspect.signaturecall on mocks (#96335) #101646inspect.signaturecall on mocks (#96335) #101647inspect.signaturecall on mocks (#96335) (gh-96127) #108652