[issue38346] Wrong behavior when using `assert_called_with` with mutable object

2019-10-05 Thread Mario Corchero
Mario Corchero added the comment: Thanks! We can look at adding a copying mock if we see people needing it, but yeah, adding copy by default would be quite complex if we don't want to break existing users. -- stage: -> resolved status: open -> closed

[issue38346] Wrong behavior when using `assert_called_with` with mutable object

2019-10-04 Thread Vedran Čačić
Vedran Čačić added the comment: > when `b` has same values of `a` before it is mutated. There might be no such object. Or it might exist, but you still wouldn't want it. Consider the case when a is a coroutine that has just started (a=coro()). You call f with a, and then advance a to the

[issue38346] Wrong behavior when using `assert_called_with` with mutable object

2019-10-03 Thread Jim Jeon
Jim Jeon added the comment: Thank you all for the kind answers. I didn't know copying could cause so many problems. @veky Thank you for the example. But it seems that the example will actually raise and I think it should. I am talking f.assert_called_with(b) when `b` has same values of `a`

[issue38346] Wrong behavior when using `assert_called_with` with mutable object

2019-10-03 Thread Mario Corchero
Mario Corchero added the comment: This might be painful in certain scenarios, like when using wraps on functions that modify the arguments: ``` def func(d): return d.pop("key") >>> def func(d): ... return d.pop("key") ... >>> m = Mock(wraps=func) >>> m({"key": 1}) 1 >>>

[issue38346] Wrong behavior when using `assert_called_with` with mutable object

2019-10-02 Thread Karthikeyan Singaravelan
Karthikeyan Singaravelan added the comment: See also https://bugs.python.org/issue28848 -- ___ Python tracker ___ ___

[issue38346] Wrong behavior when using `assert_called_with` with mutable object

2019-10-02 Thread Karthikeyan Singaravelan
Karthikeyan Singaravelan added the comment: As explained copy would cause backwards incompatible change on objects that depend on identity comparison. I am not sure if this can be fixed in a backwards compatible manner. > One possibility would be for mock to copy the arguments you pass in.

[issue38346] Wrong behavior when using `assert_called_with` with mutable object

2019-10-02 Thread Vedran Čačić
Vedran Čačić added the comment: I don't see what the "fix" should be. Python doesn't have value semantics. Functions are called with objects, not with their values. Imagine a was global variable, and then you say: a = mutable_object() f(a) a.mutate() f.assert_called_with(a)

[issue38346] Wrong behavior when using `assert_called_with` with mutable object

2019-10-02 Thread Jim Jeon
New submission from Jim Jeon : When `assert_called_with` is used with mutable object, test fails if the object has changed. I think this is not what it meant to do. https://docs.python.org/dev/library/unittest.mock-examples.html#coping-with-mutable-arguments The same situation is explained