GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-96127: Fix `inspect.signature` call on mocks by sobolevn · Pull Request #96335 · python/cpython

Conversation

@sobolevn

@sobolevn sobolevn commented Aug 27, 2022 •
edited by AlexWaygood
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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)>
  • Issue: Calling inspect.signature with an AsyncMock instance raises an exception #96127

Related #94050

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@sobolevn
sobolevn requested a review from cjw296 as a code owner August 27, 2022 07:39
@bedevere-bot bedevere-bot added the awaiting review label Aug 27, 2022
@sobolevn

sobolevn commented Aug 27, 2022

Copy link
Copy Markdown
Member Author

Re-triggering CI.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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

graingert commented Sep 12, 2022

Copy link
Copy Markdown
Contributor

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@graingert

graingert commented Sep 12, 2022

Copy link
Copy Markdown
Contributor

#94962 is this related?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sobolevn

sobolevn commented Sep 12, 2022 •
edited by AlexWaygood
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

graingert
graingert reviewed Sep 12, 2022
View reviewed changes
Comment thread Lib/unittest/mock.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@tirkarthi tirkarthi mentioned this pull request Jan 3, 2023
Error when inspecting the signature of AsyncMock() testing-cabal/mock#504
Closed
@AlexWaygood AlexWaygood mentioned this pull request Jan 3, 2023
Calling inspect.signature with an AsyncMock instance raises an exception #96127
Closed
cjw296
cjw296 reviewed Jan 5, 2023
View reviewed changes
Comment thread Lib/unittest/mock.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@cjw296

cjw296 commented Jan 7, 2023

Copy link
Copy Markdown
Contributor

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sobolevn
pythongh-96127: Fix inspect.signature call on mocks
d9c74a3
@sobolevn
sobolevn force-pushed the issue-96127 branch from b1c6e20 to d9c74a3 Compare January 7, 2023 10:25
@sobolevn

sobolevn commented Jan 7, 2023

Copy link
Copy Markdown
Member Author

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 reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cjw296
cjw296 approved these changes Jan 7, 2023
View reviewed changes
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting review labels Jan 7, 2023
@cjw296
cjw296 merged commit 9e7d726 into python:main Jan 7, 2023
@bedevere-bot bedevere-bot removed the awaiting merge label Jan 7, 2023
@zmievsa

zmievsa commented Feb 1, 2023

Copy link
Copy Markdown
Contributor

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

sobolevn reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

miss-islington commented Feb 7, 2023

Copy link
Copy Markdown
Contributor

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington

miss-islington commented Feb 7, 2023

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington miss-islington assigned cjw296 Feb 7, 2023
@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

miss-islington commented Feb 7, 2023

Copy link
Copy Markdown
Contributor

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington

miss-islington commented Feb 7, 2023

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

arhadthedev pushed a commit to arhadthedev/cpython that referenced this pull request Feb 7, 2023
@sobolevn @arhadthedev
pythongh-96127: Fix inspect.signature call on mocks (python#96335)
ca9f3e9
(cherry picked from commit 9e7d726)
arhadthedev pushed a commit to arhadthedev/cpython that referenced this pull request Feb 7, 2023
@sobolevn @arhadthedev
pythongh-96127: Fix inspect.signature call on mocks (python#96335)
01d94ca
(cherry picked from commit 9e7d726)
@bedevere-bot

bedevere-bot commented Feb 7, 2023

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

bedevere-bot commented Feb 7, 2023

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ambv pushed a commit that referenced this pull request Feb 8, 2023
@arhadthedev @sobolevn
[3.11] gh-96127: Fix inspect.signature call on mocks (#96335) (#101646
efcab38
)

(cherry picked from commit 9e7d726)

Co-authored-by: Nikita Sobolev <[email protected]>
thechrisu pushed a commit to thechrisu/cpython that referenced this pull request Aug 29, 2023
@sobolevn Christoph Ulshoefer
pythongh-96127: Fix inspect.signature call on mocks (python#96335)
ac3aad4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@graingert graingert graingert left review comments
@cjw296 cjw296 cjw296 approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@cjw296 cjw296

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@sobolevn @graingert @cjw296 @zmievsa @miss-islington @bedevere-bot @AlexWaygood
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.