mirror of
https://github.com/python/cpython.git
synced 2025-11-11 19:12:05 +00:00
[3.11] gh-102978: Fix mock.patch function signatures for class and staticmethod decorators (GH-103228) (#103499)
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 59e0de4903)
Co-authored-by: Tomas R <tomas.roun8@gmail.com>
This commit is contained in:
parent
1692a16a25
commit
e95ca78fab
5 changed files with 58 additions and 0 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue