diff --git a/Lib/test/test_unittest/testmock/testmock.py b/Lib/test/test_unittest/testmock/testmock.py index ffc89dc5ce1562d..52a7a594b19ab58 100644 --- a/Lib/test/test_unittest/testmock/testmock.py +++ b/Lib/test/test_unittest/testmock/testmock.py @@ -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') diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index d3d47ff81e85969..c8cd3d510a18c69 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -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 @@ -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 = [ @@ -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) @@ -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) @@ -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) @@ -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): diff --git a/Misc/NEWS.d/next/Library/2026-12-01-21-26-00.gh-issue-94478.f8K2mP.rst b/Misc/NEWS.d/next/Library/2026-12-01-21-26-00.gh-issue-94478.f8K2mP.rst new file mode 100644 index 000000000000000..08cc9cb02641507 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-12-01-21-26-00.gh-issue-94478.f8K2mP.rst @@ -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.