google / yapf

A formatter for Python files

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dictionary literals in function calls are unexpectedly expanded

MilanStaffehl opened this issue · comments

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:

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

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

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

Formatting style used:

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

[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?