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 spec can't be array/ndarray in Python 3.9
Type: behavior Stage:
Components: Tests Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: valeriupredoi, xtreak
Priority: normal Keywords:

Created on 2020-12-07 13:31 by valeriupredoi, last changed 2022-04-11 14:59 by admin.

Messages (11)
msg382634 - (view) Author: Valeriu Predoi (valeriupredoi) Date: 2020-12-07 13:31
Hey guys, the new unittest.mock.Mock for Python 3.9 can not accept a spec arg if it is a numpy array, it'll accept all other types but not ndarray, have a look at this quick minimal code:

import numpy as np

from unittest import mock as mg

ob = mg.Mock(spec=[4, 4])
print("spec is list, mock is", ob)

ob = mg.Mock(spec=["4", "4"])
print("spec is list of str, mock is", ob)

ob = mg.Mock(spec=(4, 4))
print("spec is tuple, mock is", ob)

ob = mg.Mock(spec="cow")
print("spec is string, mock is", ob)

ob = mg.Mock(spec=22)
print("spec is int, mock is", ob)

ob = mg.Mock(spec=np.array([4, 4]))
print("spec is ndarray, mock is", ob)

versions:
python  3.9.0 h2a148a8_4_cpython    conda-forge
pytest-mock 3.3.1  pypi_0    pypi

Is this intended or it's a buggy-bug?

Cheers muchly! V
msg382636 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-12-07 13:39
I don't have Python 3.9 with numpy to test this. Does this used to work with python 3.8? Does using numpy produce any error or traceback in 3.9?
msg382637 - (view) Author: Valeriu Predoi (valeriupredoi) Date: 2020-12-07 13:50
Hello mate, yes it used to work fine with Python 3.8, no issue with Python 3.9 and numpy if you call mock as 

ob = mg.Mock(spec=np.array([4, 4]))

this results in a nice exception being raised:

Traceback (most recent call last):
  File "/home/valeriu/ESMValCore/tmock.py", line 20, in <module>
    ob = mg.Mock(spec=np.array([4, 4]))
  File "/home/valeriu/miniconda3/envs/esmvalcore39/lib/python3.9/unittest/mock.py", line 409, in __new__
    if spec_arg and _is_async_obj(spec_arg):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

which is pretty normal since that func performs an existence check on an array - hope this helps! Cheers
msg382638 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-12-07 14:34
Thanks, this looks like a duplicate of https://bugs.python.org/issue42532
msg382640 - (view) Author: Valeriu Predoi (valeriupredoi) Date: 2020-12-07 14:40
Indeed so! Sorry, I only glossed over that one before posting mine, now that I looked at it more carefully you are right, it does need a check on None and not calling the __bool__ member, which is affecting functionally passing an ndarray as spec - what should I do with my issue, close it? Cheers muchly!
msg382641 - (view) Author: Valeriu Predoi (valeriupredoi) Date: 2020-12-07 14:45
Note however, that the need I have for my type of functionality is fulfilled and all works fine for Python 3.7 and 3.8, I noticed the problem only for 3.9 - the OP in that issue says their versions affected include 3.8 (maybe it creeps up for 3.8.6 or larger, I tested with 3.8.5). Cheers!
msg382642 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-12-07 14:56
It's a problem with 3.9 only as far as I know. The fix has been merged to master (3.10) and 3.9 . I will check with 3.8 but I don't think it will be a problem since 3.8 has a different code path. You can check the PR merged for more discussion.
msg382652 - (view) Author: Valeriu Predoi (valeriupredoi) Date: 2020-12-07 16:13
Great, cheers mate, I read through the PR's (and sorry to have pestered you over at the mock backport GitHub too haha) - this looks it will fix the issue nicely. Do you know when will the release be available for us using Python from Anaconda etc.? Feel free to close this, and thanks for the assistance! Cheers, V
msg382653 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-12-07 16:18
3.9.1 is in RC1 as per https://www.python.org/dev/peps/pep-0596/#lifespan . It will be available as part of 3.9.1 or 3.9.2 . I am not sure about anaconda packaging schedule though.
msg382654 - (view) Author: Valeriu Predoi (valeriupredoi) Date: 2020-12-07 16:20
Excellent, just gonna let my team know about this and we'll hold off on 3.9.0 then - good stuff, mate!
msg382655 - (view) Author: Valeriu Predoi (valeriupredoi) Date: 2020-12-07 16:38
Changed status to pending, will close once we've had 3.9.1 in our env and tested proper, but should be OK, cheers!
History
Date User Action Args
2022-04-11 14:59:39adminsetgithub: 86752
2020-12-07 16:38:28valeriupredoisetstatus: pending -> open

messages: + msg382655
2020-12-07 16:37:20valeriupredoisetstatus: open -> pending
2020-12-07 16:20:19valeriupredoisetmessages: + msg382654
2020-12-07 16:18:11xtreaksetmessages: + msg382653
2020-12-07 16:13:30valeriupredoisetmessages: + msg382652
2020-12-07 14:56:17xtreaksetmessages: + msg382642
2020-12-07 14:45:50valeriupredoisetmessages: + msg382641
2020-12-07 14:40:15valeriupredoisetmessages: + msg382640
2020-12-07 14:34:25xtreaksetmessages: + msg382638
2020-12-07 13:50:11valeriupredoisetmessages: + msg382637
2020-12-07 13:39:20xtreaksetnosy: + xtreak
messages: + msg382636
2020-12-07 13:31:34valeriupredoicreate