Skip to content

Commit

Permalink
Remove Comprehension priority (astral-sh#6947)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Aug 29, 2023
1 parent adb4869 commit 715d86d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,11 @@
isinstance(aaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb) and (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy) or isinstance(ccccccccccc, dddddd)
):
pass


def test():
return (
isinstance(other, Mapping)
and {k.lower(): v for k, v in self.items()}
== {k.lower(): v for k, v in other.items()}
)
14 changes: 7 additions & 7 deletions crates/ruff_python_formatter/src/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,16 +434,17 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> {
// Visits a subexpression, ignoring whether it is parenthesized or not
fn visit_subexpression(&mut self, expr: &'input Expr) {
match expr {
Expr::Dict(_) | Expr::List(_) | Expr::Tuple(_) | Expr::Set(_) => {
Expr::Dict(_)
| Expr::List(_)
| Expr::Tuple(_)
| Expr::Set(_)
| Expr::ListComp(_)
| Expr::SetComp(_)
| Expr::DictComp(_) => {
self.any_parenthesized_expressions = true;
// The values are always parenthesized, don't visit.
return;
}
Expr::ListComp(_) | Expr::SetComp(_) | Expr::DictComp(_) => {
self.any_parenthesized_expressions = true;
self.update_max_priority(OperatorPriority::Comprehension);
return;
}
// It's impossible for a file smaller or equal to 4GB to contain more than 2^32 comparisons
// because each comparison requires a left operand, and `n` `operands` and right sides.
#[allow(clippy::cast_possible_truncation)]
Expand Down Expand Up @@ -789,7 +790,6 @@ enum OperatorPriority {
String,
BooleanOperation,
Conditional,
Comprehension,
}

impl From<ast::Operator> for OperatorPriority {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ if not (
isinstance(aaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb) and (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy) or isinstance(ccccccccccc, dddddd)
):
pass
def test():
return (
isinstance(other, Mapping)
and {k.lower(): v for k, v in self.items()}
== {k.lower(): v for k, v in other.items()}
)
```

## Output
Expand Down Expand Up @@ -220,6 +228,12 @@ if not (
or isinstance(ccccccccccc, dddddd)
):
pass
def test():
return isinstance(other, Mapping) and {k.lower(): v for k, v in self.items()} == {
k.lower(): v for k, v in other.items()
}
```


Expand Down

0 comments on commit 715d86d

Please sign in to comment.