Skip to content
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

Open
tylerlaprade opened this issue Jan 30, 2024 · 4 comments
Open

[Feature request] Indentation for ternary statements #9722

tylerlaprade opened this issue Jan 30, 2024 · 4 comments
Labels
formatter Related to the formatter style How should formatted code look

Comments

@tylerlaprade
Copy link
Contributor

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

image

Prettier

image
@AlexWaygood AlexWaygood added the formatter Related to the formatter label Jan 30, 2024
@JelleZijlstra
Copy link
Contributor

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.

@tylerlaprade
Copy link
Contributor Author

That's a good point, I hadn't considered that difference. I think a single additional indent in front of if and else would make the most sense.

@MichaReiser MichaReiser added the style How should formatted code look label Jan 31, 2024
@MichaReiser
Copy link
Member

MichaReiser commented Jan 31, 2024

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)

@vanakema
Copy link

vanakema commented Nov 5, 2024

I'm with @tylerlaprade on indenting the if and else. It's simple and it makes it way more readable.

Here's an example
Current Ruff formatting

        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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter style How should formatted code look
Projects
None yet
Development

No branches or pull requests

5 participants