Skip to content

gh-136179: Document unsafe keyword for patch, patch.object, and create_autospec #136208

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
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
14 changes: 11 additions & 3 deletions Doc/library/unittest.mock.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ patch

The key is to do the patching in the right namespace. See the section `where to patch`_.

.. function:: patch(target, new=DEFAULT, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs)
.. function:: patch(target, new=DEFAULT, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, *, unsafe=False, **kwargs)

:func:`patch` acts as a function decorator, class decorator or a context
manager. Inside the body of the function or with statement, the *target*
Expand Down Expand Up @@ -1485,6 +1485,10 @@ patch
By default this is ``'test'``, which matches the way :mod:`unittest` finds tests.
You can specify an alternative prefix by setting ``patch.TEST_PREFIX``.

Patch will raise a :exc:`RuntimeError` if passed some common misspellings of
the arguments autospec and spec_set. Pass the argument *unsafe* with the
value ``True`` to disable that check.

Patch can be used as a context manager, with the with statement. Here the
patching applies to the indented block after the with statement. If you
use "as" then the patched object will be bound to the name after the
Expand Down Expand Up @@ -1622,7 +1626,7 @@ work as expected::
patch.object
~~~~~~~~~~~~

.. function:: patch.object(target, attribute, new=DEFAULT, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs)
.. function:: patch.object(target, attribute, new=DEFAULT, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, *, unsafe=False, **kwargs)

patch the named member (*attribute*) on an object (*target*) with a mock
object.
Expand Down Expand Up @@ -2404,7 +2408,7 @@ arguments are a dictionary:
create_autospec
~~~~~~~~~~~~~~~

.. function:: create_autospec(spec, spec_set=False, instance=False, **kwargs)
.. function:: create_autospec(spec, spec_set=False, instance=False, *, unsafe=False, **kwargs)

Create a mock object using another object as a spec. Attributes on the
mock will use the corresponding attribute on the *spec* object as their
Expand All @@ -2421,6 +2425,10 @@ create_autospec
spec for an instance object by passing ``instance=True``. The returned mock
will only be callable if instances of the mock are callable.

:func:`create_autospec` will raise a :exc:`RuntimeError` if passed some common
misspellings of the arguments autospec and spec_set. Pass the argument
*unsafe* with the value ``True`` to disable that check.

:func:`create_autospec` also takes arbitrary keyword arguments that are passed to
the constructor of the created mock.

Expand Down
Loading