testing-cabal / mock

The Python mock library

Home Page:https://docs.python.org/dev/library/unittest.mock.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error when inspecting the signature of AsyncMock()

peter-slovak opened this issue · comments

What versions are you using?

  • Python: 3.10.0
  • Mock: 5.0.0
  • Operating System: Linux

What happened?
Upgrading from mock==4.0.3 to mock==5.0.0 introduced an error while inspecting the signature of an AsyncMock() coroutine function. I noticed some AsyncMock()-related changes in the changelog, so I'm not sure whether this is the desired behavior, but AsyncMock() docs state that this should return a coroutine-function-like object which should be inspectable.

mock==4.0.3:

Python 3.10.0 (default, Nov  7 2022, 15:29:24) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect, unittest.mock, mock
>>> f = unittest.mock.AsyncMock()
>>> inspect.signature(f)
<Signature (*args, **kwargs)>
>>> f = mock.AsyncMock()
>>> inspect.signature(f)
<Signature (*args, **kwargs)>

mock==5.0.0:

Python 3.10.0 (default, Nov  7 2022, 15:29:24) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect, unittest.mock, mock
>>> f = unittest.mock.AsyncMock()
>>> inspect.signature(f)
<Signature (*args, **kwargs)>
>>> f = mock.AsyncMock()
>>> inspect.signature(f)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/.../.pyenv/versions/3.10.0/lib/python3.10/inspect.py", line 3245, in signature
    return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
  File "/home/.../.pyenv/versions/3.10.0/lib/python3.10/inspect.py", line 2993, in from_callable
    return _signature_from_callable(obj, sigcls=cls,
  File "/home/.../.pyenv/versions/3.10.0/lib/python3.10/inspect.py", line 2454, in _signature_from_callable
    return _signature_from_function(sigcls, obj,
  File "/home/.../.pyenv/versions/3.10.0/lib/python3.10/inspect.py", line 2298, in _signature_from_function
    positional = arg_names[:pos_count]
TypeError: 'Mock' object is not subscriptable

Thank you for your time :-)

This package is a rolling backport of unittest.mock.
As such, any problems you encounter most likely need to be fixed upstream.

Please can you try and reproduce the problem on the latest release of Python 3, including alphas, and replace any import from mock with ones from unittest.mock.

If the issue still occurs, then please report upstream through https://github.com/python/cpython/issues as it will need to be fixed there so that it can be backported here and released to you.

If not, reply with what you find out and we can re-open this issue.

@tirkarthi - I wonder if python/cpython#84753 ends up being a problem here? I remember the commit having changes to both inspect and mock, but only brought the changes back from Mock...

@cjw296 I guess python/cpython#84753 is more of an issue with iscoroutinefunction and this is related to signature. There is an open PR (python/cpython#96335) for the issue I linked that could be backported here once merged. I guess this has to be fixed upstream.

Yep, agreed, this is an upstread issue, so let's track on python/cpython#96127

Release 5.0.1 will hopefully fix this...