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

gh-96127: Fix inspect.signature call on mocks #96335

Merged
merged 1 commit into from
Jan 7, 2023

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Aug 27, 2022

Before:

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

After:

>>> from inspect import signature
>>> from unittest.mock import AsyncMock
>>> 
>>> mock = AsyncMock()
>>> signature(mock)
<Signature (*args, **kwargs)>

Related #94050

@sobolevn
Copy link
Member Author

Re-triggering CI.

@sobolevn sobolevn closed this Aug 27, 2022
@sobolevn sobolevn reopened this Aug 27, 2022
@sobolevn sobolevn requested a review from graingert September 12, 2022 08:49
@graingert
Copy link
Contributor

I have a similar PR somewhere about this, but I'm on mobile so it's tricky to find

@graingert
Copy link
Contributor

#94962 is this related?

@sobolevn
Copy link
Member Author

sobolevn commented Sep 12, 2022

They are not related as far as I can tell :)

Your PR does not fix this exact problem:

gh pr checkout 94962
Python 3.12.0a0 (heads/main-dirty:a36235d5c7, Sep 11 2022, 12:23:24) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin
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 "/Users/sobolev/Desktop/cpython/Lib/inspect.py", line 3272, in signature
    return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sobolev/Desktop/cpython/Lib/inspect.py", line 3020, in from_callable
    return _signature_from_callable(obj, sigcls=cls,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sobolev/Desktop/cpython/Lib/inspect.py", line 2507, in _signature_from_callable
    return _signature_from_function(sigcls, obj,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sobolev/Desktop/cpython/Lib/inspect.py", line 2351, in _signature_from_function
    positional = arg_names[:pos_count]
                 ~~~~~~~~~^^^^^^^^^^^^
TypeError: 'Mock' object is not subscriptable

@cjw296
Copy link
Contributor

cjw296 commented Jan 7, 2023

@sobolevn - could you rebase and resolve the conflicts? I think this is then good to land after another quick eyeball from a reviewer...

@sobolevn
Copy link
Member Author

sobolevn commented Jan 7, 2023

Sure!

  1. I've checked that this exception still happens on main with all the new changes (it does)
  2. I've rebased my changes on top of main
  3. Force pushed! 🎉

I hope we can land this now!

@zmievsa
Copy link
Contributor

zmievsa commented Feb 1, 2023

@sobolevn this fix saved me big time. Thank you for your amazing work!

@sobolevn sobolevn added the needs backport to 3.11 only security fixes label Feb 7, 2023
@miss-islington
Copy link
Contributor

Thanks @sobolevn for the PR, and @cjw296 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry @sobolevn and @cjw296, I had trouble checking out the 3.11 backport branch.
Please retry by removing and re-adding the "needs backport to 3.11" label.
Alternatively, you can backport using cherry_picker on the command line.
cherry_picker 9e7d7266ecdcccc02385fe4ccb094f3444102e26 3.11

@AlexWaygood AlexWaygood added needs backport to 3.11 only security fixes and removed needs backport to 3.11 only security fixes labels Feb 7, 2023
@miss-islington
Copy link
Contributor

Thanks @sobolevn for the PR, and @cjw296 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry, @sobolevn and @cjw296, I could not cleanly backport this to 3.11 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 9e7d7266ecdcccc02385fe4ccb094f3444102e26 3.11

arhadthedev pushed a commit to arhadthedev/cpython that referenced this pull request Feb 7, 2023
arhadthedev pushed a commit to arhadthedev/cpython that referenced this pull request Feb 7, 2023
@bedevere-bot
Copy link

GH-101646 is a backport of this pull request to the 3.11 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label Feb 7, 2023
@bedevere-bot
Copy link

GH-101647 is a backport of this pull request to the 3.10 branch.

ambv pushed a commit that referenced this pull request Feb 8, 2023
)

(cherry picked from commit 9e7d726)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
thechrisu pushed a commit to thechrisu/cpython that referenced this pull request Aug 29, 2023
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

Successfully merging this pull request may close these issues.

7 participants