Skip to content

gh-94478: Fix bug unsafe not set when patching #94479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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