google / google/yapf

Format long list of lists

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

Description

Hello,

I'm encountering an issue with how yapf handles the formatting of a very long list of lists. With my configuration file (see attached file below), the following code:

```python
for idxs in ([1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 0], [1, 1, 0, 1, 1, 0], [1, 1, 0, 0, 1, 0], [1, 1, 1, 1, 1, 1]):
pass
```

is formatted by yapf as follows:

```python
for idxs in ([1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 0], [1, 1, 0, 1, 1,
0], [1, 1, 0, 0, 1,
0], [1, 1, 1, 1, 1, 1]):
pass
```

which is really ugly. The expected formatting would be:

```python
for idxs in (
[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 0], [1, 1, 0, 1, 1, 0], [1, 1, 0, 0, 1, 0],
[1, 1, 1, 1, 1, 1]
):
pass
```

Similarly, if `process` is a function,

```python
process([1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 0], [1, 1, 0, 1, 1, 0], [1, 1, 0, 0, 1, 0], [1, 1, 1, 1, 1, 1])
```

yields:

```python
process([1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 0], [1, 1, 0, 1, 1, 0], [1, 1, 0, 0, 1, 0],
[1, 1, 1, 1, 1, 1])
```

while the expected formatting (with my settings) is:

```python
process(
[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 0], [1, 1, 0, 1, 1, 0], [1, 1, 0, 0, 1, 0],
[1, 1, 1, 1, 1, 1]
)
```

However,

```python
process(qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf,qdf,qdf)
```

is correctly formatted as expected:

```python
process(
qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf, qdf,
qdf
)
```

Is this an incorrect behavior of yapf, or is it a configuration issue on my part? Thank you for your help.

Python 3.11.5 - Windows 10 x64 - yapf 0.40.1
Usage: vscode 1.84.2, eeyore 0.1.10
[style.yapf.txt](https://github.com/google/yapf/files/13362306/style.yapf.txt)

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the examples with the attached style.yapf.txt configuration on yapf 0.40.1, including both the list-of-lists loop and function-call cases. Trace the formatter's handling of long argument and collection lists, then add regression coverage showing the expected wrapping for these inputs.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.