diff --git a/src/black/linegen.py b/src/black/linegen.py index 219495e9a5e..2102697fada 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -815,6 +815,27 @@ def bracket_split_build_line( return result +def _find_last_non_standalone_comment(line: Line) -> Optional[int]: + last_leaf = line.leaves[-1] + if last_leaf.type == STANDALONE_COMMENT: + for leaf_idx in range(len(line.leaves) - 1, 0, -1): + if line.leaves[leaf_idx].type != STANDALONE_COMMENT: + return leaf_idx + return None + + +def _safe_add_trailing_comma(line: Line, safe: bool, delimiter_priority: int) -> Line: + if ( + safe + and delimiter_priority == COMMA_PRIORITY + and line.leaves[-1].type != token.COMMA + and line.leaves[-1].type != STANDALONE_COMMENT + ): + new_comma = Leaf(token.COMMA, ",") + line.append(new_comma) + return line + + def dont_increase_indentation(split_func: Transformer) -> Transformer: """Normalize prefix of the first leaf in every line returned by `split_func`. @@ -831,7 +852,8 @@ def split_wrapper(line: Line, features: Collection[Feature] = ()) -> Iterator[Li @dont_increase_indentation -def delimiter_split(line: Line, features: Collection[Feature] = ()) -> Iterator[Line]: +def delimiter_split(line: Line, # noqa: C901 + features: Collection[Feature] = ()) -> Iterator[Line]: """Split according to delimiters of the highest priority. If the appropriate Features are given, the split will add trailing commas @@ -871,7 +893,9 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]: ) current_line.append(leaf) - for leaf in line.leaves: + last_non_comment_leaf = _find_last_non_standalone_comment(line) + + for idx, leaf in enumerate(line.leaves): yield from append_to_line(leaf) for comment_after in line.comments_after(leaf): @@ -887,7 +911,10 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]: trailing_comma_safe = ( trailing_comma_safe and Feature.TRAILING_COMMA_IN_CALL in features ) - + if idx == last_non_comment_leaf: + current_line = _safe_add_trailing_comma( + current_line, trailing_comma_safe, delimiter_priority + ) leaf_priority = bt.delimiters.get(id(leaf)) if leaf_priority == delimiter_priority: yield current_line @@ -896,14 +923,10 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]: mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets ) if current_line: - if ( - trailing_comma_safe - and delimiter_priority == COMMA_PRIORITY - and current_line.leaves[-1].type != token.COMMA - and current_line.leaves[-1].type != STANDALONE_COMMENT - ): - new_comma = Leaf(token.COMMA, ",") - current_line.append(new_comma) + if not last_non_comment_leaf: + current_line = _safe_add_trailing_comma( + current_line, trailing_comma_safe, delimiter_priority + ) yield current_line diff --git a/tests/data/simple_cases/function_trailing_comma.py b/tests/data/simple_cases/function_trailing_comma.py index abe9617c0e9..b8b52fbab97 100644 --- a/tests/data/simple_cases/function_trailing_comma.py +++ b/tests/data/simple_cases/function_trailing_comma.py @@ -13,6 +13,12 @@ def f(a:int=1,): "a": 1, "b": 2, }["a"] + test_dict = { + "a": call("a", "b"), + "xxxxxxxxxxxxxxxxxxxxxxxxxxxx": ..., + "xxxxxxxxxxxxxxxxxxxxxxxxxxxx": ...,"yyyyyyy": ... + # "comment": ... + } if a == {"a": 1,"b": 2,"c": 3,"d": 4,"e": 5,"f": 6,"g": 7,"h": 8,}["a"]: pass @@ -100,6 +106,13 @@ def f( "a": 1, "b": 2, }["a"] + test_dict = { + "a": call("a", "b"), + "xxxxxxxxxxxxxxxxxxxxxxxxxxxx": ..., + "xxxxxxxxxxxxxxxxxxxxxxxxxxxx": ..., + "yyyyyyy": ..., + # "comment": ... + } if ( a == {