Skip to content

Commit

Permalink
Fix SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED for one-item named argument…
Browse files Browse the repository at this point in the history
… lists by taking precedence over SPLIT_BEFORE_NAMED_ASSIGNS
  • Loading branch information
alexey-pelykh committed Oct 6, 2023
1 parent d553003 commit b5597a0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# All notable changes to this project will be documented in this file.
# This project adheres to [Semantic Versioning](http://semver.org/).

## '[0.41.0] 'UNRELEASED
### Fixed
- Fix SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED for one-item named argument lists by taking precedence over SPLIT_BEFORE_NAMED_ASSIGNS.

## [0.40.2] 2023-09-22
### Changes
- The verification module has been removed. NOTE: this changes the public APIs
Expand Down
19 changes: 10 additions & 9 deletions yapf/yapflib/format_decision_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,16 @@ def SurroundedByParens(token):

###########################################################################
# Argument List Splitting

if style.Get('SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED'):
# Split before arguments in a function call or definition if the
# arguments are terminated by a comma.
opening = _GetOpeningBracket(current)
if opening and opening.previous_token and opening.previous_token.is_name:
if previous.value in '(,':
if opening.matching_bracket.previous_token.value == ',':
return True

if (style.Get('SPLIT_BEFORE_NAMED_ASSIGNS') and not current.is_comment and
subtypes.DEFAULT_OR_NAMED_ASSIGN_ARG_LIST in current.subtypes):
if (previous.value not in {'=', ':', '*', '**'} and
Expand Down Expand Up @@ -409,15 +419,6 @@ def SurroundedByParens(token):
self._ArgumentListHasDictionaryEntry(current)):
return True

if style.Get('SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED'):
# Split before arguments in a function call or definition if the
# arguments are terminated by a comma.
opening = _GetOpeningBracket(current)
if opening and opening.previous_token and opening.previous_token.is_name:
if previous.value in '(,':
if opening.matching_bracket.previous_token.value == ',':
return True

if ((current.is_name or current.value in {'*', '**'}) and
previous.value == ','):
# If we have a function call within an argument list and it won't fit on
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 @@ -2277,6 +2277,8 @@ def testSplittingArgumentsTerminatedByComma(self):
r =f0 (1, 2,3,)
r =f0 (1,)
r =f0 (a=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 @@ -2309,6 +2311,10 @@ def testSplittingArgumentsTerminatedByComma(self):
r = f0(
1,
)
r = f0(
a=1,
)
""")

try:
Expand Down

0 comments on commit b5597a0

Please sign in to comment.