Author xtreak
Recipients cjw296, jaraco, mariocj89, michael.foord, xtreak
Date 2019-02-23.06:08:37
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Looking further this can be solved for a string target in patch.dict which can be resolved again while calling the decorated function. There could be a case where the actual target is specified and in that case mock could only updates the reference and cannot track if the variable has been redefined to reference a different dict object. In the below case also it's resolved with {'a': 1} in the decorator and later redefining target to {'a': 2} whose reference is not updated. I can propose a PR for string target but I am not sure if this case can be solved or it's expected. This seems to be not a problem with patch.object where redefining a class later like dict seems to work correctly and maybe it's due to creating a new class itself that updates the local to reference new class?

Any thoughts would be helpful.

# script with dict target passed

from unittest import mock

target = dict(a=1)

@mock.patch.dict(target, dict(b=2))
def test_with_decorator():
    print(f"target inside decorator : {target}")

def test_with_context_manager():
    with mock.patch.dict(target, dict(b=2)):
        print(f"target inside context : {target}")

target = dict(a=2)

$ ./python.exe
target inside decorator : {'a': 2}
target inside context : {'a': 2, 'b': 2}
Date User Action Args
2019-02-23 06:08:37xtreaksetrecipients: + xtreak, jaraco, cjw296, michael.foord, mariocj89
2019-02-23 06:08:37xtreaksetmessageid: <>
2019-02-23 06:08:37xtreaklinkissue35512 messages
2019-02-23 06:08:37xtreakcreate