Repository navigation
Disable tests using matplotlib on pyodide - #8082
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughMatplotlib availability flag ( Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
|
||
| from skimage._shared.testing import run_in_parallel | ||
| from skimage._shared._dependency_checks import has_mpl | ||
| from ...dependency_checks import has_mpl |
There was a problem hiding this comment.
I'm a bit surprised that this works, given that we don't install tests as a package. But, if it works for both editable and non-editable installs, then all good.
There was a problem hiding this comment.
Maybe because we set --import-mode=importlib in our pyproject.toml?
|
|
||
| # pyodide maintainers suggest not running tests that use matplotlib, | ||
| # https://github.com/pyodide/pyodide-recipes/issues/475#issuecomment-4020982042 | ||
| has_mpl = "pyodide" not in sys.modules and is_installed("matplotlib", ">=3.3") |
There was a problem hiding this comment.
Why not use the same check as in is_wasm? Not sure how robust sys.modules checking is.
There was a problem hiding this comment.
This is how the pyodide docs suggests doing it: https://pyodide.org/en/latest/usage/faq.html#how-to-detect-that-code-is-run-with-pyodide. This isn't a generic issue with wasm runtimes but a specific issue with pyodide's matplotlib package.
|
|
||
| # pyodide maintainers suggest not running tests that use matplotlib, | ||
| # https://github.com/pyodide/pyodide-recipes/issues/475#issuecomment-4020982042 | ||
| has_mpl = "pyodide" not in sys.modules and is_installed("matplotlib", ">=3.3") |
There was a problem hiding this comment.
Optional: Might be worth renaming to signal that the flag is no longer only about matplotlib being available. Not sure if mpl_is_supported is clearer?
There was a problem hiding this comment.
I'll do it, I think I have a way to simplify things and make pytest's skip reasons make sense.
There was a problem hiding this comment.
See the latest commit
de9b684 to
9962123
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 9a69c6a3-173b-4e4c-aee9-514055429af4
📒 Files selected for processing (5)
tests/dependency_checks.pytests/skimage/draw/test_draw.pytests/skimage/feature/test_util.pytests/skimage/filters/test_thresholding.pytests/skimage/io/test_plugin.py
🚧 Files skipped from review as they are similar to previous changes (3)
- tests/skimage/draw/test_draw.py
- tests/skimage/filters/test_thresholding.py
- tests/skimage/io/test_plugin.py
| is_pyodide = "pyodide" in sys.modules | ||
| has_mpl = is_installed("matplotlib", ">=3.3") | ||
|
|
||
| uses_matplotlib = pytest.mark.skipif(not has_mpl, reason="matplotlib not installed")( | ||
| pytest.mark.skipif( | ||
| is_pyodide, | ||
| reason=( | ||
| "importing matplotlib fails randomly. " | ||
| "See https://github.com/pyodide/pyodide-recipes/issues/475" | ||
| ), | ||
| ) | ||
| ) |
There was a problem hiding this comment.
🧩 Analysis chain
🌐 Web query:
According to current pytest docs/source, what does pytest.mark.skipif(...)(pytest.mark.skipif(...))produce: a composed decorator, or a singleskipif marker with the inner marker stored as a positional argument?
💡 Result:
It produces a single skipif MarkDecorator whose underlying Mark.args contains the inner MarkDecorator as a positional argument, not a composed decorator.
Reason: MarkDecorator.__call__ only “decorates” when the sole positional argument is a test function (per istestfunc) or a class; otherwise it falls back to with_args(*args, **kwargs), which stores that argument in the marker’s args. In pytest.mark.skipif(...)(pytest.mark.skipif(...)), the inner skipif(...) object is a MarkDecorator, not a test function/class, so it goes through with_args and gets stored as a positional argument. [1]
This matches the docs’ note that passing a single callable-like object doesn’t necessarily become an argument unless you use with_args; the __call__ path is special-cased for decorating test functions/classes. [2]
Sources: [1] [2]
🏁 Script executed:
cat -n src/skimage/_shared/version_requirements.py | head -80Repository: scikit-image/scikit-image
Length of output: 3139
The marker composition is invalid and the helper still triggers the flaky import path on Pyodide.
Line 8 calls is_installed("matplotlib", ">=3.3"), which imports matplotlib via __import__() in get_module_version() to read its version. On Pyodide, this executes at collection time before any skip can be applied, so collection remains flaky.
Additionally, pytest.mark.skipif(...)(pytest.mark.skipif(...)) does not create a composed decorator. When MarkDecorator.__call__ receives a non-test object (here, another MarkDecorator), it stores that object as a positional argument rather than decorating with it. The result is a single skipif marker that will apply when any of its stored conditions are true, so @uses_matplotlib does not implement the intended two-skip logic.
🔧 Proposed fix
is_pyodide = "pyodide" in sys.modules
-has_mpl = is_installed("matplotlib", ">=3.3")
-
-uses_matplotlib = pytest.mark.skipif(not has_mpl, reason="matplotlib not installed")(
- pytest.mark.skipif(
- is_pyodide,
- reason=(
- "importing matplotlib fails randomly. "
- "See https://github.com/pyodide/pyodide-recipes/issues/475"
- ),
- )
-)
+has_mpl = False if is_pyodide else is_installed("matplotlib", ">=3.3")
+
+def uses_matplotlib(test_func):
+ if is_pyodide:
+ return pytest.mark.skip(
+ reason=(
+ "importing matplotlib fails randomly. "
+ "See https://github.com/pyodide/pyodide-recipes/issues/475"
+ )
+ )(test_func)
+ if not has_mpl:
+ return pytest.mark.skip(reason="matplotlib not installed")(test_func)
+ return test_funcThere was a problem hiding this comment.
♻️ Duplicate comments (1)
tests/dependency_checks.py (1)
7-8:⚠️ Potential issue | 🔴 CriticalShort-circuit
has_mplon Pyodide.
is_installed("matplotlib", ">=3.3")still runs during module import, so collection can hit the flaky matplotlib import on Pyodide beforeuses_matplotlibever skips the test. Makehas_mplconditional onnot is_pyodide.🐛 Proposed fix
is_pyodide = "pyodide" in sys.modules -has_mpl = is_installed("matplotlib", ">=3.3") +has_mpl = False if is_pyodide else is_installed("matplotlib", ">=3.3")Run this to verify whether
is_installed()imports the target module while resolving its version. Expected result:get_module_versionuses an import path, confirming Line 8 still touches matplotlib at collection time.#!/bin/bash set -e rg -n -C3 'def get_module_version|def is_installed|__import__|import_module' src/skimage/_shared/version_requirements.py
🧹 Nitpick comments (1)
tests/dependency_checks.py (1)
11-21: Add a numpydoc docstring touses_matplotlib.This helper now centralizes test gating across multiple modules, but it does not document its Pyodide short-circuit or decorator behavior.
📝 Suggested docstring
def uses_matplotlib(test_func): + """Skip tests that require matplotlib when it is unavailable or unsupported. + + Parameters + ---------- + test_func : callable + Test function to decorate. + + Returns + ------- + callable + The original test function, or a skipped variant when running on + Pyodide or when ``matplotlib >= 3.3`` is unavailable. + """ if is_pyodide: return pytest.mark.skip(As per coding guidelines,
**/*.py: All code should be documented to the same standard as NumPy and SciPy using numpydoc format.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 73cec661-7d16-4733-bb87-dd450c204c97
📒 Files selected for processing (1)
tests/dependency_checks.py
|
Looks like the CI failure is due to a network issue on the CI runner. I think this is OK to merge. |
|
Sorry, @ngoldbaum, looks like we ran into some merge conflicts now; I will approve, and if you merge with main we can merge your PR immediately. |
|
@stefanv should be good to merge now. |
|
Reviews (1): Last reviewed commit: "Merge branch 'main' into disable-mpl-on-..." | Re-trigger Greptile |
| import sys | ||
| import pytest | ||
| from _skimage2._shared.version_requirements import is_installed |
There was a problem hiding this comment.
Relative imports without
__init__.py will fail
All of the changed test files now import from this module using relative imports (e.g. from ...dependency_checks import uses_matplotlib), but no __init__.py files exist anywhere under tests/. Without them, Python cannot resolve the package hierarchy these relative imports assume, and every affected test module will raise ImportError: attempted relative import with no known parent package at collection time.
Since tests/ is on the collection path (not imported as a package via --pyargs), the simplest fix is to use an absolute import in the test files — e.g. from dependency_checks import uses_matplotlib — or to add __init__.py files to establish the package structure the relative imports require.
| import pytest | ||
|
|
||
| from _skimage2._shared._dependency_checks import has_mpl | ||
| from ...dependency_checks import has_mpl |
There was a problem hiding this comment.
Inconsistent migration:
has_mpl imported instead of uses_matplotlib
Every other test file in this PR was migrated to the new uses_matplotlib decorator, but test_radon_transform.py still imports the raw has_mpl boolean. The usages of has_mpl here are inside if debug and has_mpl: guards within helper functions rather than as test-function decorators, so the existing matplotlib calls are not skipped on pyodide. If the debug flag is ever enabled in a pyodide CI run, matplotlib would still be imported and could hit the intermittent failure that motivated this PR.
Consider replacing those guards with the uses_matplotlib decorator on any test that calls the debug helper, or at a minimum noting why this file is intentionally excluded from the pyodide skip logic.
|
Thanks very much! |
* origin/main: Port tests for `features.canny` to skimage2 (#8126) Replace NumPy global RNG use in tests with local seeded RNGs (#8084) Disable tests using matplotlib on pyodide (#8082) Test with pythonoptimize (#8108) Add skimage2 porting decorator (#8114) Drop 3.13t builds from cibuildwheel configuration (#8127) Restore wording of AI policy (#8123)
I noticed a CI flake on my other PR in the pyodide CI on Friday. When I asked about it on the pyodide issue tracker, one of the maintainers suggested disabling tests that use matplotlib on pyodide.
I noticed that
has_mplis only used in the tests, so I moved it into a test-onlydependency_checks.py, outside of the skimage module. Happy to move it back if you'd prefer I not do that, but I thought it made sense to have a distinction between dependency checks needed for test runs and dependency checks needed for scikit-image runtime behavior.