Skip to content

TST fix uncollected tests and avoidable warnings in the test suite - #35476

Merged
virchan merged 2 commits into
scikit-learn:mainfrom
lorentzenchr:test_warnings
Nov 25, 2026
Merged

virchan merged 2 commits into
scikit-learn:mainfrom
lorentzenchr:test_warnings

Conversation

@lorentzenchr

@lorentzenchr lorentzenchr commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

Toward #5685.

What does this implement/fix? Explain your changes.

On main, pytest -W error emits avoidable warnings:

  • bugfix: _mark_no_check_spmatrix_if_pytest_imported in test_estimator_checks.py
    returned pytest.mark.no_check_spmatrix instead of
    pytest.mark.no_check_spmatrix(f). As a consequence, test_check_estimator
    and test_check_estimator_clones were replaced by a MarkDecorator and
    silently not collected (introduced in Enable config setting sparse_interface to control sparray and spmatrix creation #31177). Both tests are collected
    again and pass.
  • _tested_estimators now skips SKIPPED_ESTIMATORS (FrozenEstimator)
    directly instead of triggering a SkipTestWarning while collecting
    test_common.py. The set of collected tests is unchanged.
  • check_warnings_as_errors passed match="" to pytest.raises, which
    always matches and emits a PytestWarning. It now passes match=None.
  • test_spectral_clustering_np_matrix_raises silences numpy's
    PendingDeprecationWarning around the deliberate np.matrix creation.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

I prompted the AI (Claude Fable 5.2) to fix pytest -W error and reviewed all changes.

Any other comments?

One could argue that this closes #5685.

Details `SKLEARN_WARNINGS_AS_ERRORS=1` (#28372) already turns `DeprecationWarning`, `FutureWarning` and `VisibleDeprecationWarning` into errors in CI, which is what the discussion in the issue converged on.

A literal pytest -W error is a different goal. With this PR and
-W "ignore::sklearn.exceptions.SkipTestWarning", a full local run (macOS,
Python 3.13, numpy 2.4.2, scipy 1.17.0, pytest 9.0.2) gives 1466 failed,
39786 passed. Almost all failures are warnings that are emitted on purpose
for the inputs used in the tests:

Category Count
UserWarning 577
RuntimeWarning (353 are "invalid value encountered in cast") 445
ConvergenceWarning 264
UndefinedMetricWarning 44
EfficiencyWarning / SparseEfficiencyWarning 36
other ~100

About 40% come from two parametrized tests,
test_set_output_transform_configured and
test_classification_inf_nan_input. Making these pass would mean catching
expected warnings in about 80 test files, and is better tracked in a new,
dedicated issue if we want it.

Notes

Not changed here, but noticed along the way:

  • NeighborsBase raises a SyntaxWarning when p is given in
    metric_params. The category is unusual, but it is asserted in
    test_metric_params_interface and in a DBSCAN test, so changing it is an
    API decision.
  • test_sklearn_warnings_as_errors cannot pass under a blanket -W error
    by construction, since it checks that ignored warnings are recorded when
    SKLEARN_WARNINGS_AS_ERRORS is unset.

- Fix `_mark_no_check_spmatrix_if_pytest_imported`, which returned the bare
  mark instead of the marked function. `test_check_estimator` and
  `test_check_estimator_clones` were therefore never collected.
- Skip `SKIPPED_ESTIMATORS` in `_tested_estimators` instead of emitting a
  `SkipTestWarning` at collection time.
- Pass `match=None` instead of `match=""` to `pytest.raises` in
  `check_warnings_as_errors` to avoid a `PytestWarning`.
- Silence numpy's `PendingDeprecationWarning` when deliberately creating an
  `np.matrix` in `test_spectral_clustering_np_matrix_raises`.

@cakedev0 cakedev0 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.

Nice, LGTM

@lorentzenchr

Copy link
Copy Markdown
Member Author
pytest -W error -W "ignore::sklearn.exceptions.SkipTestWarning"

results in 1460 failed, 39792 passed, 15168 skipped, 186 xfailed, 98 xpassed in 1424.85s (0:23:44)

The good news is that with this PR we can see those warnings as errors if we want to.

Examples
FAILED sklearn/cluster/tests/test_affinity_propagation.py::test_affinity_propagation_equal_points - UserWarning: All samples have mutually equal similarities. Returning arbitrary cluster center(s).

FAILED sklearn/decomposition/tests/test_nmf.py::test_nmf_smoke[cd-tall-nndsvd] - sklearn.exceptions.ConvergenceWarning: Maximum number of iterations 200 reached. Increase it to improve convergence.

FAILED sklearn/model_selection/tests/test_validation.py::test_cross_val_predict_unbalanced - UserWarning: The least populated class in y has only 1 members, which is less than n_splits=2.

FAILED sklearn/utils/tests/test_testing.py::test_sklearn_warnings_as_errors[warning_info13] - DeprecationWarning: vert. bool was deprecated in Matplotlib

@virchan virchan 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.

LGTM as well! Thanks everyone!

Merging.

@virchan
virchan merged commit 46a731c into scikit-learn:main Nov 25, 2026
39 checks passed
@lorentzenchr
lorentzenchr deleted the test_warnings branch November 25, 2026 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Aim to raise warnings as errors in test suite

3 participants