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 — unittest.mock.patch with autospec doesn't hold for classmethods nor staticmethods · Issue #102978 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
#103228
Closed
unittest.mock.patch with autospec doesn't hold for classmethods nor staticmethods#102978
#103228
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@rileyjohngibbs
rileyjohngibbs
opened on Mar 23, 2023
Issue body actions

Bug report

Using patch with autospec=True does not work when a method is decorated with @classmethod or @staticmethod. The resulting mock can be called with any arguments without raising a TypeError.

Example:

from unittest.mock import patch
import pytest

class Foo:
    def foo(self):
        pass
    @staticmethod
    def bar():
        pass
    @classmethod
    def baz(cls):
        pass

@pytest.mark.parametrize("method", ["foo", "bar", "baz"])
def test_foo(method: str):
    with patch.object(Foo, method, autospec=True):
        getattr(Foo(), method)(5)

The only subtest that fails is foo. The other two pass, even though they're clearly being called incorrectly.

If you prefer not to use pytest to demo/repro this:

with patch.object(Foo, "foo", autospec=True):
    try:
        Foo().foo(5)
    except TypeError:
        print("Correctly raises on foo")
    else:
        print("Incorrectly does not raise with foo")

with patch.object(Foo, "bar", autospec=True):
    try:
        Foo().bar(5)
    except TypeError:
        print("Correctly raises on bar")
    else:
        print("Incorrectly does not raise with bar")

with patch.object(Foo, "baz", autospec=True):
    try:
        Foo().baz(5)
    except TypeError:
        print("Correctly raises on baz")
    else:
        print("Incorrectly does not raise with baz")

This has output:

Correctly raises on foo
Incorrectly does not raise with bar
Incorrectly does not raise with baz

Your environment

  • CPython versions tested on: 3.10, 3.11
  • Operating system and architecture: macOS 12.6 with Apple M1 chip

Linked PRs

  • gh-102978: Fix mock.patch function signatures for class and staticmethod decorators #103228
  • [3.11] gh-102978: Fix mock.patch function signatures for class and staticmethod decorators (GH-103228) #103499

Activity

  1. rileyjohngibbs
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Mar 23, 2023
  2. arhadthedev
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Apr 1, 2023
  3. tomasr8 commented on Apr 3, 2023

    @tomasr8
    tomasr8
    on Apr 3, 2023
    Member
    More actions

    Looks like the signature is not set for @classmethod and @staticmethod. Adding a special case for them and using __func__ to get the correct signature I was able to get it to work. I'll see if I can contribute a PR that fixes this.

    Reacted by Riley Gibbs
  4. cjw296
    added a commit that references this issue on Apr 13, 2023

    Merge branch 'main' into pythongh-102978

    1ec730f
  5. cjw296
    closed this as completedon Apr 13, 2023
  6. arhadthedev commented on Apr 13, 2023

    @arhadthedev
    arhadthedev
    on Apr 13, 2023
    Member
    More actions

    @cjw296 Can this issue be considered completed? The child PR gh-103228 is neither merged nor backported yet.

  7. miss-islington
    added a commit that references this issue on Apr 13, 2023

    pythongh-102978: Fix mock.patch function signatures for class and sta…

    64e3218
  8. cjw296
    added a commit that references this issue on Apr 13, 2023

    gh-102978: Fix mock.patch function signatures for class and staticmet…

    59e0de4
  9. carljm
    added a commit that references this issue on Apr 13, 2023

    Merge branch 'main' into superopt

    9384106
  10. aisk
    added a commit that references this issue on Apr 18, 2023

    pythongh-102978: Fix mock.patch function signatures for class and sta…

    b00ae99
  11. ambv
    added a commit that references this issue on May 22, 2023

    [3.11] gh-102978: Fix mock.patch function signatures for class and st…

    e95ca78
  12. jscarfo11
    added 2 commits that reference this issue on Jan 13, 2025

    Fix 'raise_input_error' inline with the bugfix in python/cpython#102978

    c11feab

    Fix 'raise_input_error' inline with the bugfix in python/cpython#102978

    c7b2518
  13. jhcole
    added 2 commits that reference this issue on Jan 22, 2025

    Fix 'raise_input_error' inline with the bugfix in python/cpython#102978

    cb17971

    Fix 'raise_input_error' inline with the bugfix in python/cpython#102978…

    956735b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.