Skip to content

Lint: Enable RUF012 and annotate mutable class attributes with ClassVar#3033

Merged
quaquel merged 6 commits intomesa:mainfrom
falloficarus22:cleanup/enable-ruf012
Jan 6, 2026
Merged

Lint: Enable RUF012 and annotate mutable class attributes with ClassVar#3033
quaquel merged 6 commits intomesa:mainfrom
falloficarus22:cleanup/enable-ruf012

Conversation

@falloficarus22
Copy link
Copy Markdown
Contributor

Summary

Enables the RUF012 Ruff rule and adds required typing.ClassVar annotations 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. Enabling RUF012 enforces the best practice of using ClassVar, preventing potential bugs where shared state is accidentally shadowed or misused.

Implementation

  • Configuration: Removed RUF012 from the extend-ignore list in pyproject.toml.
  • Core Annotations:
  1. mesa/agent.py: Annotated _ids (the global agent ID tracker).
  2. mesa/discrete_space/cell_agent.py: Annotated DIRECTION_MAP.
  3. mesa/mesa_logging.py: Annotated FORMATS in the log formatter.
  • Example Improvements:
    mesa/examples/advanced/pd_grid/model.py: Annotated activation_regimes and payoff.
  • Test Consistency:
    tests/visualization/test_backends.py: Annotated dummy space/agent attributes used for backend validation.

Usage Examples

# Before
class PdGrid(mesa.Model):
    activation_regimes = ["Sequential", "Random", "Simultaneous"]

# After
class PdGrid(mesa.Model):
    activation_regimes: typing.ClassVar[list[str]] = ["Sequential", "Random", "Simultaneous"]

Additional Notes

  • Verified that all checks pass by running ruff check . --select RUF012.
  • This is a non-breaking change with no impact on simulation logic or performance.

falloficarus22 and others added 2 commits December 28, 2025 21:28
- 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.
@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -2.0% [-3.1%, -0.9%] 🔵 -1.6% [-2.0%, -1.3%]
BoltzmannWealth large 🔵 +0.1% [-2.0%, +3.4%] 🔵 -1.6% [-4.1%, +0.8%]
Schelling small 🔵 +2.0% [+0.8%, +3.2%] 🔵 +3.0% [+1.9%, +4.0%]
Schelling large 🔵 +1.2% [-0.0%, +2.9%] 🔵 +1.2% [-0.8%, +3.2%]
WolfSheep small 🟢 -3.9% [-4.8%, -3.1%] 🔵 -3.0% [-3.4%, -2.6%]
WolfSheep large 🔵 -3.6% [-7.6%, -1.5%] 🟢 -4.4% [-5.9%, -3.1%]
BoidFlockers small 🔵 -0.3% [-0.8%, +0.1%] 🔵 -0.2% [-0.5%, +0.0%]
BoidFlockers large 🔵 -0.6% [-0.8%, -0.5%] 🔵 -0.1% [-0.5%, +0.3%]

@quaquel quaquel added docs Release notes label ci Release notes label and removed docs Release notes label labels Dec 30, 2025
@quaquel quaquel merged commit 83d268a into mesa:main Jan 6, 2026
10 of 12 checks passed
@falloficarus22 falloficarus22 deleted the cleanup/enable-ruf012 branch January 7, 2026 07:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants