Skip to content

Disable tests using matplotlib on pyodide - #8082

Merged
stefanv merged 6 commits into
scikit-image:mainfrom
ngoldbaum:disable-mpl-on-pyodide
Apr 25, 2026
Merged

stefanv merged 6 commits into
scikit-image:mainfrom
ngoldbaum:disable-mpl-on-pyodide

Conversation

@ngoldbaum

Copy link
Copy Markdown
Contributor

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_mpl is only used in the tests, so I moved it into a test-only dependency_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.

@coderabbitai

coderabbitai Bot commented Mar 9, 2026 •

Copy link
Copy Markdown

Note

Reviews paused

It 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 reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Matplotlib availability flag (has_mpl) was removed from the shared dependency checks; tests now provide a local uses_matplotlib guard (based on is_installed and Pyodide detection). Tests were updated to import and apply uses_matplotlib instead of relying on the removed has_mpl signal.

Changes

Cohort / File(s) Summary
Shared dependency checks
src/skimage/_shared/_dependency_checks.py
Removed import/use of is_installed/version_requirements and the has_mpl matplotlib availability flag; kept is_wasm.
Test utilities / guards
tests/dependency_checks.py
Added a test-side guard: imports is_installed from skimage._shared.version_requirements, detects Pyodide via sys.modules, computes matplotlib availability for matplotlib>=3.3, and exposes a uses_matplotlib decorator to skip tests when appropriate.
Tests updated to use test guard
tests/skimage/draw/test_draw.py, tests/skimage/feature/test_util.py, tests/skimage/filters/test_thresholding.py, tests/skimage/io/test_plugin.py
Replaced pytest.mark.skipif(not has_mpl, ...) usages with @uses_matplotlib decorators imported from the new test utility; gating behavior preserved but relocated.
Import path tweak
tests/skimage/transform/test_radon_transform.py
Adjusted import path to reference the tests' dependency checks (switched import to the test-side location).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Disable tests using matplotlib on pyodide' is concise, descriptive, and directly reflects the main objective of the PR to skip matplotlib tests on the pyodide platform.
Description check ✅ Passed The description explains the motivation (CI flake on pyodide), the solution taken (moving has_mpl to test-only dependency_checks.py), and includes relevant context with links to the CI failure and pyodide discussion.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Comment thread tests/skimage/draw/test_draw.py Outdated

from skimage._shared.testing import run_in_parallel
from skimage._shared._dependency_checks import has_mpl
from ...dependency_checks import has_mpl

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe because we set --import-mode=importlib in our pyproject.toml?

Comment thread tests/dependency_checks.py Outdated

# 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")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the same check as in is_wasm? Not sure how robust sys.modules checking is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK great, thanks for checking!

Comment thread tests/dependency_checks.py Outdated

# 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")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lagru Feel free to push a rename and merge.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do it, I think I have a way to simplify things and make pytest's skip reasons make sense.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the latest commit

@ngoldbaum
ngoldbaum force-pushed the disable-mpl-on-pyodide branch from de9b684 to 9962123 Compare March 10, 2026 16:32

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0ff1690 and 9962123.

📒 Files selected for processing (5)
  • tests/dependency_checks.py
  • tests/skimage/draw/test_draw.py
  • tests/skimage/feature/test_util.py
  • tests/skimage/filters/test_thresholding.py
  • tests/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

Comment thread tests/dependency_checks.py Resolved

@stefanv stefanv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever, thanks Nathan!

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 589bbf55-1ae0-44c4-887a-e1ae94f44548

📥 Commits

Reviewing files that changed from the base of the PR and between 9962123 and 665c70e.

📒 Files selected for processing (1)
  • tests/dependency_checks.py

Comment thread tests/dependency_checks.py Outdated
Comment on lines +7 to +18
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"
),
)
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 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 -80

Repository: 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_func

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
tests/dependency_checks.py (1)

7-8: ⚠️ Potential issue | 🔴 Critical

Short-circuit has_mpl on Pyodide.

is_installed("matplotlib", ">=3.3") still runs during module import, so collection can hit the flaky matplotlib import on Pyodide before uses_matplotlib ever skips the test. Make has_mpl conditional on not 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_version uses 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 to uses_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

📥 Commits

Reviewing files that changed from the base of the PR and between 82a5da6 and 4c14a41.

📒 Files selected for processing (1)
  • tests/dependency_checks.py

@ngoldbaum

Copy link
Copy Markdown
Contributor Author

Looks like the CI failure is due to a network issue on the CI runner. I think this is OK to merge.

@stefanv

stefanv commented Apr 23, 2026

Copy link
Copy Markdown
Member

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.

@ngoldbaum

Copy link
Copy Markdown
Contributor Author

@stefanv should be good to merge now.

@greptile-apps

greptile-apps Bot commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "Merge branch 'main' into disable-mpl-on-..." | Re-trigger Greptile

Comment on lines +1 to +3
import sys
import pytest
from _skimage2._shared.version_requirements import is_installed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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.

@stefanv
stefanv merged commit a7ac8e8 into scikit-image:main Apr 25, 2026
22 checks passed
@stefanv

stefanv commented Apr 25, 2026

Copy link
Copy Markdown
Member

Thanks very much!

@stefanv stefanv added this to the 0.27 milestone Apr 25, 2026
matthew-brett added a commit that referenced this pull request Apr 27, 2026
* 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants