From 849ed5dba164f15b79acf51b7ad6a60ea5073561 Mon Sep 17 00:00:00 2001 From: Alexey Pelykh Date: Tue, 10 Dec 2024 09:40:38 +0100 Subject: [PATCH] (fix) SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED: skip comment --- yapf/yapflib/format_decision_state.py | 4 ++++ yapftests/reformatter_basic_test.py | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/yapf/yapflib/format_decision_state.py b/yapf/yapflib/format_decision_state.py index 06f3455d9..ad1549a00 100644 --- a/yapf/yapflib/format_decision_state.py +++ b/yapf/yapflib/format_decision_state.py @@ -390,6 +390,10 @@ def SurroundedByParens(token): if opening and opening.previous_token and opening.previous_token.is_name: if previous.value in '(,': if opening.matching_bracket.previous_token.value == ',': + if current.is_comment: + # Don't require splitting before a comment, since it may be + # related to the current line. + return False return True if (style.Get('SPLIT_BEFORE_NAMED_ASSIGNS') and not current.is_comment and diff --git a/yapftests/reformatter_basic_test.py b/yapftests/reformatter_basic_test.py index 74b1ba405..96b094a40 100644 --- a/yapftests/reformatter_basic_test.py +++ b/yapftests/reformatter_basic_test.py @@ -2336,6 +2336,10 @@ def _pack_results_for_constraint_or(cls, combination, constraints): def testSplittingArgumentsTerminatedByComma(self): unformatted_code = textwrap.dedent("""\ + class Class( # comment + object,): + pass + function_name(argument_name_1=1, argument_name_2=2, argument_name_3=3) function_name(argument_name_1=1, argument_name_2=2, argument_name_3=3,) @@ -2351,6 +2355,12 @@ def testSplittingArgumentsTerminatedByComma(self): r =f0 (a=1,) """) # noqa expected_formatted_code = textwrap.dedent("""\ + class Class( # comment + object, + ): + pass + + function_name(argument_name_1=1, argument_name_2=2, argument_name_3=3) function_name(