[Python-checkins] cpython: Minor unittest.mock.patch doc / docstring improvement

michael.foord python-checkins at python.org
Wed Mar 28 16:08:05 CEST 2012


http://hg.python.org/cpython/rev/cd83fe4e3191
changeset: 75972:cd83fe4e3191
user: Michael Foord <michael at voidspace.org.uk>
date: Wed Mar 28 15:08:08 2012 +0100
summary:
 Minor unittest.mock.patch doc / docstring improvement
files:
 Doc/library/unittest.mock.rst | 25 +++++++++++---------
 Lib/unittest/mock.py | 27 +++++++++++++---------
 2 files changed, 30 insertions(+), 22 deletions(-)
diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst
--- a/Doc/library/unittest.mock.rst
+++ b/Doc/library/unittest.mock.rst
@@ -920,17 +920,20 @@
 
 `patch` acts as a function decorator, class decorator or a context
 manager. Inside the body of the function or with statement, the `target`
- (specified in the form `'package.module.ClassName'`) is patched
- with a `new` object. When the function/with statement exits the patch is
- undone.
-
- The `target` is imported and the specified attribute patched with the new
- object, so it must be importable from the environment you are calling the
- decorator from. The target is imported when the decorated function is
- executed, not at decoration time.
-
- If `new` is omitted, then a new `MagicMock` is created and passed in as an
- extra argument to the decorated function.
+ is patched with a `new` object. When the function/with statement exits
+ the patch is undone.
+
+ If `new` is omitted, then the target is replaced with a
+ :class:`MagicMock`. If `patch` is used as a decorator and `new` is
+ omitted, the created mock is passed in as an extra argument to the
+ decorated function. If `patch` is used as a context manager the created
+ mock is returned by the context manager.
+
+ `target` should be a string in the form `'package.module.ClassName'`. The
+ `target` is imported and the specified object replaced with the `new`
+ object, so the `target` must be importable from the environment you are
+ calling `patch` from. The target is imported when the decorated function
+ is executed, not at decoration time.
 
 The `spec` and `spec_set` keyword arguments are passed to the `MagicMock`
 if patch is creating one for you.
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1351,18 +1351,23 @@
 ):
 """
 `patch` acts as a function decorator, class decorator or a context
+
+ `patch` acts as a function decorator, class decorator or a context
 manager. Inside the body of the function or with statement, the `target`
- (specified in the form `'package.module.ClassName'`) is patched
- with a `new` object. When the function/with statement exits the patch is
- undone.
-
- The `target` is imported and the specified attribute patched with the new
- object, so it must be importable from the environment you are calling the
- decorator from. The target is imported when the decorated function is
- executed, not at decoration time.
-
- If `new` is omitted, then a new `MagicMock` is created and passed in as an
- extra argument to the decorated function.
+ is patched with a `new` object. When the function/with statement exits
+ the patch is undone.
+
+ If `new` is omitted, then the target is replaced with a
+ `MagicMock`. If `patch` is used as a decorator and `new` is
+ omitted, the created mock is passed in as an extra argument to the
+ decorated function. If `patch` is used as a context manager the created
+ mock is returned by the context manager.
+
+ `target` should be a string in the form `'package.module.ClassName'`. The
+ `target` is imported and the specified object replaced with the `new`
+ object, so the `target` must be importable from the environment you are
+ calling `patch` from. The target is imported when the decorated function
+ is executed, not at decoration time.
 
 The `spec` and `spec_set` keyword arguments are passed to the `MagicMock`
 if patch is creating one for you.
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /