Skip to content

Comments

[flake8-bugbear] Include certain guaranteed-mutable expressions: tuples, generators, and assignment expressions (B006)#20024

Merged
dylwil3 merged 7 commits intoastral-sh:mainfrom
IDrokin117:feat/ruff-20004
Oct 3, 2025
Merged

[flake8-bugbear] Include certain guaranteed-mutable expressions: tuples, generators, and assignment expressions (B006)#20024
dylwil3 merged 7 commits intoastral-sh:mainfrom
IDrokin117:feat/ruff-20004

Conversation

@IDrokin117
Copy link
Contributor

@IDrokin117 IDrokin117 commented Aug 21, 2025

Summary

Resolves #20004

The implementation now supports guaranteed-mutable expressions in the following cases:

  • Tuple literals with mutable elements (supporting deep nesting)
  • Generator expressions
  • Named expressions (walrus operator) containing mutable components

Preserves original formatting for assignment value:

# Test case
def f5(x=([1, ])):
    print(x)
# Fix before
def f5(x=(None)):
    if x is None:
        x = [1]
    print(x)
# Fix after 
def f5(x=None):
    if x is None:
        x = ([1, ])
    print(x)

The expansion of detected expressions and the new fixes gated behind previews.

Test Plan

  • Added B006_9.py with a bunch of test cases
  • Generated snapshots

pub fn expr_parenthesized(mut self, expr: &Expr) -> String {
self.unparse_expr(expr, precedence::MAX);
self.generate()
}
Copy link
Contributor Author

@IDrokin117 IDrokin117 Aug 21, 2025

Choose a reason for hiding this comment

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

I've added this method to be able to generate an expression with parentheses. I didn't find a way to ensure it is parenthesized. Using expr to generate source code for Tuple, I got

tuple_expr = "([1], 2)"
.expr(tuple_expr) # -> "[1], 2", but expected ([1], 2)

I am not sure about this, would be appreciated for more sophisticated solution.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 21, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@dylwil3 dylwil3 self-requested a review August 29, 2025 19:25
@dylwil3 dylwil3 added the rule Implementing or modifying a lint rule label Sep 1, 2025
Copy link
Collaborator

@dylwil3 dylwil3 left a comment

Choose a reason for hiding this comment

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

This is great thank you, and sorry for the delay! Can we experiment a bit with the fix?

Also, we should be gating this behind preview - both the expansion of detected expressions and the new fix. If you'd like, you can make these two separate functions in preview.rs since we may stabilize them independently.

Copy link
Contributor Author

@IDrokin117 IDrokin117 left a comment

Choose a reason for hiding this comment

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

@dylwil3 Thanks for your review! I've pushed some changes according to your points:

  1. Added 2 preview flags.
  2. Added annotation expr into fix content if any. It is gated behind preview. There already exists a test, so it might solve the annotation issue.

Igor Drokin added 4 commits September 29, 2025 20:51
…ld be simple to support

- tuples containing mutable expressions
- generator expressions
- assignment expressions containing mutable expressions
…ld be simple to support

- tuples containing mutable expressions
- generator expressions
- assignment expressions containing mutable expressions
@IDrokin117
Copy link
Contributor Author

@dylwil3 Could you please review the PR again?

@dylwil3
Copy link
Collaborator

dylwil3 commented Oct 1, 2025

@dylwil3 Could you please review the PR again?

Yes! Sorry for the delay - I will not be able to get to this till the end of the week, but I have not forgotten!

Copy link
Collaborator

@dylwil3 dylwil3 left a comment

Choose a reason for hiding this comment

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

Thank you for your patience and for the great contribution!

@dylwil3 dylwil3 added fixes Related to suggested fixes for violations preview Related to preview mode features labels Oct 3, 2025
@dylwil3 dylwil3 merged commit 673167a into astral-sh:main Oct 3, 2025
38 checks passed
dcreager added a commit that referenced this pull request Oct 3, 2025
* origin/main:
  [`flake8-bugbear`] Include certain guaranteed-mutable expressions: tuples, generators, and assignment expressions (`B006`) (#20024)
  [`flake8-comprehensions`] Clarify fix safety documentation (`C413`) (#20640)
  [ty] improve base conda distinction from child conda (#20675)
  [`ruff`] Extend FA102 with listed PEP 585-compatible APIs (#20659)
  [`ruff`] Handle argfile expansion errors gracefully (#20691)
  [`flynt`] Fix f-string quoting for mixed quote joiners (`FLY002`) (#20662)
  [ty] Fix file root matching for `/`
  [ruff,ty] Enable tracing's `log` feature
  [`flake8-annotations`] Fix return type annotations to handle shadowed builtin symbols (`ANN201`, `ANN202`, `ANN204`, `ANN205`, `ANN206`) (#20612)
  Bump 0.13.3 (#20685)
  Update benchmarking CI for cargo-codspeed v4 (#20686)
  [ty] Support single-starred argument for overload call (#20223)
  [ty] `~T` should never be assignable to `T` (#20606)
  [`pylint`] Clarify fix safety to include left-hand hashability (`PLR6201`) (#20518)
  [ty] No union with `Unknown` for module-global symbols (#20664)
  [`ty`] Reject renaming files to start with slash in Playground (#20666)
  [ty] Enums: allow multiple aliases to point to the same member (#20669)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fixes Related to suggested fixes for violations 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.

B006 doesn’t detect mutable defaults in tuples, generators, and assignment expressions

2 participants