Lint: Enable RUF012 and annotate mutable class attributes with ClassVar#3033
Merged
Lint: Enable RUF012 and annotate mutable class attributes with ClassVar#3033
Conversation
- Enable RUF012 in ruff configuration. - Annotate mutable class attributes with ClassVar in core code and tests. - Affected files: mesa/agent.py, mesa/discrete_space/cell_agent.py, mesa/mesa_logging.py, pd_grid model, and visualization tests.
|
Performance benchmarks:
|
quaquel
approved these changes
Jan 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Enables the
RUF012Ruff rule and adds requiredtyping.ClassVarannotations to mutable class-level attributes across core modules, examples, and tests.Motive
The codebase contained several mutable class attributes (dictionaries, lists, and defaultdict instances) that were not annotated. Without
ClassVar, it is not explicitly clear to static analysis tools and developers whether these variables are intended to be shared across all instances or are actually intended to be instance-level defaults. EnablingRUF012enforces the best practice of usingClassVar, preventing potential bugs where shared state is accidentally shadowed or misused.Implementation
RUF012from the extend-ignore list inpyproject.toml.mesa/agent.py: Annotated_ids(the global agent ID tracker).mesa/discrete_space/cell_agent.py: AnnotatedDIRECTION_MAP.FORMATSin the log formatter.mesa/examples/advanced/pd_grid/model.py: Annotatedactivation_regimesandpayoff.tests/visualization/test_backends.py: Annotated dummy space/agent attributes used for backend validation.Usage Examples
Additional Notes
ruff check . --select RUF012.