Skip to content

Commit

Permalink
pythongh-102978: Fix mock.patch function signatures for class and sta…
Browse files Browse the repository at this point in the history
…ticmethod decorators (pythonGH-103228)

Fixes unittest.mock.patch not enforcing function signatures for methods
decorated with @classmethod or @staticmethod when patch is called with
autospec=True.
(cherry picked from commit 59e0de4)

Co-authored-by: Tomas R <tomas.roun8@gmail.com>
  • Loading branch information
tomasr8 authored and miss-islington committed Apr 13, 2023
1 parent e643412 commit 64e3218
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__.
Expand Down
18 changes: 18 additions & 0 deletions Lib/unittest/test/testmock/testhelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,24 @@ def __getattr__(self, attribute):
self.assertFalse(hasattr(autospec, '__name__'))


def test_autospec_signature_staticmethod(self):
class Foo:
@staticmethod
def static_method(a, b=10, *, c): pass

mock = create_autospec(Foo.__dict__['static_method'])
self.assertEqual(inspect.signature(Foo.static_method), inspect.signature(mock))


def test_autospec_signature_classmethod(self):
class Foo:
@classmethod
def class_method(cls, a, b=10, *, c): pass

mock = create_autospec(Foo.__dict__['class_method'])
self.assertEqual(inspect.signature(Foo.class_method), inspect.signature(mock))


def test_spec_inspect_signature(self):

def myfunc(x, y): pass
Expand Down
30 changes: 30 additions & 0 deletions Lib/unittest/test/testmock/testpatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,36 @@ def test_autospec_classmethod(self):
method.assert_called_once_with()


def test_autospec_staticmethod_signature(self):
# Patched methods which are decorated with @staticmethod should have the same signature
class Foo:
@staticmethod
def static_method(a, b=10, *, c): pass

Foo.static_method(1, 2, c=3)

with patch.object(Foo, 'static_method', autospec=True) as method:
method(1, 2, c=3)
self.assertRaises(TypeError, method)
self.assertRaises(TypeError, method, 1)
self.assertRaises(TypeError, method, 1, 2, 3, c=4)


def test_autospec_classmethod_signature(self):
# Patched methods which are decorated with @classmethod should have the same signature
class Foo:
@classmethod
def class_method(cls, a, b=10, *, c): pass

Foo.class_method(1, 2, c=3)

with patch.object(Foo, 'class_method', autospec=True) as method:
method(1, 2, c=3)
self.assertRaises(TypeError, method)
self.assertRaises(TypeError, method, 1)
self.assertRaises(TypeError, method, 1, 2, 3, c=4)


def test_autospec_with_new(self):
patcher = patch('%s.function' % __name__, new=3, autospec=True)
self.assertRaises(TypeError, patcher.start)
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,7 @@ Hugo van Rossum
Saskia van Rossum
Robin Roth
Clement Rouault
Tomas Roun
Donald Wallace Rouse II
Liam Routt
Todd Rovito
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixes :func:`unittest.mock.patch` not enforcing function signatures for methods
decorated with ``@classmethod`` or ``@staticmethod`` when patch is called with
``autospec=True``.

0 comments on commit 64e3218

Please sign in to comment.