[flake8-bugbear] Include certain guaranteed-mutable expressions: tuples, generators, and assignment expressions (B006)#20024
Conversation
| pub fn expr_parenthesized(mut self, expr: &Expr) -> String { | ||
| self.unparse_expr(expr, precedence::MAX); | ||
| self.generate() | ||
| } |
There was a problem hiding this comment.
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.
crates/ruff_linter/src/rules/flake8_bugbear/rules/mutable_argument_default.rs
Outdated
Show resolved
Hide resolved
|
dylwil3
left a comment
There was a problem hiding this comment.
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.
crates/ruff_linter/src/rules/flake8_bugbear/rules/mutable_argument_default.rs
Outdated
Show resolved
Hide resolved
2f34892 to
f465a06
Compare
IDrokin117
left a comment
There was a problem hiding this comment.
@dylwil3 Thanks for your review! I've pushed some changes according to your points:
- Added 2 preview flags.
- 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.
crates/ruff_linter/src/rules/flake8_bugbear/rules/mutable_argument_default.rs
Outdated
Show resolved
Hide resolved
…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
7a6c7c4 to
7c04082
Compare
|
@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
There was a problem hiding this comment.
Thank you for your patience and for the great contribution!
* 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)
Summary
Resolves #20004
The implementation now supports guaranteed-mutable expressions in the following cases:
Preserves original formatting for assignment value:
The expansion of detected expressions and the new fixes gated behind previews.
Test Plan