Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Merge is_guaranteed_mutable_expr into is_mutable_expr#23072

Open
eureka928 wants to merge 4 commits intoastral-sh:mainfrom
eureka928:merge-is-mutable-expr-patterns-impl
Open

Merge is_guaranteed_mutable_expr into is_mutable_expr#23072
eureka928 wants to merge 4 commits intoastral-sh:mainfrom
eureka928:merge-is-mutable-expr-patterns-impl

Conversation

@eureka928
Copy link

Summary

Resolves #20851.

is_mutable_expr was missing several expression types that is_guaranteed_mutable_expr (used only by B006) already handled: generators, walrus/named expressions, and tuples containing mutable elements. This caused false negatives in B039, RUF008, RUF012, and RUF024.

This PR merges those extra cases directly into is_mutable_expr:

  • Expr::Generator — generators are now recognized as mutable
  • Expr::Named — walrus expressions (e.g. x := []) check the assigned value recursively
  • Expr::Tuple — tuples are mutable if any element is mutable

With these patterns unified in is_mutable_expr, the separate is_b006_check_guaranteed_mutable_expr_enabled and is_b006_unsafe_fix_preserve_assignment_expr_enabled preview gates are no longer needed and are removed.

Test Plan

cargo nextest run -p ruff_linter and cargo clippy.

Added test fixtures for all four affected rules covering generators, walrus expressions, and tuples with mutable elements:

  • B039: ContextVar("cv", default=(x for x in "cv")), (x := []), ([],)
  • RUF008: same patterns in dataclass field defaults
  • RUF012: same patterns in class-level defaults
  • RUF024: same patterns in dict.fromkeys() values

@astral-sh-bot
Copy link

astral-sh-bot bot commented Feb 4, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+5 -1 violations, +0 -0 fixes in 2 projects; 53 projects unchanged)

apache/airflow (+3 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview --select ALL

+ airflow-core/src/airflow/models/asset.py:226:22: RUF012 Mutable class attributes should be annotated with `typing.ClassVar`
+ airflow-core/src/airflow/models/asset.py:333:22: RUF012 Mutable class attributes should be annotated with `typing.ClassVar`
+ airflow-core/src/airflow/models/asset.py:808:22: RUF012 Mutable class attributes should be annotated with `typing.ClassVar`

apache/superset (+2 -1 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview --select ALL

+ tests/integration_tests/core_tests.py:374:9: D102 Missing docstring in public method
- tests/integration_tests/core_tests.py:374:9: D102 Missing docstring in public method
+ tests/integration_tests/core_tests.py:861:22: RUF012 Mutable class attributes should be annotated with `typing.ClassVar`

Changes by rule (2 rules affected)

code total + violation - violation + fix - fix
RUF012 4 4 0 0 0
D102 2 1 1 0 0

@eureka928
Copy link
Author

Hi @ntBre thanks for last PR
would you review this PR?

@ntBre
Copy link
Contributor

ntBre commented Feb 4, 2026

Hi, thanks for the PRs! As Micha asked you a couple of weeks ago (#22600 (comment)), please give us some time before pinging us. I'm subscribed to all notifications on the repository, so I'll see your new PRs as they come in.

@ntBre ntBre self-requested a review February 4, 2026 17:43
@ntBre ntBre added rule Implementing or modifying a lint rule preview Related to preview mode features labels Feb 4, 2026
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

We cannot stabilize preview features outside of minor releases. Instead of removing preview features, we needed to add a new preview feature and use it in the rules mentioned in #20851 because this expands the scope of all of these rules. The changes to B006 need to be reverted, and we need to use a similar pattern to lines 117-121 in the other rules.

eureka928 and others added 3 commits February 7, 2026 00:18
Fixes false negatives in B039, RUF008, RUF012, and RUF024 by extending
`is_mutable_expr` to handle generator expressions, tuples containing
mutable elements, and walrus (`:=`) expressions. These patterns were
previously only detected in B006 behind a preview gate.

Also stabilizes the B006 fix generation to always preserve original
source text, which correctly handles walrus operator parentheses.

Closes astral-sh#20851
The rebase added a new class C before the field() test class (now class D),
shifting line numbers in both the stable and preview snapshots.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
The field(default=...) detection from astral-sh#23046 is behind a preview gate,
so class D diagnostics should only appear in the preview snapshot.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@eureka928 eureka928 force-pushed the merge-is-mutable-expr-patterns-impl branch from 221c5f7 to 7a80bf9 Compare February 6, 2026 23:18
…table_expr`

Remove B006-specific preview flags (`is_b006_check_guaranteed_mutable_expr_enabled`,
`is_b006_unsafe_fix_preserve_assignment_expr_enabled`) in favor of the shared
`is_extended_mutable_expr_enabled` flag used by all other mutable-expression rules.
Delete the duplicate local `is_guaranteed_mutable_expr` from B006 since it is
identical to `is_mutable_expr_extended` in typing.rs.
@eureka928 eureka928 requested a review from ntBre February 7, 2026 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview Related to preview mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

is_mutable_expr causes false negatives in B039, RUF008, RUF012, and RUF024

2 participants