pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
12.12k stars 2.68k forks source link

Running code with numpy array element assignent returns a ValueError: assignment destination is read-only #10541

Closed smoia closed 1 year ago

smoia commented 1 year ago

Hello! Not sure this is a bug, but similar code elsewhere does not raise the same issue.

I wrote a test for a function that has a (input) numpy array element assignment at a certain point in the flow.

Running it in pytest raises: ValueError: assignment destination is read-only

Running the same function outside of pytest does not provide the same error.

A similar behaviour in a different function does not raise any issue.

You can find the line here, compare with this line that is similar but passes tests. The test that fails raising this issue is here. The log of an independent CI is here (this test is with python3.7, there's another test with python 3.10, and on my system I have python 3.8)

I do not understand why the variable I am assigning is read-only, honestly. When I check variable value with pdb on crash, the variable is returned as intended after the line run.

Versions:

pytest: 7.2.0, python: 3.8.10, 3.7.15, 3.10.8, OS: Ubuntu 20.04

pip3 list:

Package                       Version                                Editable project location
----------------------------- -------------------------------------- -------------------------------------
alabaster                     0.7.12
annexremote                   1.6.0
appdirs                       1.4.4
apt-xapian-index              0.49
apturl                        0.5.2
argcomplete                   2.0.0
astroid                       2.11.7
asttokens                     2.0.5
attrdict                      2.0.1
attrs                         22.1.0
Babel                         2.10.3
backcall                      0.2.0
backports.zoneinfo            0.2.1
bcrypt                        3.1.7
beautifulsoup4                4.8.2
bidscoin                      3.7.4                                  /home/nemo/Scrivania/gitlab/bidscoin
bioread                       3.0.0
black                         22.6.0
blinker                       1.4
boto                          2.49.0
braintransform                0.1.1.dev8+gd0eb082
Brlapi                        0.7.0
certifi                       2019.11.28
cffi                          1.15.1
cfgv                          3.3.1
chardet                       3.0.4
charset-normalizer            2.1.1
chevron                       0.14.0
chrome-gnome-shell            0.0.0
ci-info                       0.3.0
citeproc-py                   0.6.0
click                         8.1.3
colorama                      0.4.3
colored                       1.4.3
coloredlogs                   15.0.1
command-not-found             0.3
commonmark                    0.9.1
coverage                      6.4.2
cryptography                  2.8
cupshelpers                   1.0
cycler                        0.11.0
datalad                       0.17.6
dbus-python                   1.2.16
decorator                     5.1.1
defer                         1.0.6
defusedxml                    0.7.1
dill                          0.3.5.1
distlib                       0.3.5
distro                        1.4.0
distro-info                   0.23ubuntu1
docopt                        0.6.2
docutils                      0.17.1
duecredit                     0.9.1
duplicity                     0.8.12.0
entrypoints                   0.3
et-xmlfile                    1.1.0
etelemetry                    0.3.0
evdev                         1.3.0
exceptiongroup                1.0.4
executing                     0.9.1
fasteners                     0.14.1
filelock                      3.7.1
flake8                        4.0.1
flake8-docstrings             1.6.0
fonttools                     4.34.4
fsleyes                       1.5.0
fsleyes-props                 1.8.2
fsleyes-widgets               0.12.3
fslpy                         3.10.0
future                        0.18.2
Gooey                         1.0.8.1
gst                           0.1.0
h5py                          3.7.0
html5lib                      1.0.1
httplib2                      0.14.0
humanfriendly                 10.0
humanize                      4.4.0
identify                      2.5.6
idna                          2.8
imagesize                     1.4.1
importlib-metadata            4.12.0
importlib-resources           5.10.0
iniconfig                     1.1.1
ipython                       8.5.0
iso8601                       1.1.0
isort                         5.10.1
jaraco.classes                3.2.3
jedi                          0.18.1
jeepney                       0.8.0
Jinja2                        3.1.2
joblib                        1.1.0
jsonschema                    4.16.0
kazam                         1.5.3
keyring                       23.9.3
keyrings.alt                  4.2.0
kiwisolver                    1.4.4
language-selector             0.1
launchpadlib                  1.10.13
lazr.restfulclient            0.14.2
lazr.uri                      1.0.3
lazy-object-proxy             1.7.1
lockfile                      0.12.2
louis                         3.12.0
lxml                          4.5.0
macaroonbakery                1.3.1
Mako                          1.1.0
markdown-it-py                2.1.0
MarkupSafe                    2.1.1
matplotlib                    3.5.2
matplotlib-inline             0.1.3
mccabe                        0.6.1
mdit-py-plugins               0.3.0
mdurl                         0.1.1
monotonic                     1.5
more-itertools                8.14.0
msgpack                       1.0.4
multiecho                     0.28
mypy-extensions               0.4.3
myst-parser                   0.18.0
netifaces                     0.10.4
nibabel                       4.0.1
nigsp                         0.6.1+5.gc96a98e                       /home/nemo/Scrivania/gitlab/nigsp
nilearn                       0.9.2
niutils                       0.3.4                                  /home/nemo/Scrivania/gitlab/niutils
nodeenv                       1.7.0
numpy                         1.23.5
oauthlib                      3.1.0
odfpy                         1.4.1
olefile                       0.46
openpyxl                      3.0.10
packaging                     21.3
pandas                        1.5.1
paramiko                      2.6.0
parso                         0.8.3
pathspec                      0.9.0
patool                        1.12
patsy                         0.5.3
pbr                           5.10.0
peakdet                       0.1.0+48.g6009c2b                      /home/nemo/Scrivania/gitlab/peakdet
pexpect                       4.6.0
phys2bids                     2.6.2+8.g9b8521e                       /home/nemo/Scrivania/gitlab/phys2bids
phys2cvr                      0.15.2+1.g528d8dc                      /home/nemo/Scrivania/gitlab/phys2cvr
pickleshare                   0.7.5
Pillow                        7.0.0
pip                           22.3.1
pkgutil_resolve_name          1.3.10
platformdirs                  2.5.2
pluggy                        1.0.0
pre-commit                    2.20.0
prompt-toolkit                3.0.30
protobuf                      3.6.1
psutil                        5.9.1
pure-eval                     0.2.2
py                            1.11.0
PyBluez                       0.22
pycairo                       1.16.2
pycodestyle                   2.8.0
pycparser                     2.21
pycryptodomex                 3.6.1
pycups                        1.9.73
pydicom                       2.3.0
pydocstyle                    6.1.1
pyflakes                      2.4.0
Pygments                      2.12.0
PyGObject                     3.36.0
pygtrie                       2.5.0
PyJWT                         1.7.1
pylint                        2.14.5
pymacaroons                   0.13.0
pymatreader                   0.0.30
PyNaCl                        1.3.0
PyOpenGL                      3.1.6
pyparsing                     2.4.7
PyQt5                         5.15.7
PyQt5-Qt5                     5.15.2
PyQt5-sip                     12.11.0
pyRFC3339                     1.1
pyrsistent                    0.18.1
pytest                        7.2.0
pytest-cov                    4.0.0
python-apt                    2.0.0+ubuntu0.20.4.8
python-dateutil               2.8.2
python-debian                 0.1.36ubuntu1
python-gitlab                 3.10.0
python-lsp-jsonrpc            1.0.0
python-lsp-server             1.5.0
python-magic                  0.4.16
python-xlib                   0.23
pytz                          2022.1
pytz-deprecation-shim         0.1.0.post0
pyxattr                       0.6.1
pyxdg                         0.26
PyYAML                        5.3.1
regex                         2022.9.13
reportlab                     3.5.34
requests                      2.28.1
requests-toolbelt             0.10.0
requests-unixsocket           0.2.0
rich                          12.6.0
ruamel.yaml                   0.17.21
ruamel.yaml.clib              0.2.6
scikit-learn                  1.1.1
scipy                         1.9.0
screen-resolution-extra       0.0.0
seaborn                       0.12.1
SecretStorage                 3.3.3
setproctitle                  1.1.10
setuptools                    65.4.1
simplejson                    3.16.0
sip                           4.19.21
six                           1.14.0
snowballstemmer               2.2.0
soupsieve                     1.9.5
Sphinx                        5.1.1
sphinx-argparse               0.3.1
sphinx-rtd-theme              1.0.0
sphinxcontrib-applehelp       1.0.2
sphinxcontrib-devhelp         1.0.2
sphinxcontrib-htmlhelp        2.0.0
sphinxcontrib-jsmath          1.0.1
sphinxcontrib-qthelp          1.0.3
sphinxcontrib-serializinghtml 1.1.5
stack-data                    0.3.0
statsmodels                   0.13.5
systemd-python                234
tedana                        Release-EuskalIBUR-version-24.gc30d692 /home/nemo/Scrivania/gitlab/tedana
testresources                 2.0.1
threadpoolctl                 3.1.0
toml                          0.10.2
tomli                         2.0.1
tomlkit                       0.11.1
tqdm                          4.64.0
traitlets                     5.3.0
typing_extensions             4.3.0
tzdata                        2022.5
tzlocal                       4.2
ubuntu-advantage-tools        27.11.3
ubuntu-drivers-common         0.0.0
ufw                           0.36
ujson                         5.4.0
unattended-upgrades           0.1
urllib3                       1.25.8
usb-creator                   0.3.7
versioneer                    0.22
virtualenv                    20.16.2
wadllib                       1.3.3
wcwidth                       0.2.5
webencodings                  0.5.1
wheel                         0.37.1
Whoosh                        2.7.4
wrapt                         1.14.1
wxPython                      4.2.0
xkit                          0.0.0
xmltodict                     0.13.0
youtube-dl                    2021.12.17
zipp                          3.8.1
kalekundert commented 1 year ago

The issue is with degree = degree.diagonal() on line 121 of laplacian.py. This makes a view of the array, rather than a copy, and views can't be assigned to. You can easily make a copy instead by changing the line to degree = degree.diagonal().copy(). This stack overflow Q/A has some more information on what's going on.