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.

classification
Title: unittest.mock.Mock's new "unsafe" feature needs a better error message
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Dima.Tisnek, Randy Syring, ZackerySpytz, kushal.das, rbcollins, xtreak
Priority: normal Keywords: patch

Created on 2015-07-30 19:07 by Randy Syring, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 12991 merged ZackerySpytz, 2019-04-28 05:59
Messages (7)
msg247690 - (view) Author: Randy Syring (Randy Syring) Date: 2015-07-30 19:07
In issue http://bugs.python.org/issue21238 a feature was introduced to help prevent false-positive test cases by throwing an AttributeError whenever a non-existent method prefixed with "assert_" is used on a mock object.

I had mock objects with legitimate "assert_*" methods that had been working for some time.  My tests included calls like:

my_mock.assert_in_status.assert_called_once_with(...)

My tests started failing unexpectedly and it took me HOURS to figure out that I had a new mock version installed.  Those hours could have been turned into seconds my simply giving a better error message, something like:

AttributeError: you used "assert_in_status" but that method is not a valid Mock assert method.  Please check your spelling.  If this was not a typing mistake, you can use the `unsafe` keyword argument to the Mock instance to turn this validation check off.  See https://mock-docs...org/ for more details.
msg247692 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2015-07-30 20:00
How were you setting/introducing your assertions on the mocks? e.g. could you supply a small sample script showing what used to work? Thanks.
msg247731 - (view) Author: Randy Syring (Randy Syring) Date: 2015-07-31 03:35
Old functionality:

(temp)rsyring@loftex:~/projects/hllapi-src$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mock
>>> mock.__version__
'1.0.0'
>>> m = mock.Mock()
>>> m.assert_screen_status.call_count
0

New functionality:

(temp)rsyring@loftex:~/projects/hllapi-src$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mock
>>> mock.__version__
'1.1.0'
>>> m = mock.Mock()
>>> m.assert_screen_status.call_count
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/rsyring/.virtualenvs/temp/local/lib/python2.7/site-packages/mock/mock.py", line 714, in __getattr__
    raise AttributeError(name)
AttributeError: assert_screen_status

In my case, the objects I was patching had a legit method like assert_screen_status().  But, after upgrading Mock, the use of those methods started throwing AttributeError's even though it was very obvious that a) the methods existed on the real objects and b) Mock is supposed to let me call anything on I want.  As I said previously, this was a VERY confusing situation to figure out.
msg248927 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2015-08-21 00:05
@Randy - ok thanks. So, please do improve the prose in the error message, should be a very straight forward patch.
msg341022 - (view) Author: Zackery Spytz (ZackerySpytz) * (Python triager) Date: 2019-04-28 06:02
I've created a PR for this issue.
msg341916 - (view) Author: Kushal Das (kushal.das) * (Python committer) Date: 2019-05-08 17:32
New changeset b9b08cd948de97d756a199b60becce8397a8c882 by Kushal Das (Zackery Spytz) in branch 'master':
bpo-24758: Improve the error msg for unittest.mock.Mock()'s unsafe mode (#12991)
https://github.com/python/cpython/commit/b9b08cd948de97d756a199b60becce8397a8c882
msg342240 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-05-12 05:21
Closing this as resolved since PR was merged. Thanks Zackery for the PR.
History
Date User Action Args
2022-04-11 14:58:19adminsetgithub: 68946
2019-05-12 05:21:13xtreaksetstatus: open -> closed

nosy: + xtreak
messages: + msg342240

resolution: fixed
stage: patch review -> resolved
2019-05-08 17:32:28kushal.dassetnosy: + kushal.das
messages: + msg341916
2019-04-28 06:02:40ZackerySpytzsetnosy: + ZackerySpytz

messages: + msg341022
versions: + Python 3.8, - Python 3.5
2019-04-28 05:59:44ZackerySpytzsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request12914
2015-08-21 00:05:20rbcollinssetmessages: + msg248927
stage: test needed -> needs patch
2015-07-31 03:35:06Randy Syringsetmessages: + msg247731
2015-07-30 20:00:50rbcollinssetstage: test needed
2015-07-30 20:00:40rbcollinssetnosy: + rbcollins
messages: + msg247692
2015-07-30 19:11:44Dima.Tisneksetnosy: + Dima.Tisnek
2015-07-30 19:07:05Randy Syringcreate