Skip to content

Commit 984a800

Browse files
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. (cherry picked from commit 96200eb) Co-authored-by: Mario Corchero <[email protected]>
1 parent b9182aa commit 984a800

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

Doc/library/unittest.mock.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,17 +2374,18 @@ Sealing mocks
23742374

23752375
.. function:: seal(mock)
23762376

2377-
Seal will disable the creation of mock children by preventing getting or setting
2378-
of any new attribute on the sealed mock. The sealing process is performed recursively.
2377+
Seal will disable the automatic creation of mocks when accessing an attribute of
2378+
the mock being sealed or any of its attributes that are already mocks recursively.
23792379

2380-
If a mock instance is assigned to an attribute instead of being dynamically created
2380+
If a mock instance with a name or a spec is assigned to an attribute
23812381
it won't be considered in the sealing chain. This allows one to prevent seal from
23822382
fixing part of the mock object.
23832383

23842384
>>> mock = Mock()
23852385
>>> mock.submock.attribute1 = 2
2386-
>>> mock.not_submock = mock.Mock()
2386+
>>> mock.not_submock = mock.Mock(name="sample_name")
23872387
>>> seal(mock)
2388+
>>> mock.new_attribute # This will raise AttributeError.
23882389
>>> mock.submock.attribute2 # This will raise AttributeError.
23892390
>>> mock.not_submock.attribute2 # This won't raise.
23902391

Lib/unittest/mock.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,15 +2423,14 @@ def __set__(self, obj, val):
24232423

24242424

24252425
def seal(mock):
2426-
"""Disable the automatic generation of "submocks"
2426+
"""Disable the automatic generation of child mocks.
24272427
24282428
Given an input Mock, seals it to ensure no further mocks will be generated
24292429
when accessing an attribute that was not already defined.
24302430
2431-
Submocks are defined as all mocks which were created DIRECTLY from the
2432-
parent. If a mock is assigned to an attribute of an existing mock,
2433-
it is not considered a submock.
2434-
2431+
The operation recursively seals the mock passed in, meaning that
2432+
the mock itself, any mocks generated by accessing one of its attributes,
2433+
and all assigned mocks without a name or spec will be sealed.
24352434
"""
24362435
mock._mock_sealed = True
24372436
for attr in dir(mock):

0 commit comments

Comments
 (0)