Skip to content

Commit

Permalink
Fix SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED for one-item argument lists (
Browse files Browse the repository at this point in the history
…#1142)

Turn

r = f0(1,)
into

r = f0(
    1,
)

in case SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED is enabled
  • Loading branch information
alexey-pelykh authored Sep 19, 2023
1 parent 16ad355 commit 11b284e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
by removing the "verify" parameter.
- Changed FORCE_MULTILINE_DICT to override SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES.
- Adopt pyproject.toml (PEP 517) for build system
### Fixed
- Do not treat variables named `match` as the match keyword
- Fix SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED for one-item argument lists.

## [0.40.1] 2023-06-20
### Fixed
Expand Down
9 changes: 7 additions & 2 deletions yapf/yapflib/reformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,13 @@ def _CanPlaceOnSingleLine(line):
Returns:
True if the line can or should be added to a single line. False otherwise.
"""
token_names = [x.name for x in line.tokens]
if (style.Get('FORCE_MULTILINE_DICT') and 'LBRACE' in token_names):
token_types = [x.type for x in line.tokens]
if (style.Get('SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED') and
any(token_types[token_index - 1] == token.COMMA
for token_index, token_type in enumerate(token_types[1:], start=1)
if token_type == token.RPAR)):
return False
if (style.Get('FORCE_MULTILINE_DICT') and token.LBRACE in token_types):
return False
indent_amt = style.Get('INDENT_WIDTH') * line.depth
last = line.last
Expand Down
6 changes: 6 additions & 0 deletions yapftests/reformatter_basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2275,6 +2275,8 @@ def testSplittingArgumentsTerminatedByComma(self):
a_very_long_function_name(long_argument_name_1, long_argument_name_2, long_argument_name_3, long_argument_name_4,)
r =f0 (1, 2,3,)
r =f0 (1,)
""") # noqa
expected_formatted_code = textwrap.dedent("""\
function_name(argument_name_1=1, argument_name_2=2, argument_name_3=3)
Expand Down Expand Up @@ -2303,6 +2305,10 @@ def testSplittingArgumentsTerminatedByComma(self):
2,
3,
)
r = f0(
1,
)
""")

try:
Expand Down

0 comments on commit 11b284e

Please sign in to comment.