@@ -106,6 +106,7 @@ class ImplicitBooleanessChecker(checkers.BaseChecker):
106106 }
107107
108108 options = ()
109+ _operators = {"!=" , "==" , "is not" , "is" }
109110
110111 @utils .only_required_for_messages ("use-implicit-booleaness-not-len" )
111112 def visit_call (self , node : nodes .Call ) -> None :
@@ -189,7 +190,6 @@ def visit_compare(self, node: nodes.Compare) -> None:
189190 self ._check_compare_to_str_or_zero (node )
190191
191192 def _check_compare_to_str_or_zero (self , node : nodes .Compare ) -> None :
192- _operators = {"!=" , "==" , "is not" , "is" }
193193 # note: astroid.Compare has the left most operand in node.left
194194 # while the rest are a list of tuples in node.ops
195195 # the format of the tuple is ('compare operator sign', node)
@@ -199,19 +199,21 @@ def _check_compare_to_str_or_zero(self, node: nodes.Compare) -> None:
199199 iter_ops = iter (ops )
200200 all_ops = list (itertools .chain (* iter_ops ))
201201 for ops_idx in range (len (all_ops ) - 2 ):
202- op_1 = all_ops [ops_idx ]
203202 op_2 = all_ops [ops_idx + 1 ]
203+ if op_2 not in self ._operators :
204+ continue
205+ op_1 = all_ops [ops_idx ]
204206 op_3 = all_ops [ops_idx + 2 ]
205207 error_detected = False
206208 if self .linter .is_message_enabled (
207209 "use-implicit-booleaness-not-comparison-to-zero"
208210 ):
209211 # 0 ?? X
210- if _is_constant_zero (op_1 ) and op_2 in _operators :
212+ if _is_constant_zero (op_1 ):
211213 error_detected = True
212214 op = op_3
213215 # X ?? 0
214- elif op_2 in _operators and _is_constant_zero (op_3 ):
216+ elif _is_constant_zero (op_3 ):
215217 error_detected = True
216218 op = op_1
217219 if error_detected :
@@ -231,7 +233,7 @@ def _check_compare_to_str_or_zero(self, node: nodes.Compare) -> None:
231233 if self .linter .is_message_enabled (
232234 "use-implicit-booleaness-not-comparison-to-str"
233235 ):
234- if op_1 is None or op_3 is None or op_2 not in _operators :
236+ if op_1 is None or op_3 is None :
235237 continue
236238 node_name = ""
237239 # x ?? ""
0 commit comments