Skip to content

Commit

Permalink
Remove redundant condition from has_magic_trailing_comma (#4023)
Browse files Browse the repository at this point in the history
The second `if` cannot be true at its execution point, because it is
already covered by the first `if`. The condition
`comma.parent.type == syms.subscriptlist` always holds if
`closing.parent.type == syms.trailer` holds, because `subscriptlist`
only appears inside `trailer` in the grammar:

```
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
subscriptlist: (subscript|star_expr) (',' (subscript|star_expr))* [',']
```
  • Loading branch information
bluetech committed Nov 8, 2023
1 parent 1a7d9c2 commit 72e7a2e
Showing 1 changed file with 3 additions and 18 deletions.
21 changes: 3 additions & 18 deletions src/black/lines.py
Expand Up @@ -353,9 +353,9 @@ def has_magic_trailing_comma(

if closing.type == token.RSQB:
if (
closing.parent
closing.parent is not None
and closing.parent.type == syms.trailer
and closing.opening_bracket
and closing.opening_bracket is not None
and is_one_sequence_between(
closing.opening_bracket,
closing,
Expand All @@ -365,22 +365,7 @@ def has_magic_trailing_comma(
):
return False

if not ensure_removable:
return True

comma = self.leaves[-1]
if comma.parent is None:
return False
return (
comma.parent.type != syms.subscriptlist
or closing.opening_bracket is None
or not is_one_sequence_between(
closing.opening_bracket,
closing,
self.leaves,
brackets=(token.LSQB, token.RSQB),
)
)
return True

if self.is_import:
return True
Expand Down

0 comments on commit 72e7a2e

Please sign in to comment.