Repository navigation
Report an unimportable example as one failing test, not a collection error - #502
Merged
Merged
Conversation
…error
`get_models()` runs inside the `@pytest.mark.parametrize` decorator, so every
`model.py` under `examples/` is imported while pytest is still collecting. The
import is unguarded, so one `ImportError` propagates out of collection and
pytest stops with
ERROR test_examples.py
Interrupted: 1 error during collection
That is not one failing example. It is zero tests run, for every example in
the repository, and the summary line says nothing about the other 25.
This is what made the recent breakages invisible rather than merely broken.
The warehouse example's stale `mesa.meta_agents` import (mesa#499) aborted
collection, so mesa's own `examples` job went red and stayed red for 24
consecutive runs on `main` without ever reporting on the examples that were
fine. A new breakage introduced during that window would have looked
identical to the standing one.
Guard the import and turn a failure into a single parametrized case that
fails with the underlying exception, keyed by module name. One broken example
now costs exactly one red test.
Demonstrated on 9112701 by adding a throwaway example whose `model.py`
imports a missing module:
before 1 error in 11.87s (Interrupted during collection, 0 ran)
after 1 failed, 25 passed (FAILED ...[examples.zz_broken_demo.model])
With a healthy tree the behaviour is unchanged: 25 passed, and the test ids
for working examples stay as they were (`test_model_steps[AcoTspModel]`).
Co-Authored-By: Claude Opus 5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The gap
test_examples.pybuilds its parameter list inside the decorator:@pytest.mark.parametrize("model_class", get_models("examples"))get_models()callsimportlib.import_module()on everymodel.pyunderexamples/, unguarded, while pytest is still collecting. OneImportErrortherefore propagates out of collection and pytest stops:That is not one failing example — it is zero tests run, for all 26, and the summary says nothing about the 25 that are fine.
This is the amplifier behind the recent run of breakages. The warehouse example's stale
mesa.meta_agentsimport (fixed in #499) aborted collection, so mesa's ownexamplesjob was red for 24 consecutive runs onmain(2026-10-22 → 2026-10-28) while reporting nothing about any other example. A genuinely new breakage introduced in that window would have been indistinguishable from the standing one, and PRs merged throughout.Reproducing it
On
9112701, add one throwaway example whose import fails:1 error in 11.87s—Interrupted: 1 error during collection, 0 tests ran1 failed, 25 passed—FAILED test_examples.py::test_model_steps[examples.zz_broken_demo.model]The change
Guard the import and append a parametrized case carrying the exception, keyed by module name. The test fails with the real cause:
Twelve added lines, no new dependency, no change to how healthy examples are collected or named — the ids stay
test_model_steps[AcoTspModel]and a healthy tree is still25 passed.Why this shape
The alternative is to parametrize over module paths and import inside the test, which also works but collapses every model class in a module into a single test and renames all 25 existing ids. Guarding the import keeps the current granularity and touches one function.
What I deliberately left out
No fixture-based regression test for this behaviour. A permanently-broken example committed under
examples/would be collected as a real example and fail forever, so the fixture would have to live outside that tree with its ownos.walkroot — more machinery than the twelve lines it guards. The manual reproduction above is in the commit message so the next person can re-run it in thirty seconds. Happy to add it properly if you would rather have it enforced.