Skip to content

Commit 96200eb

Browse files
mariocj89vstinner
authored andcommitted
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.
1 parent 5a30620 commit 96200eb

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
@@ -2410,17 +2410,18 @@ Sealing mocks
24102410

24112411
.. function:: seal(mock)
24122412

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

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

24202420
>>> mock = Mock()
24212421
>>> mock.submock.attribute1 = 2
2422-
>>> mock.not_submock = mock.Mock()
2422+
>>> mock.not_submock = mock.Mock(name="sample_name")
24232423
>>> seal(mock)
2424+
>>> mock.new_attribute # This will raise AttributeError.
24242425
>>> mock.submock.attribute2 # This will raise AttributeError.
24252426
>>> mock.not_submock.attribute2 # This won't raise.
24262427

Lib/unittest/mock.py

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

24262426

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

0 commit comments

Comments
 (0)