Skip to content

reformatting some chained ternaries decreases their readability #4667

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

Open
jap opened this issue May 27, 2025 · 2 comments
Open

reformatting some chained ternaries decreases their readability #4667

jap opened this issue May 27, 2025 · 2 comments
Labels
T: bug Something isn't working

Comments

@jap
Copy link

jap commented May 27, 2025

Just a data point, I was surprised by how black treated my code: reformatting some chained ternaries decreases their readability.

To Reproduce

            assert xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx == (
                xxxxxxxxxxxxx if xxxxxxxxxxx == 0 else
                xxxxxxxxxxxxxxxxxx if xxxxxxxxxxx == 1 else
                xxxxxxxxxxxxxxxxxxxx
            )

is reformatted as:

            assert xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx == (
                xxxxxxxxxxxxx
                if xxxxxxxxxxx == 0
                else xxxxxxxxxxxxxxxxxx if xxxxxxxxxxx == 1 else xxxxxxxxxxxxxxxxxxxx
            )

which no longer has the structure that was present in to first.
(If only we had match expressions in Python.)

@jap jap added the T: bug Something isn't working label May 27, 2025
@tusharsadhwani
Copy link
Collaborator

Thanks for the input! The style of breaking ternaries before the else is intentional, as opposed to after the else in your preferred style. Had the else clause been a bit longer the second else would also get split into its own line.

Nonetheless I appreciate the data point.

@tusharsadhwani
Copy link
Collaborator

(p.s. I did end up starting a discussion on match expressions on the discuss forum, feel free to add your thoughts there, maybe we'll see something come out of it!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants