Skip to content

Commit

Permalink
Fix SPLIT_ALL_COMMA_SEPARATED_VALUES and SPLIT_ALL_TOP_LEVEL_COMMA_SE…
Browse files Browse the repository at this point in the history
…PARATED_VALUES being too agressive for lambdas and unpacking. (#1173)

Co-authored-by: Bill Wendling <morbo@google.com>
  • Loading branch information
alexey-pelykh and bwendling authored Oct 17, 2023
1 parent c1d7a21 commit 8dde773
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
### Fixed
- Fix SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED for one-item named argument lists
by taking precedence over SPLIT_BEFORE_NAMED_ASSIGNS.
- Fix SPLIT_ALL_COMMA_SEPARATED_VALUES and SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES
being too agressive for lambdas and unpacking.

## [0.40.2] 2023-09-22
### Changes
Expand Down
5 changes: 5 additions & 0 deletions yapf/pytree/subtype_assigner.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ def Visit_power(self, node): # pylint: disable=invalid-name
if isinstance(child, pytree.Leaf) and child.value == '**':
_AppendTokenSubtype(child, subtypes.BINARY_OPERATOR)

def Visit_lambdef(self, node): # pylint: disable=invalid-name
# trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
_AppendSubtypeRec(node, subtypes.LAMBDEF)
self.DefaultNodeVisit(node)

def Visit_trailer(self, node): # pylint: disable=invalid-name
for child in node.children:
self.Visit(child)
Expand Down
9 changes: 9 additions & 0 deletions yapf/yapflib/format_decision_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ def MustSplit(self):
return False

if style.Get('SPLIT_ALL_COMMA_SEPARATED_VALUES') and previous.value == ',':
if (subtypes.COMP_FOR in current.subtypes or
subtypes.LAMBDEF in current.subtypes):
return False

return True

if (style.Get('FORCE_MULTILINE_DICT') and
Expand All @@ -188,6 +192,11 @@ def MustSplit(self):

if (style.Get('SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES') and
previous.value == ','):

if (subtypes.COMP_FOR in current.subtypes or
subtypes.LAMBDEF in current.subtypes):
return False

# Avoid breaking in a container that fits in the current line if possible
opening = _GetOpeningBracket(current)

Expand Down
1 change: 1 addition & 0 deletions yapf/yapflib/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@
SIMPLE_EXPRESSION = 22
PARAMETER_START = 23
PARAMETER_STOP = 24
LAMBDEF = 25
50 changes: 50 additions & 0 deletions yapftests/reformatter_basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,30 @@ def foo(long_arg,
""")
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
unformatted_code = textwrap.dedent("""\
values = [ lambda arg1, arg2: arg1 + arg2 ]
""") # noqa
expected_formatted_code = textwrap.dedent("""\
values = [
lambda arg1, arg2: arg1 + arg2
]
""")
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
unformatted_code = textwrap.dedent("""\
values = [
(some_arg1, some_arg2) for some_arg1, some_arg2 in values
]
""") # noqa
expected_formatted_code = textwrap.dedent("""\
values = [
(some_arg1,
some_arg2)
for some_arg1, some_arg2 in values
]
""")
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
# There is a test for split_all_top_level_comma_separated_values, with
# different expected value
unformatted_code = textwrap.dedent("""\
Expand Down Expand Up @@ -161,6 +185,32 @@ def foo(long_arg,
""")
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
# Works the same way as split_all_comma_separated_values
unformatted_code = textwrap.dedent("""\
values = [ lambda arg1, arg2: arg1 + arg2 ]
""") # noqa
expected_formatted_code = textwrap.dedent("""\
values = [
lambda arg1, arg2: arg1 + arg2
]
""")
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
# There is a test for split_all_comma_separated_values, with different
# expected value
unformatted_code = textwrap.dedent("""\
values = [
(some_arg1, some_arg2) for some_arg1, some_arg2 in values
]
""") # noqa
expected_formatted_code = textwrap.dedent("""\
values = [
(some_arg1, some_arg2)
for some_arg1, some_arg2 in values
]
""")
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
# There is a test for split_all_comma_separated_values, with different
# expected value
unformatted_code = textwrap.dedent("""\
Expand Down

0 comments on commit 8dde773

Please sign in to comment.