One function

_get_signature_object in testing-cabal/mock

The author described this change as gh-102978: Fix mock.patch function signatures for class and staticmethod decorators (#103228). It counts as a record because the check below fails on the code as it stood at 1c7bfeff6 and passes on 079655cd9, with nothing else changed between the two runs.

Fix saved2023-04-16
Sharing licenceBSD-2-Clause · LICENSE.txt
Change size+6 0

What the code was meant to do, written into the code itself as a docstring

Given an arbitrary, possibly callable object, try to create a suitable signature object. Return a (reduced func, signature) tuple, or None.

The change

99 func = func.__init__
1010 # Skip the `self` argument in __init__
1111 eat_self = True
12+ elif isinstance(func, (classmethod, staticmethod)):
13+ if isinstance(func, classmethod):
14+ # Skip the `cls` argument of a class method
15+ eat_self = True
16+ # Use the original decorated method to extract the correct function signature
17+ func = func.__func__
1218 elif not isinstance(func, FunctionTypes):
1319 # If we really want to model an instance of the passed type,
1420 # __call__ should be looked up, not __init__.

The check that tells the two apart

failpass·mock/tests/testhelpers.py::SpecSignatureTest::test_autospec_signature_classmethod

Check file mock/tests/testhelpers.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before it1c7bfeff6575a5fee073bb648126780e00923936
Broken version dated2023-04-16
Modulemock.mock
Units changed_get_signature_object
Fingerprintfd28fbed176f0955
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in testing-cabal/mock