Treat all trailing commas as pre-existing, as they effectively are

On a second pass of Black on the same file, inserted trailing commas are now
pre-existing.  Doesn't make sense to differentiate between the passes then.
This commit is contained in:
Łukasz Langa 2020-08-26 12:22:56 +02:00
parent 824d06f720
commit d7aa7f3cdd

View File

@ -1627,14 +1627,13 @@ def contains_multiline_strings(self) -> bool:
def maybe_should_explode(self, closing: Leaf) -> bool: def maybe_should_explode(self, closing: Leaf) -> bool:
"""Return True if this line should explode (always be split), that is when: """Return True if this line should explode (always be split), that is when:
- there's a pre-existing trailing comma here; and - there's a trailing comma here; and
- it's not a one-tuple. - it's not a one-tuple.
""" """
if not ( if not (
closing.type in CLOSING_BRACKETS closing.type in CLOSING_BRACKETS
and self.leaves and self.leaves
and self.leaves[-1].type == token.COMMA and self.leaves[-1].type == token.COMMA
and not self.leaves[-1].was_checked # pre-existing
): ):
return False return False
@ -2661,7 +2660,7 @@ def rhs(line: Line, features: Collection[Feature]) -> Iterator[Line]:
# All splits failed, best effort split with no omits. # All splits failed, best effort split with no omits.
# This mostly happens to multiline strings that are by definition # This mostly happens to multiline strings that are by definition
# reported as not fitting a single line, as well as lines that contain # reported as not fitting a single line, as well as lines that contain
# pre-existing trailing commas (those have to be exploded). # trailing commas (those have to be exploded).
yield from right_hand_split( yield from right_hand_split(
line, line_length=mode.line_length, features=features line, line_length=mode.line_length, features=features
) )
@ -4855,7 +4854,6 @@ def bracket_split_build_line(
if leaves[i].type != token.COMMA: if leaves[i].type != token.COMMA:
new_comma = Leaf(token.COMMA, ",") new_comma = Leaf(token.COMMA, ",")
new_comma.was_checked = True
leaves.insert(i + 1, new_comma) leaves.insert(i + 1, new_comma)
break break
@ -4951,7 +4949,6 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]:
and current_line.leaves[-1].type != STANDALONE_COMMENT and current_line.leaves[-1].type != STANDALONE_COMMENT
): ):
new_comma = Leaf(token.COMMA, ",") new_comma = Leaf(token.COMMA, ",")
new_comma.was_checked = True
current_line.append(new_comma) current_line.append(new_comma)
yield current_line yield current_line
@ -5584,20 +5581,20 @@ def should_split_body_explode(line: Line, opening_bracket: Leaf) -> bool:
# than one of them (we're excluding the trailing comma and if the delimiter priority # than one of them (we're excluding the trailing comma and if the delimiter priority
# is still commas, that means there's more). # is still commas, that means there's more).
exclude = set() exclude = set()
pre_existing_trailing_comma = False trailing_comma = False
try: try:
last_leaf = line.leaves[-1] last_leaf = line.leaves[-1]
if last_leaf.type == token.COMMA: if last_leaf.type == token.COMMA:
pre_existing_trailing_comma = not last_leaf.was_checked trailing_comma = True
exclude.add(id(last_leaf)) exclude.add(id(last_leaf))
max_priority = line.bracket_tracker.max_delimiter_priority(exclude=exclude) max_priority = line.bracket_tracker.max_delimiter_priority(exclude=exclude)
except (IndexError, ValueError): except (IndexError, ValueError):
return False return False
return max_priority == COMMA_PRIORITY and ( return max_priority == COMMA_PRIORITY and (
trailing_comma
# always explode imports # always explode imports
opening_bracket.parent.type in {syms.atom, syms.import_from} or opening_bracket.parent.type in {syms.atom, syms.import_from}
or pre_existing_trailing_comma
) )
@ -5727,12 +5724,11 @@ def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[Leaf
line.should_explode line.should_explode
and prev and prev
and prev.type == token.COMMA and prev.type == token.COMMA
and not prev.was_checked
and not is_one_tuple_between( and not is_one_tuple_between(
leaf.opening_bracket, leaf, line.leaves leaf.opening_bracket, leaf, line.leaves
) )
): ):
# Never omit bracket pairs with pre-existing trailing commas. # Never omit bracket pairs with trailing commas.
# We need to explode on those. # We need to explode on those.
break break
@ -5756,10 +5752,9 @@ def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[Leaf
line.should_explode line.should_explode
and prev and prev
and prev.type == token.COMMA and prev.type == token.COMMA
and not prev.was_checked
and not is_one_tuple_between(leaf.opening_bracket, leaf, line.leaves) and not is_one_tuple_between(leaf.opening_bracket, leaf, line.leaves)
): ):
# Never omit bracket pairs with pre-existing trailing commas. # Never omit bracket pairs with trailing commas.
# We need to explode on those. # We need to explode on those.
break break
@ -6412,11 +6407,7 @@ def can_omit_invisible_parens(
# unnecessary. # unnecessary.
return True return True
if ( if line.should_explode and penultimate.type == token.COMMA:
line.should_explode
and penultimate.type == token.COMMA
and not penultimate.was_checked
):
# The rightmost non-omitted bracket pair is the one we want to explode on. # The rightmost non-omitted bracket pair is the one we want to explode on.
return True return True