scipy: BUG: scipy v1.15 breaking for pytest when assert-rewrite is on
Describe your issue.
I don’t know if this is a scipy issue or a pytest one, but the most recent version of scipy (1.15) is causing errors with pytest, specifically if assertion rewriting is enabled. If the --assert=plain flag is passed to pytest, no error occurs.
To reproduce: put this in pytest.ini
[pytest]
python_files = *.py
and run pytest on the following python script:
Reproducing Code Example
from scipy import *
Error message
========================================================== test session starts ===========================================================
platform darwin -- Python 3.12.4, pytest-8.3.4, pluggy-1.5.0 -- /Users/bbm/mambaforge/envs/refl1d/bin/python3.12
cachedir: .pytest_cache
rootdir: /private/tmp/test
configfile: pytest.ini
plugins: cov-6.0.0, anyio-4.0.0
collected 0 items / 1 error
================================================================= ERRORS =================================================================
___________________________________________________ ERROR collecting broken_pytest.py ____________________________________________________
broken_pytest.py:3: in <module>
from scipy import *
<frozen importlib._bootstrap>:1360: in _find_and_load
???
<frozen importlib._bootstrap>:1331: in _find_and_load_unlocked
???
<frozen importlib._bootstrap>:935: in _load_unlocked
???
/Users/bbm/mambaforge/envs/refl1d/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:184: in exec_module
exec(co, module.__dict__)
/Users/bbm/mambaforge/envs/refl1d/lib/python3.12/site-packages/scipy/ndimage/__init__.py:156: in <module>
from ._support_alternative_backends import *
<frozen importlib._bootstrap>:1360: in _find_and_load
???
<frozen importlib._bootstrap>:1331: in _find_and_load_unlocked
???
<frozen importlib._bootstrap>:935: in _load_unlocked
???
/Users/bbm/mambaforge/envs/refl1d/lib/python3.12/site-packages/_pytest/assertion/rewrite.py:184: in exec_module
exec(co, module.__dict__)
/Users/bbm/mambaforge/envs/refl1d/lib/python3.12/site-packages/scipy/ndimage/_support_alternative_backends.py:65: in <module>
delegator = getattr(_delegators, func_name + "_signature")
E AttributeError: module 'scipy.ndimage._delegators' has no attribute '@py_builtins_signature'. Did you mean: 'grey_dilation_signature'?
======================================================== short test summary info =========================================================
ERROR broken_pytest.py - AttributeError: module 'scipy.ndimage._delegators' has no attribute '@py_builtins_signature'. Did you mean: 'grey_dilation_signature'?
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================ 1 error in 1.82s ============================================================
SciPy/NumPy/Python version and system information
1.15.0 2.0.2 sys.version_info(major=3, minor=12, micro=4, releaselevel='final', serial=0)
Build Dependencies:
blas:
detection method: extraframeworks
found: true
include directory: unknown
lib directory: unknown
name: Accelerate
openblas configuration: unknown
pc file directory: unknown
version: unknown
lapack:
detection method: extraframeworks
found: true
include directory: unknown
lib directory: unknown
name: Accelerate
openblas configuration: unknown
pc file directory: unknown
version: unknown
pybind11:
detection method: config-tool
include directory: unknown
name: pybind11
version: 2.13.6
Compilers:
c:
commands: cc
linker: ld64
name: clang
version: 15.0.0
c++:
commands: c++
linker: ld64
name: clang
version: 15.0.0
cython:
commands: cython
linker: cython
name: cython
version: 3.0.11
fortran:
commands: gfortran
linker: ld64
name: gcc
version: 13.3.0
pythran:
include directory: ../../../../../../private/var/folders/95/0ydz4d79163427j3k5crp3fh0000gn/T/pip-build-env-8oen8nl_/overlay/lib/python3.12/site-packages/pythran
version: 0.17.0
Machine Information:
build:
cpu: aarch64
endian: little
family: aarch64
system: darwin
cross-compiled: false
host:
cpu: aarch64
endian: little
family: aarch64
system: darwin
Python Information:
path: /private/var/folders/95/0ydz4d79163427j3k5crp3fh0000gn/T/cibw-run-ni2eppd2/cp312-macosx_arm64/build/venv/bin/python
version: '3.12'
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 11
- Comments: 19 (13 by maintainers)
Commits related to this issue
- updates CI due to pytest issue see https://github.com/scipy/scipy/issues/22236 — committed to bellingcat/geoclustering by msramalho 2 years ago
- migrate gh artifact actions to v4 (#20) * migrate gh artifact actions to v4 from migration guide no breaking changes apply here. * updates pipfile.lock dependency versions * updates CI due t... — committed to bellingcat/geoclustering by msramalho 2 years ago
- Fix pytest test error There is a known bug between scipy 1.15 and pytest when assert-rewrite. https://github.com/scipy/scipy/issues/22236 . We triggered this bug by including scipy.signal in #797. T... — committed to google-deepmind/torax by jcitrin 2 years ago
- Fix pytest test error There is a known bug between scipy 1.15 and pytest when assert-rewrite. https://github.com/scipy/scipy/issues/22236 . We triggered this bug by including scipy.signal in #797. T... — committed to google-deepmind/torax by jcitrin 2 years ago
- Fix pytest test error There is a known bug between scipy 1.15 and pytest when assert-rewrite. https://github.com/scipy/scipy/issues/22236 . We triggered this bug by including scipy.signal in #797. T... — committed to google-deepmind/torax by jcitrin 2 years ago
- Fix pytest test error There is a known bug between scipy 1.15 and pytest when assert-rewrite. https://github.com/scipy/scipy/issues/22236 . We triggered this bug by including scipy.signal in #797. T... — committed to google-deepmind/torax by jcitrin 2 years ago
- Fix pytest test error There is a known bug between scipy 1.15 and pytest when assert-rewrite. https://github.com/scipy/scipy/issues/22236 . We triggered this bug by including scipy.signal in #797. T... — committed to google-deepmind/torax by jcitrin 2 years ago
- Fix pytest test error There is a known bug between scipy 1.15 and pytest when assert-rewrite. https://github.com/scipy/scipy/issues/22236 . We triggered this bug by including scipy.signal in #797. T... — committed to google-deepmind/torax by jcitrin 2 years ago
- Fix pytest test error There is a known bug between scipy 1.15 and pytest when assert-rewrite. https://github.com/scipy/scipy/issues/22236 . We triggered this bug by including scipy.signal in #797. T... — committed to google-deepmind/torax by jcitrin 2 years ago
- Fix pytest test error There is a known bug between scipy 1.15 and pytest when assert-rewrite. https://github.com/scipy/scipy/issues/22236 . We triggered this bug by including scipy.signal in #797. T... — committed to google-deepmind/torax by jcitrin 2 years ago
- BUG: add workaround for pytest assertion rewriting overreach closes gh-22236 see discussion of upstream bug from https://github.com/pytest-dev/pytest/issues/10845#issuecomment-2575032929 — committed to lucascolley/scipy by lucascolley 2 years ago
- BUG: add workaround for pytest assertion rewriting overreach closes gh-22236 see discussion of upstream bug from https://github.com/pytest-dev/pytest/issues/10845#issuecomment-2575032929 — committed to lucascolley/scipy by lucascolley 2 years ago
- blacklist scipy 1.15.{0,1,2} due to https://github.com/scipy/scipy/issues/22236 — committed to bkmgit/pymor by sdrave a year ago
Is there any update for this issue? The problem is that if I downgrade scipy to version 1.14.1, the issue will be resolved. However, in the current two releases of 1.15.0 and 1.15.1, the issue is existed and not resolved.
Perhaps changing
__all__ = [s for s in dir() if not s.startswith('_')]to also exclude strings starting with@would work.I found a solution @bmaranville , you can add this to your pytest.ini:
Be aware that pytest does the rewrite of asserts to give more debug information, so you might want to be careful. I have not seen any difference on my runs.
Yes, that import is what introduced the changed behaviour. But to me, it seems like a pytest bug that the failures are caused. I pointed to some potentially relevant pytest docs above, but if it is the case that this kind of import cannot work with this config, then it would be good for pytest to document that, and provide an alternative solution.
Thank you, it was not clear how to trigger the error, and now it is.
It is quite bizzare indeed. Using a scratch directory
and running
$ pytest . --pdbdoes trigger an OP error; Apparently, pytest injects some magic names:The crux of the problem seems to be in your
pytest.ini: changing it to list the python module explicitly avoids the error for me locally:Whether this is a scipy bug: I don’t think so. We’re not doing anything that magical or hacky in
ndimage/_supports_alternative_backends.py.So I think the answer is do not ask pytest to “rewrite assertions” in the scipy sources (or any non-test sources for that matter). Those should not contain assertions worth rewriting anyway. I’d suspect that
python_files=*is nuclear with any deep enough python dependency stack.EDIT: indeed, the scipy code that fails in this scenario is new in scipy==1.5.0, so I can see how it looks like scipy 1.5.0 breaks your code. My point however is that if you were asking pytest to “rewrite assertions” across the whole scipy codebase, it only worked by accident.
The traceback came from running
pytest -vin a directory containingpytest.iniand a single source file containing the singleimportline above… in what way do they not match?