Skip to content

Fix the docstring examples that cannot run under doctest - #4010

Merged
quaquel merged 2 commits into
mesa:mainfrom
lahari936:fix-broken-docstring-examples
Oct 28, 2026
Merged

quaquel merged 2 commits into
mesa:mainfrom
lahari936:fix-broken-docstring-examples

Conversation

@lahari936

Copy link
Copy Markdown
Contributor

Split out of #3864 at @quaquel's request, so the docstring repairs and the CI job that guards them can be reviewed separately. This is half 1 of 2; the CI job follows in its own PR and depends on this one.

What is broken

Ten modules under mesa/ carry >>> examples. Eight of them fail the moment they are collected by pytest --doctest-modules, and none of the failures is a typo in prose — each is an example that cannot execute as written:

module problem
visualization/components/portrayal_components.py continuation lines start with >>> instead of ..., so doctest feeds each continuation to the interpreter as a fresh statement and gets IndentationError
experimental/meta_agents/meta_agents_api.py create and at_level use [...] placeholders where a list of agents belongs, and never bind add_member's return value, which then appears as unexpected output
visualization/solara_viz.py, visualization/command_console.py the examples build Solara components or need a live UI callback, so they cannot run headless

What this changes

  • portrayal_components.py — continuations fixed to ....
  • meta_agents_api.py — both examples made self-contained, with setup taken from docs/apis/meta_agents.md and the [...] placeholders replaced by real agents.
  • solara_viz.py / command_console.py — the two genuinely un-runnable examples marked # doctest: +SKIP, and ModelCreator's dict literal continuation fixed to ....

Every edit is inside a docstring. The diff contains no changed code lines.

Verification

pytest --doctest-modules mesa --ignore=mesa/examples
result
main @ b4c2fbdd 8 failed, 2 passed
this branch 8 passed, 2 skipped

pytest tests/ is unaffected: 1006 passed. The one unrelated failure in that run, test_actions.py::TestOnIdle::test_suppressed_wake_is_logged_at_debug, is pre-existing test-ordering pollution — the Boltzmann example app calls log_to_stderr(..., propagate=False) at import, which mutates the global MESA logger and breaks caplog for any test that runs after it. It passes when tests/experimental/test_actions.py is run on its own.

Why mesa/examples stays out of scope

Noted here because the follow-up CI job excludes it: it has no doctests, every app.py builds a model and a renderer at import time, and st_app.py imports streamlit, which is not a dependency.

Part of #3825. Replaces the docstring half of #3864.

Ten modules under `mesa/` carry `>>>` examples, and eight of them fail when
collected with `pytest --doctest-modules`. None of the failures are typos in
prose; each is an example that cannot execute as written:

- `portrayal_components.py`: continuation lines start with `>>>` instead of
  `...`, so doctest hands the continuation to the interpreter as a fresh
  statement and it raises `IndentationError`.
- `meta_agents_api.py`: the `create` and `at_level` examples use `[...]`
  placeholders where a list of agents belongs, and discard nothing from
  `add_member`, whose return value then shows up as unexpected output. Both
  are now self-contained, with the setup taken from
  `docs/apis/meta_agents.md`.
- `solara_viz.py` and `command_console.py`: the examples build Solara
  components or need a live UI callback, so they cannot run headless. They
  are marked `# doctest: +SKIP`, and `ModelCreator`'s dict literal
  continuation is fixed to `...`.

`pytest --doctest-modules mesa --ignore=mesa/examples` goes from 8 failed /
2 passed to 8 passed / 2 skipped. No behaviour changes: every edit is inside
a docstring.

This is the first half of mesa#3864, split on request so each half can be
reviewed on its own. The CI job that keeps these green is the second half.

Part of mesa#3825.

Co-Authored-By: Claude Opus 5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 28, 2026 02:29

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@github-actions github-actions Bot added experimental Release notes label visualization labels Oct 28, 2026
@lahari936

Copy link
Copy Markdown
Contributor Author

Half 1 of 2 from the #3864 split. The CI job that keeps these examples green is #4011, which depends on this PR.

Heads-up on the red checks: they are not from this change. pytest dies at plugin load before collecting a single test:

Error while loading plugin "pytest11".
  solara/test/pytest_plugin.py:29: from solara.server.starlette import ServerStarlette
  solara/server/starlette.py:27:  import websockets.legacy.http
E DeprecationWarning: websockets.legacy is deprecated

pytest 9.2.0 landed #12697, which makes warnings raised while importing entry-point plugins go through the normal filter pipeline. Mesa runs pytest -Werror, solara's autoloaded plugin imports websockets.legacy, and the DeprecationWarning is now promoted to an error, so the run exits 3 with zero tests executed.

It is repo-wide and not specific to any PR — build (ubuntu, 3.14) was green on main at 2026-10-27 10:15 and has failed on every run since. I will file it separately.

@quaquel quaquel added docs Release notes label testing Release notes label doctest Release note label and removed experimental Release notes label visualization docs Release notes label testing Release notes label labels Oct 28, 2026
@github-actions github-actions Bot added experimental Release notes label visualization labels Oct 28, 2026
@quaquel
quaquel merged commit 6afb97d into mesa:main Oct 28, 2026
14 of 15 checks passed
lahari936 added a commit to lahari936/mesa that referenced this pull request Oct 28, 2026
`mesa/` carries `>>>` examples in ten modules, and nothing runs them, so
they rot silently. Add a `doctest` job to build_lint.yml that runs

    pytest --doctest-modules mesa --ignore=mesa/examples

on Python 3.14, matching the existing coverage job.

It is a separate job rather than part of the `build` matrix or the docs
build so that a rotten docstring is reported on its own, instead of
appearing as a unit-test failure or a docs-build failure. That is
@falloficaruss's suggestion on mesa#3825.

`mesa/examples` is excluded: it has no doctests, every `app.py` builds a
model and a renderer at import time, and `st_app.py` imports streamlit,
which is not a dependency.

`pyproject.toml` gains `doctest_optionflags = ["ELLIPSIS",
"NORMALIZE_WHITESPACE"]` under `[tool.pytest.ini_options]`. There is no
`addopts`, so the normal suite is untouched: `pytest tests/ --collect-only`
reports 1016 tests both with and without the block.

This is the second half of mesa#3864, split on request. The first half, the
docstring repairs in mesa#4010, is merged, so this job is green on arrival.

Closes mesa#3825.

Co-Authored-By: Claude Opus 5 <[email protected]>
quaquel pushed a commit that referenced this pull request Oct 28, 2026
`mesa/` carries `>>>` examples in ten modules, and nothing runs them, so
they rot silently. Add a `doctest` job to build_lint.yml that runs

    pytest --doctest-modules mesa --ignore=mesa/examples

on Python 3.14, matching the existing coverage job.

It is a separate job rather than part of the `build` matrix or the docs
build so that a rotten docstring is reported on its own, instead of
appearing as a unit-test failure or a docs-build failure. That is
@falloficaruss's suggestion on #3825.

`mesa/examples` is excluded: it has no doctests, every `app.py` builds a
model and a renderer at import time, and `st_app.py` imports streamlit,
which is not a dependency.

`pyproject.toml` gains `doctest_optionflags = ["ELLIPSIS",
"NORMALIZE_WHITESPACE"]` under `[tool.pytest.ini_options]`. There is no
`addopts`, so the normal suite is untouched: `pytest tests/ --collect-only`
reports 1016 tests both with and without the block.

This is the second half of #3864, split on request. The first half, the
docstring repairs in #4010, is merged, so this job is green on arrival.

Closes #3825.

Co-authored-by: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doctest Release note label experimental Release notes label visualization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants