Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Closed
Labels
stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
@rileyjohngibbs

Description

@rileyjohngibbs

Bug report

Usingpatch withautospec=True does not work when a method is decorated with@classmethod or@staticmethod. The resulting mock can be called with any arguments without raising aTypeError.

Example:

fromunittest.mockimportpatchimportpytestclassFoo:deffoo(self):pass@staticmethoddefbar():pass@classmethoddefbaz(cls):pass@pytest.mark.parametrize("method", ["foo","bar","baz"])deftest_foo(method:str):withpatch.object(Foo,method,autospec=True):getattr(Foo(),method)(5)

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

If you prefer not to usepytest to demo/repro this:

withpatch.object(Foo,"foo",autospec=True):try:Foo().foo(5)exceptTypeError:print("Correctly raises on foo")else:print("Incorrectly does not raise with foo")withpatch.object(Foo,"bar",autospec=True):try:Foo().bar(5)exceptTypeError:print("Correctly raises on bar")else:print("Incorrectly does not raise with bar")withpatch.object(Foo,"baz",autospec=True):try:Foo().baz(5)exceptTypeError:print("Correctly raises on baz")else:print("Incorrectly does not raise with baz")

This has output:

Correctly raises on fooIncorrectly does not raise with barIncorrectly 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

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp