yapf icon indicating copy to clipboard operation
yapf copied to clipboard

Setting to avoid line breaks after opening/closing bracket in list-comprehensions

Open fzeiser opened this issue 1 year ago • 0 comments

I use yapf v 0.32.0 and try to find a setting to format list comprehensions without introducing "extra" line breaks before and after the opening and closing brackets. I would like to get a behavior similar to a tuple (generator expression):

# original code
[function_with_really_long_name(i) for i in [1,2,3] if function_with_really_long_name(i) is not None]

(function_with_really_long_name(i) for i in [1,2,3] if function_with_really_long_name(i) is not None)
# formats to 
[
    function_with_really_long_name(i)
    for i in [1, 2, 3]
    if function_with_really_long_name(i) is not None
]

(function_with_really_long_name(i)
 for i in [1, 2, 3]
 if function_with_really_long_name(i) is not None)

As indicated in https://github.com/google/yapf/issues/549, I can use split_before_closing_bracket=false to adress the closing bracket, but there is no option to set split_before_opening_bracket=false. I tried SPLIT_BEFORE_EXPRESSION_AFTER_OPENING_PAREN and SPLIT_BEFORE_FIRST_ARGUMENT, but without success.

# based on google, but with `split_before_closing_bracket=false`
[
    function_with_really_long_name(i)
    for i in [1, 2, 3]
    if function_with_really_long_name(i) is not None]

(function_with_really_long_name(i)
 for i in [1, 2, 3]
 if function_with_really_long_name(i) is not None)

Goal:

# I would want this formatting:
[function_with_really_long_name(i)
 for i in [1, 2, 3]
 if function_with_really_long_name(i) is not None]

(function_with_really_long_name(i)
 for i in [1, 2, 3]
 if function_with_really_long_name(i) is not None)

fzeiser avatar Mar 17 '23 10:03 fzeiser