Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing missing trailing commas in dicts when standalone comment is present - 3072 #3389

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 34 additions & 11 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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


Expand Down
13 changes: 13 additions & 0 deletions tests/data/simple_cases/function_trailing_comma.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -100,6 +106,13 @@ def f(
"a": 1,
"b": 2,
}["a"]
test_dict = {
"a": call("a", "b"),
"xxxxxxxxxxxxxxxxxxxxxxxxxxxx": ...,
"xxxxxxxxxxxxxxxxxxxxxxxxxxxx": ...,
"yyyyyyy": ...,
# "comment": ...
}
if (
a
== {
Expand Down