diff options
author | Mario Corchero <mariocj89@gmail.com> | 2017-10-17 11:35:11 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2017-10-17 11:35:11 (GMT) |
commit | 552be9d7e64f91b8e4ba5b29cd5dcc442d56f92c (patch) | |
tree | 1e336d15954b38d33b069ad2caae3775dcee373e /Doc/library/unittest.mock.rst | |
parent | 2bd37c227e8a042e036c7455d974e3d0b36aed53 (diff) | |
download | cpython-552be9d7e64f91b8e4ba5b29cd5dcc442d56f92c.zip cpython-552be9d7e64f91b8e4ba5b29cd5dcc442d56f92c.tar.gz cpython-552be9d7e64f91b8e4ba5b29cd5dcc442d56f92c.tar.bz2 |
bpo-30541: Add new method to seal mocks (GH61923)
The new method allows the developer to control when to stop the
feature of mocks that automagically creates new mocks when accessing
an attribute that was not declared before
Signed-off-by: Mario Corchero <mariocj89@gmail.com>
Diffstat (limited to 'Doc/library/unittest.mock.rst')
-rw-r--r-- | Doc/library/unittest.mock.rst | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index 9e8bf11..6fdfdc4 100644 --- a/Doc/library/unittest.mock.rst +++ b/Doc/library/unittest.mock.rst @@ -2365,3 +2365,23 @@ alternative object as the *autospec* argument: a mocked class to create a mock instance *does not* create a real instance. It is only attribute lookups - along with calls to :func:`dir` - that are done. +Sealing mocks +~~~~~~~~~~~~~ + +.. function:: seal(mock) + + Seal will disable the creation of mock children by preventing to get or set + any new attribute on the sealed mock. The sealing process is performed recursively. + + If a mock instance is assigned to an attribute instead of being dynamically created + it wont be considered in the sealing chain. This allows to prevent seal from fixing + part of the mock object. + + >>> mock = Mock() + >>> mock.submock.attribute1 = 2 + >>> mock.not_submock = mock.Mock() + >>> seal(mock) + >>> mock.submock.attribute2 # This will raise AttributeError. + >>> mock.not_submock.attribute2 # This won't raise. + + .. versionadded:: 3.7 |