◐ Shell
reader mode source ↗
Skip to content

gh-102978: Fix mock.patch function signatures for class and staticmethod decorators#103228

Merged
cjw296 merged 6 commits into
python:mainfrom
tomasr8:gh-102978
Apr 13, 2023
Merged

gh-102978: Fix mock.patch function signatures for class and staticmethod decorators#103228
cjw296 merged 6 commits into
python:mainfrom
tomasr8:gh-102978

Conversation

@tomasr8

@tomasr8 tomasr8 commented Apr 3, 2023

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.

@ghost

ghost commented Apr 3, 2023

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.
CLA signed

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hide comment

Thanks!

@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 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hide comment

Almost done, in my opinion :)

@tomasr8

tomasr8 commented Apr 4, 2023

Copy link
Copy Markdown
Member Author

Thanks! I updated the tests

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hide comment

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

@tomasr8

tomasr8 commented Apr 4, 2023

Copy link
Copy Markdown
Member Author

Awesome, thanks for your time!

@tomasr8 tomasr8 force-pushed the gh-102978 branch 2 times, most recently from a1b5a6b to 98f562e Compare April 4, 2023 22:04
Fixes unittest.mock.patch not enforcing function signatures for methods
decorated with @classmethod or @staticmethod when patch is called with
autospec=True.

@cjw296 cjw296 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hide comment

Really nice PR, thanks!

@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

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.
🐍🍒⛏🤖

@bedevere-bot

Copy link
Copy Markdown

GH-103499 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 Apr 13, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Apr 13, 2023
…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 <tomas.roun8@gmail.com>
@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
* 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
…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
…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 <tomas.roun8@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stdlib Standard Library Python modules in the Lib/ directory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

6 participants