Skip to content

Commit c84677a

Browse files
committed
bpo-30587: Adds signature checking for mock autospec object method calls
Mock can accept an spec object / class as argument, making sure that accessing attributes that do not exist in the spec will cause an AttributeError to be raised, but there is no guarantee that the spec's methods signatures are respected in any way. This creates the possibility to have faulty code with passing unittests and assertions. Example: from unittest import mock class Something(object): def foo(self, a, b, c, d): pass m = mock.Mock(spec=Something) m.foo() Adds the autospec argument to Mock, and its mock_add_spec method. Passes the spec's attribute with the same name to the child mock (spec-ing the child), if the mock's autospec is True. Sets _mock_check_sig if the given spec is callable. Adds unit tests to validate the fact that the autospec method signatures are respected.
1 parent db3e990 commit c84677a

4 files changed

Lines changed: 84 additions & 8 deletions

File tree

Lib/test/test_unittest/testmock/testasync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ async def test_add_return_value(self):
450450
async def addition(self, var): pass
451451

452452
mock = AsyncMock(addition, return_value=10)
453-
output = await mock(5)
453+
output = await mock(self, 5)
454454

455455
self.assertEqual(output, 10)
456456

Lib/test/test_unittest/testmock/testmock.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,47 @@ def test_only_allowed_methods_exist(self):
668668
getattr, mock, 'something_else'
669669
)
670670

671+
def _check_autospeced_something(self, something):
672+
for method_name in ['meth', 'cmeth', 'smeth']:
673+
mock_method = getattr(something, method_name)
674+
675+
# check that the methods are callable with correct args.
676+
mock_method(sentinel.a, sentinel.b, sentinel.c)
677+
mock_method(sentinel.a, sentinel.b, sentinel.c, d=sentinel.d)
678+
mock_method.assert_has_calls([
679+
call(sentinel.a, sentinel.b, sentinel.c),
680+
call(sentinel.a, sentinel.b, sentinel.c, d=sentinel.d)])
681+
682+
# assert that TypeError is raised if the method signature is not
683+
# respected.
684+
self.assertRaises(TypeError, mock_method)
685+
self.assertRaises(TypeError, mock_method, sentinel.a)
686+
self.assertRaises(TypeError, mock_method, a=sentinel.a)
687+
self.assertRaises(TypeError, mock_method, sentinel.a, sentinel.b,
688+
sentinel.c, e=sentinel.e)
689+
690+
# assert that AttributeError is raised if the method does not exist.
691+
self.assertRaises(AttributeError, getattr, something, 'foolish')
692+
693+
694+
def test_mock_autospec_all_members(self):
695+
for spec in [Something, Something()]:
696+
mock_something = Mock(autospec=spec)
697+
self._check_autospeced_something(mock_something)
698+
699+
700+
def test_mock_spec_function(self):
701+
def foo(lish):
702+
pass
703+
704+
mock_foo = Mock(spec=foo)
705+
706+
mock_foo(sentinel.lish)
707+
mock_foo.assert_called_once_with(sentinel.lish)
708+
self.assertRaises(TypeError, mock_foo)
709+
self.assertRaises(TypeError, mock_foo, sentinel.foo, sentinel.lish)
710+
self.assertRaises(TypeError, mock_foo, foo=sentinel.foo)
711+
671712

672713
def test_from_spec(self):
673714
class Something(object):
@@ -1974,6 +2015,13 @@ def test_mock_add_spec_magic_methods(self):
19742015
self.assertRaises(TypeError, lambda: mock['foo'])
19752016

19762017

2018+
def test_mock_add_spec_autospec_all_members(self):
2019+
for spec in [Something, Something()]:
2020+
mock_something = Mock()
2021+
mock_something.mock_add_spec(spec, autospec=True)
2022+
self._check_autospeced_something(mock_something)
2023+
2024+
19772025
def test_adding_child_mock(self):
19782026
for Klass in (NonCallableMock, Mock, MagicMock, NonCallableMagicMock,
19792027
AsyncMock):

Lib/unittest/mock.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ def checksig(self, /, *args, **kwargs):
137137
type(mock)._mock_check_sig = checksig
138138
type(mock).__signature__ = sig
139139

140+
return sig
141+
140142

141143
def _copy_func_details(func, funcopy):
142144
# we explicitly don't copy func.__dict__ into this copy as it would
@@ -465,7 +467,8 @@ def __new__(
465467
def __init__(
466468
self, spec=None, wraps=None, name=None, spec_set=None,
467469
parent=None, _spec_state=None, _new_name='', _new_parent=None,
468-
_spec_as_instance=False, _eat_self=None, unsafe=False, **kwargs
470+
_spec_as_instance=False, _eat_self=None, unsafe=False,
471+
autospec=None, **kwargs
469472
):
470473
if _new_parent is None:
471474
_new_parent = parent
@@ -476,10 +479,15 @@ def __init__(
476479
__dict__['_mock_new_name'] = _new_name
477480
__dict__['_mock_new_parent'] = _new_parent
478481
__dict__['_mock_sealed'] = False
482+
__dict__['_autospec'] = autospec
479483

480484
if spec_set is not None:
481485
spec = spec_set
482486
spec_set = True
487+
if autospec is not None:
488+
# autospec is even stricter than spec_set.
489+
spec = autospec
490+
autospec = True
483491
if _eat_self is None:
484492
_eat_self = parent is not None
485493

@@ -522,12 +530,18 @@ def attach_mock(self, mock, attribute):
522530
setattr(self, attribute, mock)
523531

524532

525-
def mock_add_spec(self, spec, spec_set=False):
533+
def mock_add_spec(self, spec, spec_set=False, autospec=None):
526534
"""Add a spec to a mock. `spec` can either be an object or a
527535
list of strings. Only attributes on the `spec` can be fetched as
528536
attributes from the mock.
529537
530-
If `spec_set` is True then only attributes on the spec can be set."""
538+
If `spec_set` is True then only attributes on the spec can be set.
539+
If `autospec` is True then only attributes on the spec can be accessed
540+
and set, and if a method in the `spec` is called, it's signature is
541+
checked.
542+
"""
543+
if autospec is not None:
544+
self.__dict__['_autospec'] = autospec
531545
self._mock_add_spec(spec, spec_set)
532546

533547

@@ -545,9 +559,9 @@ def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
545559
_spec_class = spec
546560
else:
547561
_spec_class = type(spec)
548-
res = _get_signature_object(spec,
549-
_spec_as_instance, _eat_self)
550-
_spec_signature = res and res[1]
562+
563+
_spec_signature = _check_signature(spec, self, _eat_self,
564+
_spec_as_instance)
551565

552566
spec_list = dir(spec)
553567

@@ -714,9 +728,20 @@ def __getattr__(self, name):
714728
# execution?
715729
wraps = getattr(self._mock_wraps, name)
716730

731+
kwargs = {}
732+
if self.__dict__.get('_autospec') is not None:
733+
# get the mock's spec attribute with the same name and
734+
# pass it to the child.
735+
spec_class = self.__dict__.get('_spec_class')
736+
spec = getattr(spec_class, name, None)
737+
is_type = isinstance(spec_class, type)
738+
eat_self = _must_skip(spec_class, name, is_type)
739+
kwargs['_eat_self'] = eat_self
740+
kwargs['autospec'] = spec
741+
717742
result = self._get_child_mock(
718743
parent=self, name=name, wraps=wraps, _new_name=name,
719-
_new_parent=self
744+
_new_parent=self, **kwargs
720745
)
721746
self._mock_children[name] = result
722747

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mock: Added autospec argument to the constructors and mock_add_spec. Passing
2+
the autospec argument, will also check the method signatures of the mocked
3+
methods.

0 commit comments

Comments
 (0)