google / google/yapf

Knob for keeping the first line of multi-line dictionary entries on the same line as the key

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

Description

I often create dictionaries that store an instance of an object. I would initialize this dictionary as:

```python
my_dict = {
'key1': module.Foo(
keyword_arg_1=value_1,
keyword_arg_2=value_2,
keyword_arg_3=value_3,
keyword_arg_4=value_4,
),
'key2': module.Foo(
keyword_arg_1=value_1,
keyword_arg_2=value_2,
keyword_arg_3=value_3,
keyword_arg_4=value_4,
),
}
```

Currently, I use the following configuration:

```
[style]
based_on_style = pep8
column_limit = 100
dedent_closing_brackets = True
indent_dictionary_value = True
indent_width = 2
join_multiple_lines = False
spaces_before_comment = 1
split_before_logical_operator = True
each_dict_entry_on_separate_line = True
```

With the above configuration, yapf reformats my code such that the dictionary entries are all moved to the following line:

```python
'key1':
module.Foo(
keyword_arg_1=value_1,
keyword_arg_2=value_2,
keyword_arg_3=value_3,
keyword_arg_4=value_4,
),
'key2':
peripheral.VariableSpec(
keyword_arg_1=value_1,
keyword_arg_2=value_2,
keyword_arg_3=value_3,
keyword_arg_4=value_4,
),
```

I'd really like to keep the original style without having to mark every instance of this with a `# yapf: disable`. Is there any way to retain the original style with the current yapf knobs?

If not, would it make sense to add a knob to accomplish this?

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.