Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SPLIT_ALL_[TOP_LEVEL_]COMMA_SEPARATED_VALUES for lambdas and unpacking #1173

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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