-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Format ExprIfExp (ternary operator) (#5597)
## Summary Format `ExprIfExp`, also known as the ternary operator or inline `if`. It can look like ```python a1 = 1 if True else 2 ``` but also ```python b1 = ( # We return "a" ... "a" # that's our True value # ... if this condition matches ... if True # that's our test # ... otherwise we return "b§ else "b" # that's our False value ) ``` This also fixes a visitor order bug. The jaccard index on django goes from 0.911 to 0.915. ## Test Plan I added fixtures without and with comments in strange places.
- Loading branch information
Showing
9 changed files
with
328 additions
and
212 deletions.
There are no files selected for viewing
This file contains 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
33 changes: 33 additions & 0 deletions
33
crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/if.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
a1 = 1 if True else 2 | ||
|
||
a2 = "this is a very long text that will make the group break to check that parentheses are added" if True else 2 | ||
|
||
# These comment should be kept in place | ||
b1 = ( | ||
# We return "a" ... | ||
"a" # that's our True value | ||
# ... if this condition matches ... | ||
if True # that's our test | ||
# ... otherwise we return "b" | ||
else "b" # that's our False value | ||
) | ||
|
||
# These only need to be stable, bonus is we also keep the order | ||
c1 = ( | ||
"a" # 1 | ||
if # 2 | ||
True # 3 | ||
else # 4 | ||
"b" # 5 | ||
) | ||
c2 = ( | ||
"a" # 1 | ||
# 2 | ||
if # 3 | ||
# 4 | ||
True # 5 | ||
# 6 | ||
else # 7 | ||
# 8 | ||
"b" # 9 | ||
) |
This file contains 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
35 changes: 28 additions & 7 deletions
35
crates/ruff_python_formatter/src/expression/expr_if_exp.rs
This file contains 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
This file contains 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
Oops, something went wrong.