black icon indicating copy to clipboard operation
black copied to clipboard

reformatting some chained ternaries decreases their readability

Open jap opened this issue 11 months ago • 2 comments

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 avatar May 27 '25 14:05 jap

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 avatar May 27 '25 14:05 tusharsadhwani

(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!)

tusharsadhwani avatar May 27 '25 14:05 tusharsadhwani