Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
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 bazYour environment
- CPython versions tested on: 3.10, 3.11
- Operating system and architecture: macOS 12.6 with Apple M1 chip