Skip to content

Commit

Permalink
Count conditional expressions as two operations
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jul 10, 2023
1 parent 7b5436e commit f4b6387
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
15 changes: 3 additions & 12 deletions crates/ruff_python_formatter/src/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,18 +303,9 @@ impl<'input> MaxOperatorPriorityVisitor<'input> {
range: _,
}) => self.update_max_priority(OperatorPriority::from(*op)),

Expr::IfExp(ast::ExprIfExp {
range: _,
test,
body: _,
orelse: _,
}) => {
self.update_max_priority(OperatorPriority::Conditional);

// Nested if else expressions are always parenthesized. Ignore parentheses in this case
if let Expr::IfExp(_) = test.as_ref() {
self.update_max_priority(OperatorPriority::Conditional);
}
Expr::IfExp(_) => {
// + 1 for the if and one for the else
self.update_max_priority_with_count(OperatorPriority::Conditional, 2);
}

Expr::Compare(ast::ExprCompare {
Expand Down
12 changes: 5 additions & 7 deletions crates/ruff_python_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,11 @@ if True:
fn quick_test() {
let src = r#"
if (
field.is_relation
and not field.many_to_many
and hasattr(field, "attname")
and field.attname == name
):
pass
def test3():
if True:
field = (
model._meta.pk if from_field is None else model._meta.get_field(from_field)
)
"#;
// Tokenize once
let mut tokens = Vec::new();
Expand Down

0 comments on commit f4b6387

Please sign in to comment.