Fix SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED for one-item named argument lists by taking precedence over SPLIT_BEFORE_NAMED_ASSIGNS (#1160)
By taking precedence over SPLIT_BEFORE_NAMED_ASSIGNS, turn
r = f0(a=1,)
into
r = f0(
a=1,
)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 14f671f..d8127cf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,7 +6,9 @@
### Changes
- Remove dependency on importlib-metadata
- Remove dependency on tomli when using >= py311
-
+### 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
diff --git a/yapf/yapflib/format_decision_state.py b/yapf/yapflib/format_decision_state.py
index bc7f977..ce74313 100644
--- a/yapf/yapflib/format_decision_state.py
+++ b/yapf/yapflib/format_decision_state.py
@@ -373,6 +373,16 @@
###########################################################################
# 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
@@ -409,15 +419,6 @@
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
diff --git a/yapftests/reformatter_basic_test.py b/yapftests/reformatter_basic_test.py
index ebc2cd3..24f34a6 100644
--- a/yapftests/reformatter_basic_test.py
+++ b/yapftests/reformatter_basic_test.py
@@ -2274,6 +2274,8 @@
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)
@@ -2306,6 +2308,10 @@
r = f0(
1,
)
+
+ r = f0(
+ a=1,
+ )
""")
try: