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
86 changes: 77 additions & 9 deletions Lib/test/test_unittest/testmock/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2402,20 +2402,88 @@ 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:
patched.assret_called_once()
with patch.object(
Foo, 'one', unsafe=True, autospect=True
) as patched:
patched.assret_called_once()
with patch(
f'{__name__}.Something.meth', unsafe=True, auto_spec=True
) as patched:
patched.assret_called_once()
with patch.object(
Foo, 'one', unsafe=True, auto_spec=True
) as patched:
patched.assret_called_once()
with patch(
f'{__name__}.Something.meth', unsafe=True, set_spec=True
) as patched:
patched.assret_called_once()
with patch.object(
Foo, 'one', unsafe=True, set_spec=True
) as patched:
patched.assret_called_once()
m = create_autospec(Foo, set_spec=True, unsafe=True)
self.assertTrue(m._mock_unsafe)
self.assertTrue(m()._mock_unsafe)
with patch.multiple(
f'{__name__}.Typos', autospect=True, set_spec=True, auto_spec=True):
pass

def test_patch_and_autospec_propagate_unsafe(self):
class Foo:
one = 'one'
prop = property(lambda self: self.one)

with patch(f'{__name__}.Something.meth') as m:
with self.assertRaisesRegex(AttributeError, "not a valid assertion"):
m.assret_called_once()

with patch(f'{__name__}.Something.meth', unsafe=True) as m:
m.assret_called_once()

with patch.object(Foo, 'one', unsafe=True) as m:
m.assret_called_once()

with patch(f'{__name__}.Something', spec=True, unsafe=True) as m:
self.assertTrue(m._mock_unsafe)
self.assertTrue(m()._mock_unsafe)

with patch(f'{__name__}.Something', autospec=True, unsafe=True) as m:
self.assertTrue(m._mock_unsafe)
self.assertTrue(m()._mock_unsafe)

with patch(f'{__name__}.Something', autospec=True, unsafe=True,
autospect=True) as m:
self.assertTrue(m._mock_unsafe)
self.assertTrue(m()._mock_unsafe)

for spec in (None, Foo.prop):
with self.subTest(spec=spec):
m_default = create_autospec(spec)
with self.assertRaisesRegex(AttributeError, "not a valid assertion"):
m_default.assret_called_once()
m_unsafe = create_autospec(spec, unsafe=True)
m_unsafe.assret_called_once()

# Class decoration copies the _patcher and must preserve unsafe=True
@patch(f'{__name__}.Something.meth', unsafe=True, autospect=True)
class TestDecorated:
def test_method(self, patched):
patched.assret_called_once()

TestDecorated().test_method()

# unsafe=True must not leak into non-Mock new_callable or new=...
sentinel_obj = object()
with patch(f'{__name__}.Something.meth', new=sentinel_obj, unsafe=True) as m:
self.assertIs(m, sentinel_obj)
with patch(f'{__name__}.Something.meth', new_callable=dict, unsafe=True) as m:
self.assertEqual(m, {})

def test_property_not_called_with_spec_mock(self):
obj = SomethingElse()
self.assertIsNone(obj._instance, msg='before mock')
Expand Down
21 changes: 13 additions & 8 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,7 @@ def __init__(
self.spec_set = spec_set
self.autospec = autospec
self.kwargs = kwargs
self.unsafe = unsafe
self.additional_patchers = []
self.is_started = False

Expand All @@ -1377,7 +1378,8 @@ def copy(self):
patcher = _patch(
self.getter, self.attribute, self.new, self.spec,
self.create, self.spec_set,
self.autospec, self.new_callable, self.kwargs
self.autospec, self.new_callable, self.kwargs,
unsafe=self.unsafe
)
patcher.attribute_name = self.attribute_name
patcher.additional_patchers = [
Expand Down Expand Up @@ -1561,10 +1563,12 @@ def __enter__(self):
if spec_set is not None:
_kwargs['spec_set'] = spec_set

# add a name to mocks
if (isinstance(Klass, type) and
issubclass(Klass, NonCallableMock) and self.attribute):
_kwargs['name'] = self.attribute
# add a name and unsafe flag to mocks
if isinstance(Klass, type) and issubclass(Klass, NonCallableMock):
if self.attribute:
_kwargs['name'] = self.attribute
if self.unsafe:
_kwargs['unsafe'] = self.unsafe

_kwargs.update(kwargs)
new = Klass(**_kwargs)
Expand Down Expand Up @@ -1610,7 +1614,8 @@ def __enter__(self):
f'[target={self.target!r}, attr={autospec!r}]')

new = create_autospec(autospec, spec_set=spec_set,
_name=self.attribute, **kwargs)
_name=self.attribute, unsafe=self.unsafe,
**kwargs)
elif kwargs:
# can't set keyword args when we aren't creating the mock
# XXXX If new is a Mock we could call new.configure_mock(**kwargs)
Expand Down Expand Up @@ -2818,7 +2823,7 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
Klass = NonCallableMagicMock

mock = Klass(parent=_parent, _new_parent=_parent, _new_name=_new_name,
name=_name, **_kwargs)
name=_name, unsafe=unsafe, **_kwargs)
if is_dataclass_spec:
mock._mock_extend_spec_methods(dataclass_spec_list)

Expand All @@ -2840,7 +2845,7 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
if is_type and not instance and 'return_value' not in kwargs:
mock.return_value = create_autospec(spec, spec_set, instance=True,
_name='()', _parent=mock,
wraps=wrapped)
wraps=wrapped, unsafe=unsafe)

for entry, original in entries:
if _is_magic(entry):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :func:`unittest.mock.patch`, :func:`unittest.mock.patch.object`, and
:func:`unittest.mock.create_autospec` so that ``unsafe=True`` is propagated to
the created mocks.
Loading