Repo-wide handling of docstring doctests
Background
Follow-up to #3819. That PR fixed signal docstring examples and added isolated coverage (doctest.testmod(signal_types)).
cc @quaquel
Problem
Only signal_types.py is doctest-covered. No repo-wide mechanism exists:
pyproject.tomlhas no[tool.pytest.ini_options]/doctest_optionflags- CI (
.github/workflows/build_lint.yml:60) never runs--doctest-modulesorsphinx -b doctest grep -R ">>>" mesa --include="*.py"hits 5 files (see audit below) – 4/5 currently faildoctest.testmod.
Audit (verified via pytest --doctest-modules and doctest.testmod)
| Module | Result | Reason |
|---|---|---|
mesa/experimental/mesa_signals/signal_types.py |
PASS | self-contained, uses ... |
mesa/meta_agents/meta_agents_api.py:214 (create) + :388 (at_level) |
FAIL NameError: model |
fragmentary snippets, undefined alice/bob/world, [...] needs ELLIPSIS |
mesa/visualization/components/portrayal_components.py:30 |
FAIL IndentationError |
>>> return should be ... return |
mesa/visualization/solara_viz.py:112 |
FAIL NameError: MyModel/np |
incomplete setup |
mesa/visualization/command_console.py:143 |
FAIL NameError: my_model |
undefined callback |
What's the problem this feature will solve?
Prevent docstring examples from rotting (already happened in #3818/#3819). Ensure new examples stay runnable, and give contributors a clear pattern.
Proposed solutions (seeking direction)
A. pytest --doctest-modules (idiomatic):
Add to pyproject.toml:
[tool.pytest.ini_options]
doctest_optionflags = ["ELLIPSIS","NORMALIZE_WHITESPACE"]
# addopts = "--doctest-modules" # or explicit CI flag
norecursedirs = ["mesa/examples",".venv","build","docs/_build"]
Fix 4 files (+SKIP or correct ...). Update CI to --doctest-modules mesa --ignore=mesa/examples. Pro: standard, zero custom code. Con: addopts changes every pytest run; needs fixes before green.
B. Central collector test tests/test_doctests.py (mirrors #3819 style):
import doctest, pkgutil, importlib, mesa, pytest
# walk mesa.*, skip examples/st_app, assert doctest.testmod(...).failed==0
Keeps config minimal, allows per-module globs injection. Slightly less standard.
C. Sphinx make doctest CI job: Enable docs/conf.py:41 + doctest_test_doctest_blocks="default" and run sphinx-build -b doctest – validates docs/ but not mesa/**/*.py docstrings alone.
Open questions
- Preferred mechanism: A (global addopts) vs B (isolated walker) vs both?
- Fix vs +SKIP: make snippets runnable (add boilerplate like from mesa import Model) or just # doctest: +SKIP for illustrative fragments?
- Scope: mesa/ only, or also tests/ / docs/? Exclude mesa/examples?
Additional context
- #3818 fixed similar docstring inaccuracies
- docs/conf.py:41 already enables
sphinx.ext.doctestbut not in CI - pyproject.toml:dev already has pytest, sphinx; no extra dependencies needed for A/B
Preferred mechanism: A (global addopts) vs B (isolated walker) vs both?
I don't know yet. I haven't dealt with this before. Do we see doctests as part of docs, or is it part of our unit tests? I see it more as part of docs, but just doing it via sphinx also seems strange. Currently, failing docs is not blocking on a merge. Moreover, having docs fail might have causes other than a rotten docstring.
Is it possible to have a separate CI action somewhere only for doctests? So at least it gets flagged cleanly if the cause of the problem is a rotten docstring?
Fix vs +SKIP: make snippets runnable (add boilerplate like from mesa import Model) or just # doctest: +SKIP for illustrative fragments?
Not sure I fully understand this. I guess often snippets are more illustration of use, rather than stand alone runnable.
Scope: mesa/ only, or also tests/ / docs/? Exclude mesa/examples?
mesa + examples.
I'll add a separate CI job just for doctests so it's clear when a docstring is broken not mixed with the general docs build
Hi @falloficaruss @quaquel,
Just checking in to see if anyone is currently working on this. If not, I would be happy to help out and open a PR.
I tested pytest --doctest-modules mesa locally and looked into the current failures. Based on @quaquel's suggestions, here is a simple plan:
- Dedicated CI job: Add a separate, lightweight
doctestjob in.github/workflows/build_lint.ymlso docstring checks are isolated from regular unit tests. (We can use--ignore=mesa/examplesso it stays focused on core Mesa and doesn't require optional Streamlit dependencies). - Fix docstring syntax: In
mesa/visualization/components/portrayal_components.py, update multi-line docstring prompts from>>>to...to resolve theIndentationError. - Illustrative examples: For interactive UI components and high-level fragments (
solara_viz.py,command_console.py,meta_agents_api.py), add# doctest: +SKIPso the examples remain clear and readable without needing extra boilerplate. - Pytest flags: Add
doctest_optionflags = ["NORMALIZE_WHITESPACE", "ELLIPSIS"]under[tool.pytest.ini_options]inpyproject.toml.
I have tested these adjustments locally and all docstring tests pass cleanly.
Please let me know if this direction sounds good, and I'll be glad to open the PR.
Thanks!
Hi, I'd like to contribute to #3825. I investigated the current doctest coverage and reproduced the failures locally.
A few findings:
pytest --doctest-modules mesa --ignore=mesa/examplescurrently gives 8 failures and 2 passes across the 5 files containing Python doctests.mesa/examplescontains no doctests and importing it adds significant overhead; some example apps also require dependencies not used by the core package.- The existing pytest-based approach appears to fit Mesa better than a custom doctest walker.
- Sphinx doctest does not provide complete coverage of Python-source docstrings because only autodoc-exposed modules are included.
My current recommendation is:
- use
pytest --doctest-modules mesa --ignore=mesa/examples; - keep this as a separate CI job rather than adding it to pytest's
default
addopts; - configure the relevant doctest option flags in
pyproject.toml; - fix genuinely executable examples;
- mark UI/context-dependent illustrative examples appropriately rather than forcing artificial setup into them.
Before implementing, I'd like to confirm the intended scope and approach,
especially whether mesa/examples should be excluded and how the
illustrative visualization examples should be handled.
I also noticed another contributor has proposed working on a similar solution, so I'm happy to coordinate to avoid duplicate work.
Hi, I’ve completed an initial investigation of #3825 and would like to confirm the intended direction before implementing.
I reproduced the current failures with:
pytest --doctest-modules mesa --ignore=mesa/examples
and currently get 8 failures and 2 passes across the five files containing doctests.
My current recommendation is to use pytest’s native doctest collection in a separate CI job, rather than adding it to the default pytest addopts. I don’t think a custom doctest walker is necessary because it would duplicate pytest functionality, and Sphinx doctest does not provide complete coverage of Python source docstrings.
I have a few questions before proceeding:
-
Scope: Should the repo-wide doctest job cover
mesa/while excludingmesa/examples/? I found no doctests inmesa/examples, and importing those examples adds significant overhead and can require dependencies such as Streamlit. -
Broken examples: For the visualization-related snippets that depend on Solara/UI context (
solara_viz.pyandcommand_console.py), should these remain illustrative/skipped rather than being made artificially executable? -
at_level: Should I make theMetaAgents.at_levelexample fully executable with the necessary setup, or would you prefer it to remain a skipped/illustrative snippet? -
Existing signal doctest: Once repo-wide pytest doctest coverage exists, should the existing
test_signal_type_docstringstest remain, or should it eventually be removed as redundant? -
CI: Does a separate Ubuntu doctest job, independent of the main OS/Python matrix, sound appropriate?
-
I also noticed another contributor may be working on a similar solution. If so, I’m happy to coordinate before proceeding.
I’m ready to continue once I have your guidance on these points.
Picking this back up. #3864 is rebased onto current main and out of draft.
Worth noting that the problem has not drifted at all in the meantime — pytest --doctest-modules mesa --ignore=mesa/examples reports the same 8 failures on main today as it did when I first looked, in the same 8 docstrings. The only thing that needed changing was following the mesa/meta_agents/ → mesa/experimental/meta_agents/ move, including two doctest lines that still imported the old path and would have failed at runtime.
With the PR applied, that command goes to 8 passed / 2 skipped, and pytest tests/ is unchanged at 912 collected, since the config deliberately sets no addopts.
@quaquel, the open question is still yours and it is the only thing blocking this rather than anything in the diff. From your earlier reply:
I don't know yet. I haven't dealt with this before. Do we see doctests as part of docs or as part of testing?
#3864 is a concrete answer to that: option A (pytest --doctest-modules) as a standalone CI job, so a broken docstring is reported on its own rather than mixed into unit-test or docs-build output — the shape @falloficaruss suggested here. Roughly 20 s per run.
I also tried C (Sphinx -b doctest) properly rather than dismissing it. It only sees docstrings pulled in through automodule in docs/apis/*.md, so it misses mesa_signals, which is the one module whose doctests already pass; it shares one namespace per doc page, so a single failure cascades across all 48 examples on apis/visualization; and through :inherited-members: on Priority(IntEnum) it ends up testing CPython's int.bit_length docstring. So "doctests as part of docs" is possible, but it tests less and fails worse.
Happy to reshape it if you would rather they ran inside the existing test job, or alongside the docs build. There is working code to look at either way now.
@Dilraj07 — you offered to help here back in September and I would rather not have stepped on that. If you have a version you prefer, I am glad to hand this over or fold your changes in.
Hi @lahari936, thanks for following up and for the ping. No worries at all about stepping on anything. I really appreciate you checking in. The changes in #3864 looks fine and cexecutes what we discussed.
Please go ahead with #3864. l will head over to the PR to leave a review and help test it.
Okay
On Fri, 23 Oct 2026, 10:14 pm Dilraj Singh, @.***> wrote:
Dilraj07 left a comment (mesa/mesa#3825) https://github.com/mesa/mesa/issues/3825#issuecomment-6369972335
Hi @lahari936 https://github.com/lahari936, thanks for following up and for the ping. No worries at all about stepping on anything. I really appreciate you checking in. The changes in #3864 https://github.com/mesa/mesa/pull/3864 looks fine and cexecutes what we discussed.
Please go ahead with #3864 https://github.com/mesa/mesa/pull/3864. l will head over to the PR to leave a review and help test it.
— Reply to this email directly, view it on GitHub https://github.com/mesa/mesa/issues/3825?email_source=notifications&email_token=BL3P7AERLBWW5ILQXMPTCOD5VODIVA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTMMZWHE4TOMRTGM22M4TFMFZW63VHNVSW45DJN5XKKZLWMVXHJLDGN5XXIZLSL5RWY2LDNM#issuecomment-6369972335, or unsubscribe https://github.com/notifications/unsubscribe-auth/BL3P7ADK2WMNOCFU76NQSH35VODIVAVCNFSNUABEKJSXA33TNF2G64TZHMZDIMRUGU3TGNZ3JFZXG5LFHM2TEOJWGQ2TAMJQG6QXMAQ . You are receiving this because you were mentioned.Message ID: @.***>