mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-27 18:36:23 +00:00
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.
This commit is contained in:
parent
0f9d7283e7
commit
0b9af031fb
9 changed files with 328 additions and 212 deletions
|
|
@ -476,8 +476,9 @@ where
|
|||
orelse,
|
||||
range: _range,
|
||||
}) => {
|
||||
visitor.visit_expr(test);
|
||||
// `body if test else orelse`
|
||||
visitor.visit_expr(body);
|
||||
visitor.visit_expr(test);
|
||||
visitor.visit_expr(orelse);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue