Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ruff] Detect mutable defaults in dataclass field calls (RUF008) #16883

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

gopoto
Copy link

@gopoto gopoto commented Mar 21, 2025

Summary

Extend RUF008 to catch mutable default values supplied via dataclasses.field and attrs.field calls. Previously, RUF008 only flagged direct assignments like mutable_default: list[int] = [], and ignored cases such as attrs.field(default=[]) within attrs-decorated classes. The rule now inspects calls to field helpers and emits a diagnostic if the default keyword is a mutable literal.

Test fixtures for both dataclasses and attrs classes were updated to include explicit field(default=[]) cases, the attrs fixture now imports attrs so that these calls are resolvable, and snapshots were updated accordingly.

Test Plan

Built the workspace (cargo check) and ran Ruff against the updated fixtures:

cargo run -p ruff -- check crates/ruff_linter/resources/test/fixtures/ruff/RUF008_attrs.py --select RUF008 --preview --no-cache

reports both direct mutable defaults and attrs.field(default=[]) defaults. A minimal reproduction of the original issue:

import attrs
@attrs.define
class B:
    mutable_default: list[int] = []
    mutable_default2: list[int] = attrs.field(default=[])

now yields two RUF008 diagnostics as expected.

Running the hooks (cargo fmt, cargo clippy, etc.) and pre-commit checks pass after formatting, and updated snapshot tests reflect the new diagnostics.


This PR was generated by an AI system in collaboration with maintainers: @carljm, @ntBre

Fixes #16495

Unverified

This user has not yet uploaded their public signing key.
Signed-off-by: Gene Parmesan Thomas <201852096+gopoto@users.noreply.github.com>
@ntBre ntBre added the rule Implementing or modifying a lint rule label Mar 21, 2025
checker.report_diagnostic(diagnostic);
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be gated behind preview.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call – I've wrapped the new field(default=[]) check behind checker.settings.preview.is_enabled() and added preview snapshots/tests.

Signed-off-by: gopoto <201852096+gopoto@users.noreply.github.com>
@gopoto
Copy link
Author

gopoto commented Mar 25, 2025

Added preview gating to the new field(default=[]) detection in RUF008 so the expanded behavior only triggers under --preview. Baseline RUF008 snapshots were reverted to the pre-change expectation with updated line numbers, and new preview__RUF008 snapshots were introduced to cover the field default cases under preview. The test harness was updated to exercise the rule in preview mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ruff-mutable-default (RUF008) not not catching mutable default passed to attrs.field.
3 participants