diff --git a/Lib/test/test_unittest/testmock/testmock.py b/Lib/test/test_unittest/testmock/testmock.py index 8a92490137f9a7..5434d7a62e4f90 100644 --- a/Lib/test/test_unittest/testmock/testmock.py +++ b/Lib/test/test_unittest/testmock/testmock.py @@ -2214,8 +2214,9 @@ def __init__(self): def test_misspelled_arguments(self): class Foo(): one = 'one' + assret_testing = False # patch, patch.object and create_autospec need to check for misspelled - # arguments explicitly and throw a RuntimError if found. + # arguments explicitly and throw a RuntimeError if found. with self.assertRaises(RuntimeError): with patch(f'{__name__}.Something.meth', autospect=True): pass with self.assertRaises(RuntimeError): @@ -2243,16 +2244,39 @@ class Foo(): with patch.multiple( f'{__name__}.Something', meth=DEFAULT, set_spec=True): pass - with patch(f'{__name__}.Something.meth', unsafe=True, autospect=True): - pass - with patch.object(Foo, 'one', unsafe=True, autospect=True): pass - with patch(f'{__name__}.Something.meth', unsafe=True, auto_spec=True): - pass - with patch.object(Foo, 'one', unsafe=True, auto_spec=True): pass - with patch(f'{__name__}.Something.meth', unsafe=True, set_spec=True): - pass - with patch.object(Foo, 'one', unsafe=True, set_spec=True): pass + with patch( + f'{__name__}.Something.meth', unsafe=True, autospect=True + ) as patched_object: + patched_object.assret_called_once() + + with patch.object( + Foo, 'one', unsafe=True, autospect=True + ) as patched_object: + patched_object.assret_called_once() + + with patch( + f'{__name__}.Something.meth', unsafe=True, auto_spec=True + ) as patched_object: + patched_object.assret_called_once() + + with patch.object( + Foo, 'one', unsafe=True, auto_spec=True + ) as patched_object: + patched_object.assret_called_once() + + with patch( + f'{__name__}.Something.meth', unsafe=True, set_spec=True + ) as patched_object: + patched_object.assret_called_once() + + with patch.object( + Foo, 'one', unsafe=True, set_spec=True + ) as patched_object: + patched_object.assret_called_once() + m = create_autospec(Foo, set_spec=True, unsafe=True) + m.assret_testing = True + with patch.multiple( f'{__name__}.Typos', autospect=True, set_spec=True, auto_spec=True): pass diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index cd46fea5162aba..f316c665cba4f8 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -1282,6 +1282,8 @@ def __init__( f'Cannot spec attr {attribute!r} as the spec_set ' f'target has already been mocked out. [spec_set={spec_set!r}]') + if unsafe: + kwargs['unsafe'] = unsafe self.getter = getter self.attribute = attribute self.new = new @@ -2652,6 +2654,8 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None, _check_spec_arg_typos(kwargs) _kwargs.update(kwargs) + if unsafe: + _kwargs['unsafe'] = unsafe Klass = MagicMock if inspect.isdatadescriptor(spec):