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

Most upvoted comments

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:

[pytest]
addopts = --assert=plain
python_files = *.py
python_functions = test_* _test_*

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

br@gonzales:~/temp/pytest_scipy_1.15$ ll
total 32
drwxrwxr-x  2 br br  4096 янв  4 16:17 ./
drwxrwxr-x 11 br br 20480 янв  4 16:06 ../
-rw-rw-r--  1 br br    60 янв  4 16:11 failed.py
-rw-rw-r--  1 br br    35 янв  4 16:15 pytest.ini
br@gonzales:~/temp/pytest_scipy_1.15$ cat failed.py 
from scipy import signal
br@gonzales:~/temp/pytest_scipy_1.15$ cat pytest.ini 
[pytest]
python_files = *.py

and running $ pytest . --pdb does trigger an OP error; Apparently, pytest injects some magic names:

(Pdb) p dir(_delegators)
['@py_builtins', '@pytest_ar', '__builtins__', # the rest is OK
]

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:

$ cat pytest.ini 
[pytest]
python_files = failed.py

$ pytest . --pdb
================================= test session starts ==================================
platform linux -- Python 3.12.4, pytest-8.2.2, pluggy-1.5.0
rootdir: /home/br/temp/pytest_scipy_1.15
configfile: pytest.ini
plugins: cov-5.0.0, hypothesis-6.108.0, json-report-1.5.0, xdoctest-1.2.0, xdist-3.6.1, anyio-4.4.0, scipy_doctest-1.6.dev0, timeout-2.3.1, metadata-3.1.1
collected 0 items                                                                      

================================ no tests ran in 0.53s =================================

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 -v in a directory containing pytest.ini and a single source file containing the single import line above… in what way do they not match?