Karthikeyan Singaravelan <[email protected]> added the comment:
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)
test_with_decorator()
test_with_context_manager()
$ ./python.exe test_foo.py
target inside decorator : {'a': 2}
target inside context : {'a': 2, 'b': 2}
----------
nosy: +cjw296, mariocj89, michael.foord
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue35512>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com