PyWavelets / pywt

PyWavelets - Wavelet Transforms in Python
http://pywavelets.readthedocs.org
MIT License
1.97k stars 460 forks source link

Bug: cannot run tests with an editable installation of `pyWavelets`, possible `pytesttester` test collection discrepancy? #700

Closed agriyakhetarpal closed 2 months ago

agriyakhetarpal commented 4 months ago

Description

Hi, I'm trying to run the test suite using pytest against an editable installation of pyWavelets/pywt, and it looks like the PytestTester class in the pywt/_pytesttester.py module is not configured to run against on editable installations (with the -e . command-line flag) in the same way it seems to run in CI for non-editable installations (pip install .)

MWE, i.e., steps to reproduce

In a terminal, run

pip install numpy==1.23.3 cython meson-python  # Python 3.11, build-time requirements sourced from the build-system table in pyproject.toml
pip install -e .
pip install pytest

and then,

  1. pytest --pyargs pywt does not work and returns the following trace:
Expand to view logs

```bash ====================================================================== test session starts ====================================================================== platform darwin -- Python 3.11.7, pytest-8.0.1, pluggy-1.4.0 rootdir: /Users/agriyakhetarpal/Desktop/Work/pywt configfile: pytest.ini collected 0 items / 1 error ============================================================================ ERRORS ============================================================================= _______________________________________________________________ ERROR collecting pywt/__init__.py _______________________________________________________________ venv/lib/python3.11/site-packages/pluggy/_hooks.py:501: in __call__ return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult) firstresult = True kwargs = {'collector': , 'name': 'test', 'obj': } self = venv/lib/python3.11/site-packages/pluggy/_manager.py:119: in _hookexec return self._inner_hookexec(hook_name, methods, kwargs, firstresult) firstresult = True hook_name = 'pytest_pycollect_makeitem' kwargs = {'collector': , 'name': 'test', 'obj': } methods = [>] self = <_pytest.config.PytestPluginManager object at 0x105633550> venv/lib/python3.11/site-packages/_pytest/python.py:260: in pytest_pycollect_makeitem warnings.warn_explicit( E pytest.PytestCollectionWarning: cannot collect 'test' because it is not a function. collector = filename = PosixPath('/Users/agriyakhetarpal/Desktop/Work/pywt/pywt/_pytesttester.py') lineno = 72 name = 'test' obj = ==================================================================== short test summary info ==================================================================== ERROR pywt/__init__.py - pytest.PytestCollectionWarning: cannot collect 'test' because it is not a function. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ``` With `pywt.test()`, there is another error on editable installations: ```python 113 import pytest 115 module = sys.modules[self.module_name] --> 116 module_path = os.path.abspath(module.__path__[0]) 118 # setup the pytest arguments 119 pytest_args = ["-l"] IndexError: list index out of range ```

Workarounds

However, for now,

  1. pytest .,
  2. pytest pywt

seem to work as usual and proceed with the collection of 1036 tests in total. These commands have been tested in a fresh, virtualenv-based virtual environment with Python 3.11.7 on a macOS M-series machine.

Additional notes

The bug seems to be coming from the --pyargs configuration in pywt/_pytesttester.py here:

https://github.com/PyWavelets/pywt/blob/e69b126c096868b0ea7650d38ed11cd95a9dc182/pywt/_pytesttester.py#L150-L156

which I'm not totally sure about, but it seems to be searching for test inside pywt/__init__.py as an importable Python module – while it is an instance of the PytestTester class.

rgommers commented 4 months ago

Thanks for the report @agriyakhetarpal. I believe the second error (IndexError from pywt.test()) will be resolved by https://github.com/mesonbuild/meson-python/pull/569. Could you please confirm that?

agriyakhetarpal commented 4 months ago

Thanks for the resource! I installed the changes made in the PR via

pip install git+https://github.com/dnicolodi/meson-python.git@editable-module-path

and recompiled with meson-python (version 0.16.0dev0) via pip install -e . --no-build-isolation, and now all of the three commands, i.e.,

  1. pytest / pytest . / pytest pywt
  2. pytest --pyargs pywt, and
  3. python -c "import pywt; pywt.test()"

are working on editable installations of pywt. I can confirm that https://github.com/mesonbuild/meson-python/pull/569 resolves the issue entirely.

rgommers commented 4 months ago

Awesome. Then I'll focus on getting that merged and released, and once that is out in meson-python 0.16.0, we can close this issue.