yapf icon indicating copy to clipboard operation
yapf copied to clipboard

split_all_top_level_comma_separated_values makes force_multiline_dict ineffective

Open AndydeCleyre opened this issue 5 years ago • 8 comments

$ yapf --version
yapf 0.30.0
$ cat .style.yapf
[style]
each_dict_entry_on_separate_line=True
force_multiline_dict=True
split_all_top_level_comma_separated_values=True
$ cat test.py
COLOR_GROUPS = {
    'Meta-switches': magenta,
    'Switches': yellow,
    'Subcommands': blue
}
$ yapf --style .style.yapf -d test.py
--- test.py	(original)
+++ test.py	(reformatted)
@@ -1,5 +1,3 @@
 COLOR_GROUPS = {
-    'Meta-switches': magenta,
-    'Switches': yellow,
-    'Subcommands': blue
+    'Meta-switches': magenta, 'Switches': yellow, 'Subcommands': blue
 }

AndydeCleyre avatar May 01 '20 00:05 AndydeCleyre

You mean split_all_comma_separated_values or split_all_top_level_comma_separated_values?

kliyes avatar Jun 17 '20 02:06 kliyes

Whoops, thanks: split_all_top_level_comma_separated_values. I'll update the title.

AndydeCleyre avatar Jun 20 '20 18:06 AndydeCleyre

@brianmego Can you reproduce?

AndydeCleyre avatar Aug 02 '20 19:08 AndydeCleyre

I've pinpointed the bad behavior at yapf/yapflib/format_decision_state.py#L201-L204, FormatDecisionState.MustSplit:

# Allow the fallthrough code to handle the closing bracket.
if current != opening.matching_bracket:
  # If the container doesn't fit in the current line, must split
  return not self._ContainerFitsOnStartLine(opening)

Which was formerly just:

# If the container doesn't fit in the current line, must split
return not self._ContainerFitsOnStartLine(opening)

Which is problematic in the same way. 5de9ca6e546acb1730b86e982876e9cbe827bc89

AndydeCleyre avatar Aug 02 '20 19:08 AndydeCleyre

Well, I can reproduce this, though I'm not sure what we want to do about it. These two flags are sort of at odds with each other, I think. One says, "Split under all conditions" while the other says "Don't split unless the line is super long". Proof of the issue. This test fails:

https://github.com/brianmego/yapf/commit/52442c1adb7ee2b90471888d2e403bcd47c7143f

brianmego avatar Aug 04 '20 01:08 brianmego

I would think the force multiline dict knob should win, since its target is more specific, and because it's a "force" knob. I would think the other multi line dict knob should win, because its target is specific.

It feels like I'm saying, "split x, definitely split x, and also split x in some specific cases," and hearing back "no problem we found a split x and unsplit it for you."

AndydeCleyre avatar Aug 04 '20 02:08 AndydeCleyre

I still think that if the config includes

each_dict_entry_on_separate_line=True
force_multiline_dict=True

then dict entries ought to be placed on separate lines.

AndydeCleyre avatar Nov 05 '20 18:11 AndydeCleyre

This issue hit me too and blocks me from using yapf.

I think with each_dict_entry_on_separate_line=True but force_multiline_dict=False the issue should also no occur.

spaceone avatar Oct 07 '21 23:10 spaceone