yapf icon indicating copy to clipboard operation
yapf copied to clipboard

Ternary expression is split strangely.

Open dseomn opened this issue 5 years ago • 2 comments

With yapf 0.29.0, google style, I'm getting this:

next_playable_unit = self._next_playable_unit_callback(
    None if self._prepared_playable_unit_id is None else self.
    _playable_units[self._prepared_playable_unit_id])

I was expecting something more like this:

next_playable_unit = self._next_playable_unit_callback(
    None if self._prepared_playable_unit_id is None
    else self._playable_units[self._prepared_playable_unit_id])

dseomn avatar Jan 30 '20 03:01 dseomn

I am doing some tryouts and I aggree with you. It seems Yapf is very reluctant to split before the else clause. I just have a simple but too long ternary assignment which is always split before the if clause:

message = "Hard delete." if my_long_condition_variable == 'my_long_condition_name' else ''

Yapf (with the help of the surrounding parentheses) reformats this to:

message = ("Hard delete."
           if my_long_condition_variable == 'my_long_condition_name' else '')

But I would expect it to split before the else, just like you (it seems more natural to a human being, doesn't it?). The result would be:

message = ("Hard delete." if my_long_condition_variable == 'my_long_condition_name'
           else '')

LECbg avatar Jun 30 '21 15:06 LECbg

YAPF tries to keep tightly-bound regions together. In this case, it sees the if - then - else bit as being more tightly bound. This is arguably wrong,...

bwendling avatar Jun 30 '21 21:06 bwendling