google / google/yapf

Yapf could insert parentheses for better style

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

Description

Sometimes, if we add parentheses to an expression, yapf gives a better reformatting. Here's an example.

Using this config:

```
[style]
dedent_closing_brackets = true
```

## Without surrounding parentheses

Input code:
```
if aaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbb, ccccccccccccc) and dddddddddddddddd(eeeeeeeeeeeeeeeeee) == fffffffffffffffffffff:
pass
```

Reformatted by yapf:

```
if aaaaaaaaaaaaaaaaa(
bbbbbbbbbbbbbbbbbb, ccccccccccccc
) and dddddddddddddddd(eeeeeeeeeeeeeeeeee) == fffffffffffffffffffff:
pass
```

yapf reuses the parentheses of the aaa* function to keep the line continuation, which is clever, but it doesn't look as good as the next reformatting.

## With surrounding parentheses

I'm adding parentheses to the input code to "hint" yapf to reformat more:

```
if (aaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbb, ccccccccccccc) and dddddddddddddddd(eeeeeeeeeeeeeeeeee) == fffffffffffffffffffff):
pass
```

yapf now gives the following output:

```
if (
aaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbb, ccccccccccccc)
and dddddddddddddddd(eeeeeeeeeeeeeeeeee) == fffffffffffffffffffff
):
pass
```

which looks much better.

yapf should be able to add parentheses by itself.

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.