Skip to content

Run docstring doctests in CI via pytest --doctest-modules - #3864

Closed
lahari936 wants to merge 2 commits into
mesa:mainfrom
lahari936:doctest-ci
Closed

lahari936 wants to merge 2 commits into
mesa:mainfrom
lahari936:doctest-ci

Conversation

@lahari936

@lahari936 lahari936 commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Closes #3825.

Note

Rebased onto current main (949dc8c5) and marked ready for review. The analysis below still holds exactly: pytest --doctest-modules mesa --ignore=mesa/examples reports the same 8 failures on today's main as it did when this was first written. The only change needed was following the mesa/meta_agents/ → mesa/experimental/meta_agents/ move from the Meta-Agents Rewrite, including two doctest lines that still imported the old path.

Summary

Adds repo-wide handling of >>> docstring examples using option A from the issue (pytest --doctest-modules), as a separate CI job so a rotten docstring is flagged on its own rather than mixed into unit-test or docs-build failures — the shape @falloficaruss asked for on the issue.

  • pyproject.toml: [tool.pytest.ini_options] with doctest_optionflags = ["ELLIPSIS", "NORMALIZE_WHITESPACE"]. Deliberately no addopts — pytest tests/ behaves exactly as before (912 items collected before and after).
  • .github/workflows/build_lint.yml: new doctest job running pytest --doctest-modules mesa --ignore=mesa/examples -Werror -Wdefault::PendingDeprecationWarning (same -W flags as the build job; ~20 s).
  • Docstring fixes so the job is green:
File Change Why
mesa/visualization/components/portrayal_components.py >>> → ... on continuation lines (3 examples) Was an IndentationError under doctest; the examples are otherwise correct and now execute.
mesa/experimental/meta_agents/meta_agents_api.py create Added the 4-line setup already used in docs/apis/meta_agents.md; [...] placeholders → real agents; _ = add_member(...) The example was a fragment (model, alice… undefined). add_member returns a MembershipView, which would be unexpected output. [...] is valid Python — a list containing Ellipsis — so it silently created an empty group.
mesa/experimental/meta_agents/meta_agents_api.py at_level Replaced the at_level(4, root=world) one-liner with a small two-level hierarchy and asserted outputs Only executable illustration of level semantics, and cheap to make real.
mesa/visualization/solara_viz.py # doctest: +SKIP on SolaraViz / ModelCreator; ModelCreator dict continuation >>> → ... Outputs are Solara components needing a render context, and MyModel is intentionally abstract. Skipped examples are still parsed, so the syntax fix matters.
mesa/visualization/command_console.py # doctest: +SKIP Second argument is a Solara set_input_text UI callback; my_model is intentionally abstract.

Result: pytest --doctest-modules mesa --ignore=mesa/examples goes from 8 failed / 2 passed to 8 passed / 2 skipped.

Why not B / C

  • B (custom tests/test_doctests.py walker) re-implements pytest's collector (pkgutil + importlib + doctest.testmod) and reports one opaque failure instead of per-docstring items. Nothing in the current examples needs per-module globs injection.
  • C (Sphinx -b doctest) — ran it locally. It only sees docstrings pulled in via automodule in docs/apis/*.md, so it misses mesa_signals, the one module with passing doctests today. It groups every example on a doc page into one namespace, so a single failure cascades (apis/visualization alone is 48 examples). And via :inherited-members: on Priority(IntEnum) it ends up testing CPython's int.bit_length docstring. It also needs the [docs] extras, notebook execution off, and PYTHONUTF8=1 on Windows. No coverage gain over A.

Why --ignore=mesa/examples

mesa/examples has zero doctests; importing it takes ~27 s because every app.py instantiates a model and renderer at import time; and two st_app.py files hard-fail on ModuleNotFoundError: streamlit, which isn't a dependency anywhere. Examples are already covered by tests/examples/. If a doctest is added to an example later, dropping the --ignore is a one-line change.

Testing

Against current main:

pytest --doctest-modules mesa --ignore=mesa/examples   8 passed, 2 skipped
pytest tests/                                          911 passed, 912 collected

The one full-suite failure is test_suppressed_wake_is_logged_at_debug, the pre-existing logging-propagation bug in #3935 (fixed by #3938 once it lands), unrelated to this PR. pre-commit is clean.

Open points

  • The direction question from the issue is still open: @quaquel, you said you hadn't decided whether doctests belong with the docs build or the test suite. This PR is a concrete version of A as a standalone job — happy to reshape it if you'd rather they ran somewhere else, and that is the one thing blocking it rather than anything in the diff.
  • tests/experimental/test_mesa_signals.py::test_signal_type_docstrings is left in place. It is now redundant with the CI job, but removing it felt out of scope here.
  • The new [tool.pytest.ini_options] section makes pyproject.toml pytest's inifile, pinning rootdir to the repo root. Every current invocation already runs from there, so there is no observable change.
  • Happy to add the doctest command to CONTRIBUTING.md if wanted.
  • @Dilraj07 proposed a similar plan on the issue — glad to hand this over or fold in changes if you have a version you prefer.

@github-actions github-actions Bot added ci Release notes label packaging Release notes label visualization labels Sep 17, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +1.4% [+0.3%, +2.7%] 🔵 +1.6% [+0.9%, +2.3%]
BoltzmannWealth large 🔵 -4.1% [-5.9%, -2.4%] 🔵 -0.8% [-3.4%, +2.0%]
Schelling small 🔵 +2.6% [+0.8%, +4.4%] 🔵 +1.5% [+0.1%, +2.8%]
Schelling large 🔵 +4.6% [+2.4%, +6.8%] 🔴 +12.3% [+7.0%, +18.1%]
WolfSheep small 🔵 -2.9% [-4.7%, -1.0%] 🔵 +0.7% [-3.7%, +5.1%]
WolfSheep large 🔵 +1.1% [-2.6%, +5.6%] 🔵 +6.1% [-0.6%, +13.6%]
SugarscapeG1mt small 🔵 -0.7% [-2.3%, +1.2%] 🔵 +0.3% [-1.7%, +2.2%]
SugarscapeG1mt large 🔵 -4.5% [-8.2%, -0.8%] 🔵 -2.6% [-3.9%, -1.4%]
BoidFlockers small 🔵 -1.1% [-2.4%, +0.3%] 🔵 +2.2% [+1.3%, +3.1%]
BoidFlockers large 🔵 +1.3% [-0.7%, +3.4%] 🔵 +1.6% [-0.2%, +3.4%]
MultiLevelAllianceModel small 🔵 -1.4% [-3.3%, +0.5%] 🔵 +2.7% [+1.1%, +4.3%]
MultiLevelAllianceModel large 🔵 +3.2% [+1.3%, +5.4%] 🔵 +1.4% [-0.3%, +3.2%]

Add repo-wide handling of docstring examples (mesa#3825):

- pyproject.toml: `doctest_optionflags = ["ELLIPSIS", "NORMALIZE_WHITESPACE"]`
  under `[tool.pytest.ini_options]`. No `addopts`, so `pytest tests/` is
  unchanged (834 tests collected before and after).
- CI: new `doctest` job running
  `pytest --doctest-modules mesa --ignore=mesa/examples` so a rotten
  docstring is flagged on its own. `mesa/examples` is excluded because it
  has no doctests, every `app.py` builds a model at import time, and
  `st_app.py` imports streamlit which is not a dependency.
- portrayal_components.py: continuation lines used `>>>` instead of
  `...`, which is an IndentationError under doctest.
- meta_agents_api.py: `create` and `at_level` examples are now
  self-contained (setup copied from docs/apis/meta_agents.md); `[...]`
  placeholders replaced with real agents; `add_member` return value
  assigned so it does not produce unexpected output.
- solara_viz.py / command_console.py: examples that build Solara
  components or need a UI callback are marked `# doctest: +SKIP`;
  ModelCreator's dict literal continuation fixed to `...`.

Result: `pytest --doctest-modules mesa --ignore=mesa/examples` goes from
8 failed / 2 passed to 8 passed / 2 skipped.

Co-Authored-By: Claude Opus 5 <[email protected]>
@github-actions github-actions Bot added the experimental Release notes label label Oct 22, 2026
@lahari936
lahari936 marked this pull request as ready for review October 22, 2026 17:01
Copilot AI balanced review requested due to automatic review settings October 22, 2026 17:01

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 reached their quota limit.

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -8.2% [-9.1%, -7.3%] 🔵 -1.0% [-1.1%, -0.8%]
BoltzmannWealth large 🔵 -0.2% [-1.3%, +0.8%] 🔵 -2.9% [-6.3%, +0.4%]
Schelling small 🟢 -4.2% [-4.9%, -3.4%] 🔵 -1.2% [-2.1%, -0.4%]
Schelling large 🔵 -0.6% [-1.5%, +0.2%] 🔵 -1.2% [-3.6%, +1.1%]
WolfSheep small 🔵 -3.1% [-3.8%, -2.3%] 🔵 -0.8% [-4.6%, +3.5%]
WolfSheep large 🔵 -1.0% [-1.6%, -0.3%] 🔵 -1.2% [-2.6%, +0.0%]
SugarscapeG1mt small 🔵 +0.1% [-1.2%, +1.4%] 🔵 +1.5% [-0.4%, +3.6%]
SugarscapeG1mt large 🔵 -0.8% [-2.5%, +1.3%] 🔵 +1.1% [-1.6%, +4.0%]
BoidFlockers small 🔵 -0.0% [-0.8%, +0.7%] 🔵 +1.6% [+1.1%, +2.0%]
BoidFlockers large 🔵 +1.9% [+1.2%, +2.6%] 🔵 +1.5% [+1.2%, +1.8%]
MultiLevelAllianceModel small 🔵 -1.5% [-2.7%, -0.3%] 🔵 -1.9% [-2.4%, -1.5%]
MultiLevelAllianceModel large 🔵 +1.1% [+0.5%, +1.6%] 🔵 -1.8% [-2.2%, -1.5%]

@quaquel

quaquel commented Oct 26, 2026

Copy link
Copy Markdown
Member

This pr also updates a bunch of docstrings. Ideally, a PR is atomic, so why are these docstring changes necessary?

The direction question from the issue is still open: @quaquel, you said you hadn't decided whether doctests belong with the docs build or the test suite. This PR is a concrete version of A as a standalone job — happy to reshape it if you'd rather they ran somewhere else, and that is the one thing blocking it rather than anything in the diff.

I think there is a third option: build the docs, run the normal test suite, run the docstring tests. Does this third option makes sense and how is it related to this PR?

@lahari936

Copy link
Copy Markdown
Contributor Author

Thanks for looking.

On the third option — yes, that makes sense, and I think it is what this PR already does, just described from the other end. Your three activities map onto three jobs:

  1. build the docs — the existing docs build
  2. run the normal test suite — the existing build matrix
  3. run the docstring tests — the doctest job this PR adds

So this PR is the third of your three, kept out of the other two deliberately. That was @falloficaruss's suggestion on #3825: a broken docstring should be reported on its own rather than mixed into a unit-test failure or a docs-build failure. Nothing about it requires doctests to be part of the docs build, which is the part I could not tell from your earlier reply. If you would rather fold them into the normal test run instead, that is a one-line change (drop the job, add --doctest-modules to the existing invocation) and I am happy to do it.

On the docstring changes — fair, and you are right that the PR is doing two things. They are not drive-by edits though: without them the new job is red the moment it is added, because eight docstrings currently fail. They break down as:

  • Syntax, 4 examples — continuation lines written as >>> instead of ..., which doctest reads as an IndentationError. The examples were never executable.
  • Fragments, 2 examples in meta_agents_api — model, alice and friends undefined, and [...] used as a placeholder, which is valid Python (a list holding Ellipsis) and so silently created an empty group.
  • Skips, 2 examples in solara_viz and command_console — outputs are Solara components needing a render context, so they are marked # doctest: +SKIP.

If you would prefer it atomic, the natural split is:

  • PR 1: fix the eight docstrings, so pytest --doctest-modules mesa --ignore=mesa/examples passes on main. No CI change, reviewable as pure docstring work.
  • PR 2: add the job, a diff of two files and ~20 lines.

That ordering also means PR 2 cannot be merged while anything is broken. Say the word and I will split it — I would rather do that than have you review a mixed diff.

@quaquel

quaquel commented Oct 27, 2026

Copy link
Copy Markdown
Member

please split it in two, link to this PR but close it. I can then more quickly merge and review both PRs.

@lahari936

Copy link
Copy Markdown
Contributor Author

Split as requested, @quaquel. Closing this in favour of the two halves:

On your third option — build the docs, run the normal test suite, run the docstring tests — #4011 is the third of those three, kept out of the other two deliberately so a rotten docstring is reported as a rotten docstring. If you would rather it folded into the docs build or the build matrix, that is a small change to #4011 alone now, which is the main thing the split buys.

Thanks for the nudge to split; it was the right call. The docstring half is reviewable on its own in a way it was not here.

@lahari936 lahari936 closed this Oct 28, 2026
quaquel added a commit that referenced this pull request Oct 28, 2026
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 #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 #3825.

Co-authored-by: Claude Opus 5 <[email protected]>
Co-authored-by: Jan Kwakkel <[email protected]>
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

ci Release notes label experimental Release notes label packaging Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Repo-wide handling of docstring doctests

3 participants