mirror of
https://github.com/python/cpython.git
synced 2025-07-24 03:35:53 +00:00
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>
This commit is contained in:
parent
2bd37c227e
commit
552be9d7e6
5 changed files with 249 additions and 2 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue