google / google/yapf

Dictionary literals in function calls are unexpectedly expanded

Open
#1,119 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

When YAPF splits a long function call with a short dictionary literal, the dictionary is unexpectedly always spread across three lines, despite fitting into one line easily:

```python
important_function_call("There", "are many, many", "args", None, None, 200., ["a", "b", "c"], {"a": 0, "b": 1},)
```

In this example, the trailing comma is added to ensure one arg per line (`split_arguments_when_comma_terminated = true` is set), but this is formatted as

```python
important_function_call(
"There",
"are many, many",
"args",
None,
None,
200.,
["a", "b", "c"],
300.,
{
"a": 0, "b": 1 # <---- this is unexpected
},
)
```

I would have expected this to be formatted as

```python
important_function_call(
"There",
"are many, many",
"args",
None,
None,
200.,
["a", "b", "c"],
300.,
{"a": 0, "b": 1},
)
```

Formatting style used:
```ini
[style]
based_on_style = pep8
dedent_closing_brackets = true
indent_dictionary_value = true
split_all_top_level_comma_separated_values = true
split_arguments_when_comma_terminated = true
split_before_arithmetic_operator = true
split_before_first_argument = true
split_complex_comprehension = true
```

Minimum required to reproduce this behavior:
```ini
[style]
based_on_style = pep8
split_all_top_level_comma_separated_values = true
split_arguments_when_comma_terminated = true
```

All other dictionaries are formatted as expected, i.e. placed into only one line as long as it fits, except when inside a function call. I am not sure if this is expected behavior or not. If this is expected behavior, is there a knob to turn this off?

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.