From b7f50dbc9509fb936ef40ea7d5f16a02fce26c7c Mon Sep 17 00:00:00 2001 From: Leonardus Chen Date: Wed, 2 Jul 2025 21:07:15 +0700 Subject: [PATCH 1/3] Document unsafe keyword for patch, patch.object, and create_autospec --- Doc/library/unittest.mock.rst | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index 091562cc9aef98..a38c99d4f42a72 100644 --- a/Doc/library/unittest.mock.rst +++ b/Doc/library/unittest.mock.rst @@ -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* @@ -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 `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 @@ -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. @@ -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 @@ -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. + `create_autospec` will raise a `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. From 6385075dfe8b8bc274236ed8e1dc3b14973482b8 Mon Sep 17 00:00:00 2001 From: Leonardus Chen Date: Wed, 2 Jul 2025 21:14:11 +0700 Subject: [PATCH 2/3] Fix linting --- Doc/library/unittest.mock.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index a38c99d4f42a72..7e65a74f5376dc 100644 --- a/Doc/library/unittest.mock.rst +++ b/Doc/library/unittest.mock.rst @@ -1485,8 +1485,8 @@ 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 `RuntimeError` if passed some common misspellings of - the arguments autospec and spec_set. Pass the argument `unsafe` with the + 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 @@ -2425,7 +2425,7 @@ 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. - `create_autospec` will raise a `RuntimeError` if passed some common + :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. From ea2db27fa1a2aec78d48911a661302618bbba13c Mon Sep 17 00:00:00 2001 From: Leonardus Chen Date: Wed, 2 Jul 2025 21:16:50 +0700 Subject: [PATCH 3/3] Fix linting again --- Doc/library/unittest.mock.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index 7e65a74f5376dc..20ce709c9c1da8 100644 --- a/Doc/library/unittest.mock.rst +++ b/Doc/library/unittest.mock.rst @@ -1487,7 +1487,7 @@ patch 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. + 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 @@ -2427,7 +2427,7 @@ create_autospec :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. + *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.