This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Mock is equal to ANY but MagicMock is not
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7, Python 3.6, Python 3.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: berker.peksag, kushal.das, michael.foord, python-dev, rafael.fonseca, serhiy.storchaka, xiang.zhang
Priority: normal Keywords: patch

Created on 2016-11-18 16:07 by rafael.fonseca, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
magicmock_eq.patch serhiy.storchaka, 2017-01-20 17:37 review
Pull Requests
URL Status Linked Edit
PR 552 closed dstufft, 2017-03-31 16:36
Messages (8)
msg281135 - (view) Author: Rafael Jacinto Caricio da Fonseca (rafael.fonseca) Date: 2016-11-18 16:07
On Python 3.5.2 mock.Mock() is equal to mock.ANY, but mock.MagicMock() is not.

Minimal example:

In Python 3.5.2:
>>> from unittest import mock
>>> mock.Mock() == mock.ANY
>>> mock.ANY == mock.Mock()
>>> mock.MagicMock() == mock.ANY
>>> mock.ANY == mock.MagicMock()
msg285908 - (view) Author: Kushal Das (kushal.das) * (Python committer) Date: 2017-01-20 14:25
From a checkout in last week.

Python 3.7.0a0 (default:c163a76163f7, Jan 15 2017, 22:20:24) 
[GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from unittest import mock
>>> mock.ANY == mock.Mock() 
>>> mock.ANY == mock.MagicMock() 
msg285909 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2017-01-20 14:49
Kushal I think the OP is reporting `mock.MagicMock() == mock.ANY` is False while `mock.ANY == mock.MagicMock()` is True which is still the case in 3.7. A related issue is #25195. In that issue Serhiy has mentioned this behaviour(
msg285910 - (view) Author: Kushal Das (kushal.das) * (Python committer) Date: 2017-01-20 14:51
Now I understood, thanks. I think I should have more coffee before commenting on bugs :)
msg285918 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-01-20 17:37
Proposed patch fixes comparing MagicMock with ANY.
msg285925 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2017-01-20 23:13
Serhiy's patch looks pretty good to me, thanks! Also, thanks for the report, Rafael. I definitely forgot to fix this case after fixing issue 25195.
msg285966 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2017-01-21 21:17
New changeset 442eb26b1ca4 by Serhiy Storchaka in branch '3.5':
Issue #28735: Fixed the comparison of mock.MagickMock with mock.ANY.

New changeset 4a38781538f7 by Serhiy Storchaka in branch '3.6':
Issue #28735: Fixed the comparison of mock.MagickMock with mock.ANY.

New changeset 597515fcb343 by Serhiy Storchaka in branch 'default':
Issue #28735: Fixed the comparison of mock.MagickMock with mock.ANY.
msg285967 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-01-21 21:21
Thanks Berker.

Maybe _Call.__eq__ also should return NotImplemented in some cases, but I don't have a demonstrating example.
Date User Action Args
2022-04-11 14:58:39adminsetgithub: 72921
2017-03-31 16:36:21dstufftsetpull_requests: + pull_request952
2017-01-21 21:21:01serhiy.storchakasetstatus: open -> closed
resolution: fixed
messages: + msg285967

stage: commit review -> resolved
2017-01-21 21:17:52python-devsetnosy: + python-dev
messages: + msg285966
2017-01-20 23:13:59berker.peksagsetmessages: + msg285925
stage: patch review -> commit review
2017-01-20 17:37:55serhiy.storchakasetfiles: + magicmock_eq.patch
keywords: + patch
messages: + msg285918

stage: patch review
2017-01-20 14:51:39kushal.dassetmessages: + msg285910
2017-01-20 14:49:07xiang.zhangsetnosy: + serhiy.storchaka, berker.peksag, xiang.zhang
messages: + msg285909
2017-01-20 14:25:16kushal.dassetnosy: + kushal.das
messages: + msg285908
2016-11-18 21:02:43SilentGhostsetnosy: + michael.foord

versions: + Python 3.6, Python 3.7
2016-11-18 16:07:14rafael.fonsecacreate