summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/mock.py
diff options
context:
space:
mode:
authorTomas R <tomas.roun8@gmail.com>2023-04-13 07:37:57 (GMT)
committerGitHub <noreply@github.com>2023-04-13 07:37:57 (GMT)
commit59e0de4903c02e72b329e505fddf1ad9794928bc (patch)
tree25631df423f943fc427f277eda233af8ccdfa87e /Lib/unittest/mock.py
parent19d2639d1e6478e2e251479d842bdfa2e8272396 (diff)
downloadcpython-59e0de4903c02e72b329e505fddf1ad9794928bc.zip
cpython-59e0de4903c02e72b329e505fddf1ad9794928bc.tar.gz
cpython-59e0de4903c02e72b329e505fddf1ad9794928bc.tar.bz2
gh-102978: Fix mock.patch function signatures for class and staticmethod decorators (#103228)
Fixes unittest.mock.patch not enforcing function signatures for methods decorated with @classmethod or @staticmethod when patch is called with autospec=True.
Diffstat (limited to 'Lib/unittest/mock.py')
-rw-r--r--Lib/unittest/mock.py6
1 files changed, 6 insertions, 0 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 0f93cb5..7ca0857 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -98,6 +98,12 @@ def _get_signature_object(func, as_instance, eat_self):
func = func.__init__
# Skip the `self` argument in __init__
eat_self = True
+ elif isinstance(func, (classmethod, staticmethod)):
+ if isinstance(func, classmethod):
+ # Skip the `cls` argument of a class method
+ eat_self = True
+ # Use the original decorated method to extract the correct function signature
+ func = func.__func__
elif not isinstance(func, FunctionTypes):
# If we really want to model an instance of the passed type,
# __call__ should be looked up, not __init__.