Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions Lib/test/test_unittest/testmock/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down