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-102978: Fix mock.patch function signatures for class and staticmethod decorators by tomasr8 · Pull Request #103228 · python/cpython

Conversation

@tomasr8

@tomasr8 tomasr8 commented Apr 3, 2023 •
edited by bedevere-bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Closes #102978

As reported in the original issue, unittest.mock.patch does not correctly extract function signatures from methods decorated with @classmethod and @staticmethod when patch is called with autospec=True.

This fix adds a special case to the extraction logic which uses the original decorated function via __func__ to get the correct signature.

I also updated the relevant tests.

  • Issue: unittest.mock.patch with autospec doesn't hold for classmethods nor staticmethods #102978

Sorry, something went wrong.

Uh oh!

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

All reactions
@tomasr8
tomasr8 requested a review from cjw296 as a code owner April 3, 2023 22:04
@bedevere-bot bedevere-bot added the awaiting review label Apr 3, 2023
@bedevere-bot bedevere-bot mentioned this pull request Apr 3, 2023
unittest.mock.patch with autospec doesn't hold for classmethods nor staticmethods #102978
Closed
@ghost

ghost commented Apr 3, 2023 •
edited by ghost
Loading

Uh oh!

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

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.
CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

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

sobolevn
sobolevn reviewed Apr 4, 2023
View reviewed changes

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/unittest/mock.py Resolved

Uh oh!

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

Comment thread Misc/NEWS.d/next/Library/2023-04-03-23-44-34.gh-issue-102978.gy9eVk.rst Resolved

Uh oh!

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

Comment thread Lib/test/test_unittest/testmock/testpatch.py Resolved

Uh oh!

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

@arhadthedev arhadthedev added the stdlib Standard Library Python modules in the Lib/ directory label Apr 4, 2023
@tomasr8
tomasr8 force-pushed the gh-102978 branch 2 times, most recently from ab55da7 to ade55ef Compare April 4, 2023 10:49
sobolevn
sobolevn reviewed Apr 4, 2023
View reviewed changes

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost done, in my opinion :)

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_unittest/testmock/testhelpers.py Resolved

Uh oh!

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

Comment thread Lib/test/test_unittest/testmock/testhelpers.py Resolved

Uh oh!

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

@tomasr8
tomasr8 force-pushed the gh-102978 branch from ade55ef to bfd49d9 Compare April 4, 2023 11:02
@tomasr8

tomasr8 commented Apr 4, 2023

Copy link
Copy Markdown
Member Author

Thanks! I updated the tests

All reactions

Sorry, something went wrong.

Uh oh!

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

sobolevn
sobolevn reviewed Apr 4, 2023
View reviewed changes

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but you will need an approve from the code owner :)

Sorry, something went wrong.

Uh oh!

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

All reactions
@tomasr8

tomasr8 commented Apr 4, 2023

Copy link
Copy Markdown
Member Author

Awesome, thanks for your time!

sobolevn reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@tomasr8
tomasr8 force-pushed the gh-102978 branch 2 times, most recently from a1b5a6b to 98f562e Compare April 4, 2023 22:04
@tomasr8
Fix mock.patch function signatures for class and staticmethod decorators
361b596
Fixes unittest.mock.patch not enforcing function signatures for methods
decorated with @classmethod or @staticmethod when patch is called with
autospec=True.
@tomasr8
tomasr8 force-pushed the gh-102978 branch from 98f562e to 361b596 Compare April 5, 2023 09:04
cjw296
cjw296 approved these changes Apr 13, 2023
View reviewed changes

@cjw296 cjw296 left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice PR, thanks!

Sorry, something went wrong.

Uh oh!

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

tomasr8 reacted with heart emoji
All reactions
  • ❤️ 1 reaction
Comment thread Lib/test/test_unittest/testmock/testhelpers.py Resolved

Uh oh!

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

Comment thread Lib/test/test_unittest/testmock/testhelpers.py Resolved

Uh oh!

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

Comment thread Lib/test/test_unittest/testmock/testpatch.py Resolved

Uh oh!

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

Comment thread Lib/test/test_unittest/testmock/testpatch.py Resolved

Uh oh!

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

@bedevere-bot bedevere-bot added awaiting merge and removed awaiting review labels Apr 13, 2023
cjw296 added 5 commits April 13, 2023 08:00
@cjw296
Update Lib/test/test_unittest/testmock/testhelpers.py
02d2743
@cjw296
Update Lib/test/test_unittest/testmock/testhelpers.py
a41d46e
@cjw296
Update Lib/test/test_unittest/testmock/testpatch.py
5dce14d
@cjw296
Update Lib/test/test_unittest/testmock/testpatch.py
af8397a
@cjw296
Merge branch 'main' into pythongh-102978
1ec730f
@cjw296 cjw296 added the needs backport to 3.11 only security fixes label Apr 13, 2023
@cjw296
cjw296 merged commit 59e0de4 into python:main Apr 13, 2023
@miss-islington

miss-islington commented Apr 13, 2023

Copy link
Copy Markdown
Contributor

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

@bedevere-bot bedevere-bot removed the awaiting merge label Apr 13, 2023
@bedevere-bot

bedevere-bot commented Apr 13, 2023

Copy link
Copy Markdown

GH-103499 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 Apr 13, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Apr 13, 2023
@tomasr8 @miss-islington
pythongh-102978: Fix mock.patch function signatures for class and sta…
64e3218
…ticmethod decorators (pythonGH-103228)

Fixes unittest.mock.patch not enforcing function signatures for methods
decorated with @classmethod or @staticmethod when patch is called with
autospec=True.
(cherry picked from commit 59e0de4)

Co-authored-by: Tomas R <[email protected]>
@tomasr8
tomasr8 deleted the gh-102978 branch April 13, 2023 10:22
carljm added a commit to carljm/cpython that referenced this pull request Apr 13, 2023
@carljm
Merge branch 'main' into superopt
9384106
* main:
  pythongh-103479: [Enum] require __new__ to be considered a data type (pythonGH-103495)
  pythongh-103365: [Enum] STRICT boundary corrections (pythonGH-103494)
  pythonGH-103488: Use return-offset, not yield-offset. (pythonGH-103502)
  pythongh-103088: Fix test_venv error message to avoid bytes/str warning (pythonGH-103500)
  pythonGH-103082: Turn on branch events for FOR_ITER instructions. (python#103507)
  pythongh-102978: Fix mock.patch function signatures for class and staticmethod decorators (python#103228)
  pythongh-103462: Ensure SelectorSocketTransport.writelines registers a writer when data is still pending (python#103463)
  pythongh-95299: Rework test_cppext.py to not invoke setup.py directly (python#103316)
aisk pushed a commit to aisk/cpython that referenced this pull request Apr 18, 2023
@tomasr8 @aisk
pythongh-102978: Fix mock.patch function signatures for class and sta…
b00ae99
…ticmethod decorators (python#103228)

Fixes unittest.mock.patch not enforcing function signatures for methods
decorated with @classmethod or @staticmethod when patch is called with
autospec=True.
ambv pushed a commit that referenced this pull request May 22, 2023
@miss-islington @tomasr8
[3.11] gh-102978: Fix mock.patch function signatures for class and st…
e95ca78
…aticmethod decorators (GH-103228) (#103499)

Fixes unittest.mock.patch not enforcing function signatures for methods
decorated with @classmethod or @staticmethod when patch is called with
autospec=True.

(cherry picked from commit 59e0de4)

Co-authored-by: Tomas R <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

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

Uh oh!

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

Assignees

No one assigned

Labels

stdlib Standard Library Python modules in the Lib/ directory

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

unittest.mock.patch with autospec doesn't hold for classmethods nor staticmethods

Uh oh!

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

6 participants

@tomasr8 @miss-islington @bedevere-bot @cjw296 @sobolevn @arhadthedev
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.