mirror of https://github.com/python/cpython
unittest.mock doc: Fix references to recursive seal of Mocks (GH-9028)
The docs in `library/unittest.mock` have been updated to remove confusing terms about submock and be explicit about the behavior expected.
This commit is contained in:
parent
5a30620e68
commit
96200eb2ff
|
@ -2410,17 +2410,18 @@ Sealing mocks
|
||||||
|
|
||||||
.. function:: seal(mock)
|
.. function:: seal(mock)
|
||||||
|
|
||||||
Seal will disable the creation of mock children by preventing getting or setting
|
Seal will disable the automatic creation of mocks when accessing an attribute of
|
||||||
of any new attribute on the sealed mock. The sealing process is performed recursively.
|
the mock being sealed or any of its attributes that are already mocks recursively.
|
||||||
|
|
||||||
If a mock instance is assigned to an attribute instead of being dynamically created
|
If a mock instance with a name or a spec is assigned to an attribute
|
||||||
it won't be considered in the sealing chain. This allows one to prevent seal from
|
it won't be considered in the sealing chain. This allows one to prevent seal from
|
||||||
fixing part of the mock object. ::
|
fixing part of the mock object. ::
|
||||||
|
|
||||||
>>> mock = Mock()
|
>>> mock = Mock()
|
||||||
>>> mock.submock.attribute1 = 2
|
>>> mock.submock.attribute1 = 2
|
||||||
>>> mock.not_submock = mock.Mock()
|
>>> mock.not_submock = mock.Mock(name="sample_name")
|
||||||
>>> seal(mock)
|
>>> seal(mock)
|
||||||
|
>>> mock.new_attribute # This will raise AttributeError.
|
||||||
>>> mock.submock.attribute2 # This will raise AttributeError.
|
>>> mock.submock.attribute2 # This will raise AttributeError.
|
||||||
>>> mock.not_submock.attribute2 # This won't raise.
|
>>> mock.not_submock.attribute2 # This won't raise.
|
||||||
|
|
||||||
|
|
|
@ -2425,15 +2425,14 @@ class PropertyMock(Mock):
|
||||||
|
|
||||||
|
|
||||||
def seal(mock):
|
def seal(mock):
|
||||||
"""Disable the automatic generation of "submocks"
|
"""Disable the automatic generation of child mocks.
|
||||||
|
|
||||||
Given an input Mock, seals it to ensure no further mocks will be generated
|
Given an input Mock, seals it to ensure no further mocks will be generated
|
||||||
when accessing an attribute that was not already defined.
|
when accessing an attribute that was not already defined.
|
||||||
|
|
||||||
Submocks are defined as all mocks which were created DIRECTLY from the
|
The operation recursively seals the mock passed in, meaning that
|
||||||
parent. If a mock is assigned to an attribute of an existing mock,
|
the mock itself, any mocks generated by accessing one of its attributes,
|
||||||
it is not considered a submock.
|
and all assigned mocks without a name or spec will be sealed.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
mock._mock_sealed = True
|
mock._mock_sealed = True
|
||||||
for attr in dir(mock):
|
for attr in dir(mock):
|
||||||
|
|
Loading…
Reference in New Issue