-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature request] Indentation for ternary statements #9722
Comments
How would you expect the Python code to be formatted? Since Python ternaries are written out in a different order than JS ternaries, it's difficult to apply the Prettier format directly. |
That's a good point, I hadn't considered that difference. I think a single additional indent in front of |
I like the idea and I want to improve the conditional formatting because the current formatting can be hard to read. That's why Black's now parenthesizes long conditionals but I would prefer to find a more readable style without adding extra parentheses (see psf/black#4123) |
I'm with @tylerlaprade on indenting the Here's an example foo = Foo(
bar_id=bar.id,
baz_id=f'{baz.name}_{bar_numer}_{idx}'
if bar_number
else f'{baz.name}_{idx}',
zap=zap,
) Desired ruff formatting (which ruff format does not allow) foo = Foo(
bar_id=bar.id,
baz_id=f'{baz.name}_{bar_numer}_{idx}'
if bar_number
else f'{baz.name}_{idx}',
zap=zap,
) This makes it easier to glean while skimming code that it is a ternary and not part of the arg list |
Ternary statements that are broken across multiple lines are indented at the same level as the parent line, making them very hard to read, especially if surrounded by additional logic. Prettier handles the equivalent case in JS/TS by adding an indentation level to the ternaries. This would likely need to be behind a flag for ease of migration from Black, but ideally this would be the only behavior if that constraint didn't exist.
Ruff
Prettier
The text was updated successfully, but these errors were encountered: