google / google/yapf

Bad formatting in long function arguments with conditional expression

Open
#730 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

Hi!

I have a code that calls with complex arguments, and some of them have conditional expression.
For example, the code below is the minimal exploit that show this issue:
```python
def foo():
return call.me.by.dot(
key1=val1,
key2=val2,
key3=self._val_helper(
other.helper(
val1,
keykey1=val2,
keykey2=val3,
keykey3=val4,
keykey4=val5,
keykeykey=val3) if self._long_long_foo() else ()))
```

For the exploit, I ran yapf with `--style='{based_on_style=pep8, column_limit=50}'` option.

Since the `self._long_long_foo()` exceeds the column limit, I thought that whole `self._long_long_foo()` should be wrapped after formatting, but it wasn't:

```python
def foo():
return call.me.by.dot(
key1=val1,
key2=val2,
key3=self._val_helper(
other.helper(val1,
keykey1=val2,
keykey2=val3,
keykey3=val4,
keykey4=val5,
keykeykey=val3) if self.
_long_long_foo() else ()))
```

I think it is better to format not to wrap before the dot in `self.`, but to wrap before `if`, so that the reformatted code should look at least like:
```python
def foo():
return call.me.by.dot(
key1=val1,
key2=val2,
key3=self._val_helper(
other.helper(
val1,
keykey1=val2,
keykey2=val3,
keykey3=val4,
keykey4=val5,
keykeykey=val3) if
self._long_long_foo() else ()))
```
or, for the best,
```python
def foo():
return call.me.by.dot(
key1=val1,
key2=val2,
key3=self._val_helper(
other.helper(
val1,
keykey1=val2,
keykey2=val3,
keykey3=val4,
keykey4=val5,
keykeykey=val3)
if self._long_long_foo() else ()))
```

What do you think about this? It would be grateful if you consider this issue.
Thank you!

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.