Skip to content

Update Ruff to v0.13 and Fix RUF059 Violations #2865

@EwoutH

Description

@EwoutH

Description

We should update Ruff from v0.12.11 to v0.13.3 (latest version). The update introduces some new stabilized rules that will require fixes to our codebase.

Current Status

A PR updating .pre-commit-config.yaml to use Ruff v0.13.3 (#2840) currently fails due to violations of the newly stabilized RUF059 (unused-unpacked-variable) rule.

Required Changes

1. Update Ruff Version

The version update in .pre-commit-config.yaml is already prepared (#2840).

2. Fix RUF059 Violations

The RUF059 rule (previously in preview, now stable in v0.13) flags unpacked variables that are never used. We have 18 violations across our codebase where fig is unpacked from plt.subplots() but never used.

Affected files:

  • mesa/visualization/mpl_space_drawing.py (6 occurrences)
  • mesa/visualization/space_drawers.py (5 occurrences)
  • tests/test_components_matplotlib.py (7 occurrences)

Example violation:

if ax is None:
    fig, ax = plt.subplots()  # fig is never used

Recommended fix:
Replace fig with _ to indicate it's intentionally unused:

if ax is None:
    _, ax = plt.subplots()

3. Fix RUF043 Violations (Optional)

There are also 3 RUF043 violations (pytest-raises-ambiguous-pattern) where regex metacharacters in pytest match= patterns should be escaped or use raw strings. These should be addressed as well:

  • tests/test_discrete_space.py:912
  • tests/test_portrayal_components.py:131
  • tests/test_solara_viz.py:319

Migration Notes

According to the Ruff v0.13 changelog, most users should be able to upgrade without issues. The main breaking changes don't affect our configuration:

  • ✅ We don't rely on deprecated rule group selections
  • ✅ We don't use the deprecated macOS config directory
  • ✅ Removed rules (PD901, UP038) were already deprecated

Action Items

  • Fix all RUF059 violations by replacing unused fig variables with _
  • Fix RUF043 violations by escaping regex patterns or using raw strings
  • Update Ruff version to v0.13.3
  • Verify all pre-commit hooks pass

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions