Clean up dead code in magic trailing comma logic (#4131)

This commit is contained in:
Shantanu 2024-01-01 17:29:21 -08:00 committed by GitHub
parent 8e0a9dee1b
commit 269190274b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -91,7 +91,7 @@ def append(
if self.mode.magic_trailing_comma:
if self.has_magic_trailing_comma(leaf):
self.magic_trailing_comma = leaf
elif self.has_magic_trailing_comma(leaf, ensure_removable=True):
elif self.has_magic_trailing_comma(leaf):
self.remove_trailing_comma()
if not self.append_comment(leaf):
self.leaves.append(leaf)
@ -342,16 +342,11 @@ def contains_unsplittable_type_ignore(self) -> bool:
def contains_multiline_strings(self) -> bool:
return any(is_multiline_string(leaf) for leaf in self.leaves)
def has_magic_trailing_comma(
self, closing: Leaf, ensure_removable: bool = False
) -> bool:
def has_magic_trailing_comma(self, closing: Leaf) -> bool:
"""Return True if we have a magic trailing comma, that is when:
- there's a trailing comma here
- it's not from single-element square bracket indexing
- it's not a one-tuple
- it's not a single-element subscript
Additionally, if ensure_removable:
- it's not from square bracket indexing
(specifically, single-element square bracket indexing)
"""
if not (
closing.type in CLOSING_BRACKETS
@ -375,6 +370,8 @@ def has_magic_trailing_comma(
brackets=(token.LSQB, token.RSQB),
)
):
assert closing.prev_sibling is not None
assert closing.prev_sibling.type == syms.subscriptlist
return False
return True