From 11a5cb8948e311eaa39946321064f33a041c3a60 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 4 Nov 2023 18:50:57 +0200 Subject: [PATCH 1/2] No-op refactor to make next commit clearer --- src/black/lines.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/black/lines.py b/src/black/lines.py index a73c429e3d9..b9716a97cba 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -351,9 +351,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, @@ -369,16 +369,20 @@ def has_magic_trailing_comma( 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( + + if ( + comma.parent.type == syms.subscriptlist + and closing.opening_bracket is not None + and is_one_sequence_between( closing.opening_bracket, closing, self.leaves, brackets=(token.LSQB, token.RSQB), ) - ) + ): + return False + + return True if self.is_import: return True From ac21ded9498aeb54523bdb1ed45562224409ae8c Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 4 Nov 2023 19:55:23 +0200 Subject: [PATCH 2/2] Remove redundant condition from `has_magic_comma` 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))* [','] ``` --- src/black/lines.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/black/lines.py b/src/black/lines.py index b9716a97cba..fb542deae43 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -363,25 +363,6 @@ def has_magic_trailing_comma( ): return False - if not ensure_removable: - return True - - comma = self.leaves[-1] - if comma.parent is None: - return False - - if ( - comma.parent.type == syms.subscriptlist - and closing.opening_bracket is not None - and is_one_sequence_between( - closing.opening_bracket, - closing, - self.leaves, - brackets=(token.LSQB, token.RSQB), - ) - ): - return False - return True if self.is_import: