Skip to content

Commit

Permalink
fix removal of brackets in target
Browse files Browse the repository at this point in the history
  • Loading branch information
davidszotten committed Jun 21, 2023
1 parent 0fb59c9 commit c957589
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ for (((((k, v))))) in d.items():
# Only remove tuple brackets after `for`
-for k, v in d.items():
- print(k, v)
+for (k, v) in NOT_IMPLEMENTED_call():
+for k, v in NOT_IMPLEMENTED_call():
+ NOT_IMPLEMENTED_call()
# Don't touch tuple brackets after `in`
Expand All @@ -62,21 +62,21 @@ for (((((k, v))))) in d.items():
- dfkasdjfldsjflkdsjflkdsjfdslkfjldsjfgkjdshgkljjdsfldgkhsdofudsfudsofajdslkfjdslkfjldisfjdffjsdlkfjdlkjjkdflskadjldkfjsalkfjdasj.items()
-):
- print(k, v)
+for (k, v) in NOT_IMPLEMENTED_call():
+for k, v in NOT_IMPLEMENTED_call():
+ NOT_IMPLEMENTED_call()
# Test deeply nested brackets
-for k, v in d.items():
- print(k, v)
+for (k, v) in NOT_IMPLEMENTED_call():
+for k, v in NOT_IMPLEMENTED_call():
+ NOT_IMPLEMENTED_call()
```

## Ruff Output

```py
# Only remove tuple brackets after `for`
for (k, v) in NOT_IMPLEMENTED_call():
for k, v in NOT_IMPLEMENTED_call():
NOT_IMPLEMENTED_call()
# Don't touch tuple brackets after `in`
Expand All @@ -91,11 +91,11 @@ for (
) in NOT_IMPLEMENTED_call():
NOT_IMPLEMENTED_call()
for (k, v) in NOT_IMPLEMENTED_call():
for k, v in NOT_IMPLEMENTED_call():
NOT_IMPLEMENTED_call()
# Test deeply nested brackets
for (k, v) in NOT_IMPLEMENTED_call():
for k, v in NOT_IMPLEMENTED_call():
NOT_IMPLEMENTED_call()
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ for (
# remove brackets around x,y but keep them around z,w
for (x, y) in (z, w):
for x, y in (z, w):
...
Expand Down
22 changes: 18 additions & 4 deletions crates/ruff_python_formatter/src/statement/stmt_for.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
use crate::comments::{leading_alternate_branch_comments, trailing_comments};
use crate::expression::expr_tuple::TupleParentheses;
use crate::expression::parentheses::Parenthesize;
use crate::prelude::*;
use crate::{FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::node::AstNode;
use rustpython_parser::ast::{Ranged, Stmt, StmtFor};
use rustpython_parser::ast::{Expr, Ranged, Stmt, StmtFor};

#[derive(Debug)]
struct ExprTupleWithoutParentheses<'a>(&'a Expr);

impl Format<PyFormatContext<'_>> for ExprTupleWithoutParentheses<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
match self.0 {
Expr::Tuple(expr_tuple) => expr_tuple
.format()
.with_options(TupleParentheses::StripInsideForLoop)
.fmt(f),
other => other.format().with_options(Parenthesize::IfBreaks).fmt(f),
}
}
}

#[derive(Default)]
pub struct FormatStmtFor;
Expand Down Expand Up @@ -34,9 +50,7 @@ impl FormatNodeRule<StmtFor> for FormatStmtFor {
[
text("for"),
space(),
// TODO: the `IfBreaks` is currently ignored by
// https://github.com/astral-sh/ruff/blob/4b9b6829dccabdd4faf6efa6a118b4868347a701/crates/ruff_python_formatter/src/expression/expr_tuple.rs#L78
target.format().with_options(Parenthesize::IfBreaks),
ExprTupleWithoutParentheses(target.as_ref()),
space(),
text("in"),
space(),
Expand Down

0 comments on commit c957589

Please sign in to comment.