Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when inspecting the signature of AsyncMock() #504

Closed
peter-slovak opened this issue Jan 2, 2023 · 6 comments
Closed

Error when inspecting the signature of AsyncMock() #504

peter-slovak opened this issue Jan 2, 2023 · 6 comments

Comments

@peter-slovak
Copy link

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 :-)

@cjw296
Copy link
Collaborator

cjw296 commented Jan 3, 2023

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
Copy link
Contributor

CPython issue : python/cpython#96127

@cjw296
Copy link
Collaborator

cjw296 commented Jan 3, 2023

@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...

@tirkarthi
Copy link
Contributor

tirkarthi commented Jan 3, 2023

@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.

@cjw296
Copy link
Collaborator

cjw296 commented Jan 5, 2023

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

@cjw296 cjw296 closed this as completed Jan 5, 2023
@cjw296
Copy link
Collaborator

cjw296 commented Jan 9, 2023

Release 5.0.1 will hopefully fix this...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants