google / google/yapf

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

Open
#1,063 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
14k
Forks
904
PR merge metrics
No merged PRs in 30d

Description

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):

``` py
# 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)
```

``` py
# 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.

```py
# 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:
``` py
# 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)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.