FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[`flake8-bugbear`] Include certain guaranteed-mutable expressions: tuples, generators, and assignment expressions (`B006`) by IDrokin117 · Pull Request #20024 · astral-sh/ruff · GitHub

Repository navigation

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

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

dylwil3 merged 7 commits into
astral-sh:mainfrom
IDrokin117:feat/ruff-20004

Conversation

IDrokin117 commented Aug 21, 2025 •
edited by dylwil3
Loading

Copy link
Copy Markdown
Contributor

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()
}

IDrokin117 Aug 21, 2025 •
edited
Loading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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 Bot commented Aug 21, 2025 •
edited
Loading

Copy link
Copy Markdown
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

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

dylwil3 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

IDrokin117 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@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

Copy link
Copy Markdown
Contributor Author

@dylwil3 Could you please review the PR again?

dylwil3 commented Oct 1, 2025

Copy link
Copy Markdown
Collaborator

@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!

dylwil3 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thank you for your patience and for the great contribution!

dylwil3 added fixes Related to suggested fixes for violations preview Related to preview-mode features labels Oct 3, 2025
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)
ntBre added a commit that referenced this pull request Jul 20, 2026
…(`B006`)

Preview feature: #20024

Rule documentation: https://docs.astral.sh/ruff/rules/mutable-argument-default/

This is currently stacked on #26950. They were technically separate preview features, but to fix
some of the expanded mutable expressions requires the source-preserving fix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL