Importing the Boltzmann example app disables MESA log propagation, breaking caplog in later tests
Describe the bug
tests/experimental/test_actions.py::TestOnIdle::test_suppressed_wake_is_logged_at_debug fails when the full suite runs, and passes when run on its own. The test itself is fine — it is collateral damage from an import that reconfigures logging process-wide.
mesa_logging.log_to_stderr() defaults to propagate=False and sets it on the MESA root logger:
https://github.com/mesa/mesa/blob/main/mesa/mesa_logging.py#L191
mesa/examples/basic/boltzmann_wealth_model/app.py calls it at module import time, not inside a function:
https://github.com/mesa/mesa/blob/main/mesa/examples/basic/boltzmann_wealth_model/app.py#L15
tests/examples/test_examples.py::test_boltzmann_model imports that app module. From that point on, every MESA.* logger in the process has a non-propagating ancestor, so pytest's caplog (which captures via propagation to the root handler) records nothing, and any later test asserting on log output fails.
Demonstrated directly:
>>> from mesa.mesa_logging import get_rootlogger
>>> get_rootlogger().propagate
True
>>> import mesa.examples.basic.boltzmann_wealth_model.app
>>> get_rootlogger().propagate
False
Expected behavior
Importing an example app should not change logging configuration for the rest of the process. Test outcomes should not depend on collection order.
To Reproduce
Two tests are enough:
pytest tests/examples/test_examples.py::test_boltzmann_model \
"tests/experimental/test_actions.py::TestOnIdle::test_suppressed_wake_is_logged_at_debug" -q
# 1 failed, 1 passed
Reverse the order, or run the second test alone, and it passes:
pytest "tests/experimental/test_actions.py::TestOnIdle::test_suppressed_wake_is_logged_at_debug" -q
# 1 passed
Failure output:
assert "suppressed repeat on_idle wake" in caplog.text
E AssertionError: assert 'suppressed repeat on_idle wake' in ''
E + where '' = <_pytest.logging.LogCaptureFixture object at ...>.text
tests\experimental\test_actions.py:1478: AssertionError
On a clean checkout of main, pytest --cov=mesa tests/ gives 859 passed, 1 failed with this as the only failure.
I narrowed it by running the failing test after each top-level test target in turn. Only tests/examples reproduced it, then only tests/examples/test_examples.py, then only test_boltzmann_model.
Additional context
Possible fixes, roughly in order of how much they change:
- Move the
log_to_stderr(INFO)call in the Boltzmann app out of import scope, so importing the module has no side effect. The other example apps do not call it at all, so this one looks accidental. - Have the test suite restore
propagate— e.g. an autouse fixture that snapshots and restores the MESA root logger's handlers andpropagate. This guards against any future import doing the same thing. - Reconsider
propagate=Falseas the default forlog_to_stderr, though that is a public API change and out of scope here.
(1) plus (2) seems right to me: fix the specific import, and make the suite resilient so the next one does not silently break an unrelated test.
Environment: Python 3.14.2, Windows 11, main at 5451086f.
Happy to open a PR for (1) and (2) if that sounds like the right direction.
could u please assign it to me.
I'm not having write access to mesa.So I can't do it. Opening the issue doesn’t give you permission to assign it.
@EdwardBetts @alee @djmitche @AshNicolus , Niharika21-bisht asked to work on this. Could you assign it to them? On Mon, 12 Oct 2026, 12:47 pm Niharika21-bisht, @.***> wrote:
Niharika21-bisht left a comment (mesa/mesa#3935) https://github.com/mesa/mesa/issues/3935#issuecomment-6119581241
could u please assign it to me.
— Reply to this email directly, view it on GitHub https://github.com/mesa/mesa/issues/3935?email_source=notifications&email_token=BL3P7ACG4X44AJM5E7EZ2BD5TSAQ7A5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTMMJRHE2TQMJSGQY2M4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KYZTPN52GK4S7MNWGSY3L#issuecomment-6119581241, or unsubscribe https://github.com/notifications/unsubscribe-auth/BL3P7AACPCJMVJQBHEPJPOD5TSAQ7AVCNFSNUABEKJSXA33TNF2G64TZHMZDIMRUGU3TGNZ3JFZXG5LFHM2TOOJXGQ3TOMJTHCQXMAQ . You are receiving this because you authored the thread.Message ID: @.***>
@Niharika21-bisht — I owe you a heads-up I should have given on Monday.
I'd already written and tested a fix for this before you asked to be assigned, which I didn't mention at the time. That's on me, and I don't want to quietly land a PR over you after pointing maintainers your way.
So, your choice:
- If you'd like to do it, it's yours. The fix is two lines: drop the
log_to_stderr(INFO)call and its import frommesa/examples/basic/boltzmann_wealth_model/app.py. Worth knowing thatif __name__ == "__main__"won't work as a guard here, becausesolara run app.pyimports the module, so the guard never fires. The thing I'd suggest adding beyond the two-line fix is a test that stops it coming back — I found an import-based one far too slow (importing the example apps pulls in altair, solara and matplotlib; my first version took nearly two hours), so a static AST scan overmesa/examples/for module-levellog_to_stderr/log_to_file/basicConfigcalls runs in about two seconds instead. Happy to talk it through or review your PR. - If you'd rather not, say so and I'll open mine.
Either way the full suite goes from 861 passed / 1 failed to 861 passed, so it's worth getting in.
Hi @lahari936, thanks for the heads-up and the clear explanation! I would love to take this on. Please give me a little time to implement the fix and add the AST scan test as you suggested. I'll open a PR as soon as it's ready. If I need any help along the way, I'll definitely reach out. Thanks!
No rush!
On Tue, 13 Oct 2026, 5:44 pm Niharika21-bisht, @.***> wrote:
Niharika21-bisht left a comment (mesa/mesa#3935) https://github.com/mesa/mesa/issues/3935#issuecomment-6145296954
Hi @lahari936 https://github.com/lahari936, thanks for the heads-up and the clear explanation! I would love to take this on. Please give me a little time to implement the fix and add the AST scan test as you suggested. I'll open a PR as soon as it's ready. If I need any help along the way, I'll definitely reach out. Thanks!
— Reply to this email directly, view it on GitHub https://github.com/mesa/mesa/issues/3935?email_source=notifications&email_token=BL3P7AGCMC47JMV6776CCKD5TYMD5A5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTMMJUGUZDSNRZGU2KM4TFMFZW63VHNVSW45DJN5XKKZLWMVXHJLDGN5XXIZLSL5RWY2LDNM#issuecomment-6145296954, or unsubscribe https://github.com/notifications/unsubscribe-auth/BL3P7AFBSTIIPNPN3QJUIND5TYMD5AVCNFSNUABEKJSXA33TNF2G64TZHMZDIMRUGU3TGNZ3JFZXG5LFHM2TOOJXGQ3TOMJTHCQXMAQ . You are receiving this because you were mentioned.Message ID: @.***>
I've pushed the fix and added the AST scan test as discussed. Just a quick heads-up on the CI checks:
build / examples: Failing due to what looks like an unrelated upstream error (ValueError: sequence of length 3 does not match the number of agents (20) in VirusAntibodyModel).
codecov/patch: Flagging a coverage drop, likely because of the removed lines.
pre-commit.ci: Failing, but it seems to be related to the Vale linter check.
Let me know if I need to adjust anything else!
I don't have write access, so I can't assign issues. Looks like this is already in hand with #3938 though.
The reason this logging statement as in the example was to do just that: show how logging can be used with your own models. So, the simple fix is not removing the logging statement, but rather figure out how to reset logging after the example.
@quaquel that is a fair correction, and the "remove the call" suggestion was mine — I proposed it to @Niharika21-bisht on #3938, so the wrong turn is on me rather than on her.
You are right that deleting it loses the thing the example was there to show. The problem is narrower than the call: log_to_stderr() defaults to propagate=False and applies it to the MESA root logger, so importing the module reconfigures logging for the whole process and nothing puts it back.
Reading your suggestion as "keep the demonstration, reset afterwards", there seem to be two places that could own the reset:
-
The test suite owns it. An autouse fixture in a
tests/conftest.pythat saves and restores theMESAlogger's handlers, level andpropagatearound every test.tests/test_mesa_logging.pyalready has exactly such a fixture, but it is file-scoped, which is why the leak fromtests/examples/reachestests/experimental/. This keepsmesa/examples/.../app.pyuntouched, demonstration intact. -
The example owns it. Keep the call but stop it being a global side effect — for instance
log_to_stderr(INFO, propagate=True), so the handler is added without silencing ancestors, or a context manager that scopes the configuration.
My instinct is 1, since it fixes the class of problem rather than this one caller, and an example that configures logging at import is still surprising to anyone importing it for another reason. But 2 is closer to what you described, and you know the intent behind the example better than I do.
Which would you prefer? Happy to write whichever, or to review @Niharika21-bisht's if she would rather carry it — it is her PR and I do not want to take it over, having already sent it down one wrong path.
Hi @lahari936, I would like to continue working on this and update my PR! I can implement Option 1 (the autouse fixture in tests/conftest.py) since it fixes the broader issue. However, if @quaquel prefers Option 2 (modifying the example code), I am happy to implement that instead. Let me know what you both decide is best!