Repository navigation
Fix module-level logging in boltzmann example and add AST scan test - #3938
Niharika21-bisht wants to merge 8 commits into
Conversation
|
Performance benchmarks:
|
|
Hi! I noticed the build / examples check failed, but the error (ValueError: sequence of length 3 does not match the number of agents (20) in VirusAntibodyModel) seems completely unrelated to my logging changes. Let me know if I need to do anything else! |
dietcoke-17
left a comment
There was a problem hiding this comment.
Fix the codecov fails diff is too low and pre commit error
|
Thanks for picking this up, and the AST scan looks good. On the three checks — one of them is upstream, two are yours and both are quick:
Collecting offenders and asserting once at the end fixes offenders = []
for path in sorted(examples_dir.rglob("*.py")):
tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
for node in tree.body: # module level only
if not isinstance(node, ast.Expr) or not isinstance(node.value, ast.Call):
continue
func = node.value.func
name = func.attr if isinstance(func, ast.Attribute) else getattr(func, "id", "")
if name in forbidden_calls:
offenders.append(f"{path.relative_to(examples_dir).as_posix()}:{node.lineno}: {name}()")
assert not offenders, (
"example modules must not configure logging at import time: " + ", ".join(offenders)
)
Two small things worth considering, take or leave:
Nice catch on reporting the CI state clearly rather than just pushing and hoping — made this easy to look at. |
|
Thanks for the detailed review and suggestions, @lahari936 and @dietcoke-17! I have pushed the updates to fix the ruff pre-commit errors and the codecov/patch coverage dip using the suggested offenders list approach. The dictConfig has also been added to the forbidden calls list. It looks like the only remaining failing check is the build / examples one, which as noted earlier is an upstream issue. Let me know if there is anything else needed from my end to get this merged! |
|
Nice, the offenders-list change did the job — Small correction on the remaining state though: there are still two failing checks, not one. The summary line of
The |
|
The reason this logging statement as in the example was to do just that: show how logging can be used with your own models. Removing the statement also fully removes any example on how to use logging. So, I am not in favor of this PR. I am closing this PR in favor of first agreeing on a direction in the associated issue. |
Note
Use this template for bug fixes only. For enhancements/new features, use the feature template and get maintainer approval in an issue/discussion before opening a PR.
Pre-PR Checklist
Summary
Fixed a bug where importing the Boltzmann example app disabled MESA log propagation process-wide. Removed the offending module-level logging call and added a static AST scan test to prevent future regressions.
Bug / Issue
Fixes #3935.
Importing 'mesa/examples/basic/boltzmann_wealth_model/app.py'called 'log_to_stderr(INFO)' at the module level. Because this defaults to 'propagate=False' on the MESA root logger, it disabled log propagation for the rest of the process, breaking pytest's 'caplog' fixture in subsequent tests.
Implementation
Testing
Additional Notes
As discussed in the issue, an import-based test was too slow, so the AST scan approach was used as it runs in just a few seconds and provides a robust guard against this recurring.