Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is a patch.dict specifically

Tags:

python

pytest

I have found that the python internal deals with dictionary object different as the other object like function and list.

Does anyone have idea why python mock library (1.0.1) has a patch.dict specifically besides the existing patch and patch.object?

like image 833
Hello lad Avatar asked Sep 20 '25 07:09

Hello lad


1 Answers

patch.dict() for setting values in a dictionary just during a scope and restoring the dictionary to its original state when the test ends:

foo = {'key': 'value'}
original = foo.copy()

with patch.dict(foo, {'newkey': 'newvalue'}, clear=True):
    assert foo == {'newkey': 'newvalue'}

assert foo == original

See the reference for further info.

like image 126
Avinash Babu Avatar answered Sep 21 '25 21:09

Avinash Babu