Andrei Kulakov <andrei....@gmail.com> added the comment:

This issue also came up with MagicMock on stackoverflow: 
https://stackoverflow.com/questions/23257227/python-mock-method-call-arguments-display-the-last-state-of-a-list/23264042

Dave: glad this helped to clear it up. For the record, the arguments in Python 
are passed by object reference, which is not the same as by reference or by 
value; - it means that mutable objects can be mutated by receiving func and 
immutable objs cannot be mutated. In your case it was a mutable object, you 
mutated it, but compared it to value that it had before mutation.

I've thought if this should be cleared up in the docs, but I think in most 
cases it will be fairly obvious to see what happened because the tested value 
will differ from expected exactly in the way that the function modifies it. But 
if more people run into this confusion, we should consider adding a note to 
MagicMock, call_args, and call_args_list (and maybe some other mock attrs).

----------
stage:  -> resolved
status: open -> closed
title: Incorrect record of call_args_list when using multiple side_effect in 
mock.patch -> Unexpected record of call_args_list when passing mutable object 
to mock.patch
type:  -> behavior
versions: +Python 3.10, Python 3.11, Python 3.9 -Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue45305>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to