@@ -634,7 +634,7 @@ def _check_simplifiable_if(self, node: nodes.If) -> None:
634
634
for target in else_branch .targets
635
635
if isinstance (target , nodes .AssignName )
636
636
]
637
- if not first_branch_targets or not else_branch_targets :
637
+ if not ( first_branch_targets and else_branch_targets ) :
638
638
return
639
639
if sorted (first_branch_targets ) != sorted (else_branch_targets ):
640
640
return
@@ -645,7 +645,7 @@ def _check_simplifiable_if(self, node: nodes.If) -> None:
645
645
case _:
646
646
return
647
647
648
- if not first_branch_is_bool or not else_branch_is_bool :
648
+ if not ( first_branch_is_bool and else_branch_is_bool ) :
649
649
return
650
650
if not first_branch .value .value :
651
651
# This is a case that can't be easily simplified and
@@ -1053,7 +1053,7 @@ def visit_raise(self, node: nodes.Raise) -> None:
1053
1053
def _check_stop_iteration_inside_generator (self , node : nodes .Raise ) -> None :
1054
1054
"""Check if an exception of type StopIteration is raised inside a generator."""
1055
1055
frame = node .frame ()
1056
- if not isinstance (frame , nodes .FunctionDef ) or not frame .is_generator ():
1056
+ if not ( isinstance (frame , nodes .FunctionDef ) and frame .is_generator () ):
1057
1057
return
1058
1058
if utils .node_ignores_exception (node , StopIteration ):
1059
1059
return
@@ -1319,11 +1319,11 @@ def _duplicated_isinstance_types(node: nodes.BoolOp) -> dict[str, set[str]]:
1319
1319
all_types : collections .defaultdict [str , set [str ]] = collections .defaultdict (set )
1320
1320
1321
1321
for call in node .values :
1322
- if not isinstance (call , nodes .Call ) or len (call .args ) != 2 :
1322
+ if not ( isinstance (call , nodes .Call ) and len (call .args ) == 2 ) :
1323
1323
continue
1324
1324
1325
1325
inferred = utils .safe_infer (call .func )
1326
- if not inferred or not utils .is_builtin_object (inferred ):
1326
+ if not ( inferred and utils .is_builtin_object (inferred ) ):
1327
1327
continue
1328
1328
1329
1329
if inferred .name != "isinstance" :
@@ -1365,7 +1365,7 @@ def _check_consider_merging_isinstance(self, node: nodes.BoolOp) -> None:
1365
1365
def _check_consider_using_in (self , node : nodes .BoolOp ) -> None :
1366
1366
allowed_ops = {"or" : "==" , "and" : "!=" }
1367
1367
1368
- if node .op not in allowed_ops or len (node .values ) < 2 :
1368
+ if not ( node .op in allowed_ops and len (node .values ) >= 2 ) :
1369
1369
return
1370
1370
1371
1371
for value in node .values :
@@ -1416,7 +1416,7 @@ def _check_chained_comparison(self, node: nodes.BoolOp) -> None:
1416
1416
1417
1417
Care is taken to avoid simplifying a < b < c and b < d.
1418
1418
"""
1419
- if node .op != "and" or len (node .values ) < 2 :
1419
+ if not ( node .op == "and" and len (node .values ) >= 2 ) :
1420
1420
return
1421
1421
1422
1422
def _find_lower_upper_bounds (
@@ -1566,7 +1566,7 @@ def _is_simple_assignment(node: nodes.NodeNG | None) -> bool:
1566
1566
return False
1567
1567
1568
1568
def _check_swap_variables (self , node : nodes .Return | nodes .Assign ) -> None :
1569
- if not node .next_sibling () or not node .next_sibling ().next_sibling ():
1569
+ if not ( node .next_sibling () and node .next_sibling ().next_sibling () ):
1570
1570
return
1571
1571
assignments = [node , node .next_sibling (), node .next_sibling ().next_sibling ()]
1572
1572
if not all (self ._is_simple_assignment (node ) for node in assignments ):
@@ -1643,10 +1643,10 @@ def _append_context_managers_to_stack(self, node: nodes.Assign) -> None:
1643
1643
if not isinstance (value , nodes .Call ):
1644
1644
continue
1645
1645
inferred = utils .safe_infer (value .func )
1646
- if (
1647
- not inferred
1648
- or inferred .qname () not in CALLS_RETURNING_CONTEXT_MANAGERS
1649
- or not isinstance (assignee , (nodes .AssignName , nodes .AssignAttr ))
1646
+ if not (
1647
+ inferred
1648
+ and inferred .qname () in CALLS_RETURNING_CONTEXT_MANAGERS
1649
+ and isinstance (assignee , (nodes .AssignName , nodes .AssignAttr ))
1650
1650
):
1651
1651
continue
1652
1652
stack = self ._consider_using_with_stack .get_stack_for_frame (node .frame ())
@@ -1685,8 +1685,11 @@ def _check_consider_using_with(self, node: nodes.Call) -> None:
1685
1685
# checked when leaving the scope.
1686
1686
return
1687
1687
inferred = utils .safe_infer (node .func )
1688
- if not inferred or not isinstance (
1689
- inferred , (nodes .FunctionDef , nodes .ClassDef , bases .BoundMethod )
1688
+ if not (
1689
+ inferred
1690
+ and isinstance (
1691
+ inferred , (nodes .FunctionDef , nodes .ClassDef , bases .BoundMethod )
1692
+ )
1690
1693
):
1691
1694
return
1692
1695
could_be_used_in_with = (
@@ -2178,12 +2181,12 @@ def _check_unnecessary_dict_index_lookup(
2178
2181
2179
2182
# Case where .items is assigned to k,v (i.e., for k, v in d.items())
2180
2183
if isinstance (value , nodes .Name ):
2181
- if (
2182
- not isinstance (node .target , nodes .Tuple )
2184
+ if not (
2185
+ isinstance (node .target , nodes .Tuple )
2183
2186
# Ignore 1-tuples: for k, in d.items()
2184
- or len (node .target .elts ) < 2
2185
- or value .name ! = node .target .elts [0 ].name
2186
- or iterating_object_name ! = subscript .value .as_string ()
2187
+ and len (node .target .elts ) >= 2
2188
+ and value .name = = node .target .elts [0 ].name
2189
+ and iterating_object_name = = subscript .value .as_string ()
2187
2190
):
2188
2191
continue
2189
2192
@@ -2213,11 +2216,11 @@ def _check_unnecessary_dict_index_lookup(
2213
2216
2214
2217
# Case where .items is assigned to single var (i.e., for item in d.items())
2215
2218
elif isinstance (value , nodes .Subscript ):
2216
- if (
2217
- not isinstance (node .target , nodes .AssignName )
2218
- or not isinstance (value .value , nodes .Name )
2219
- or node .target .name ! = value .value .name
2220
- or iterating_object_name ! = subscript .value .as_string ()
2219
+ if not (
2220
+ isinstance (node .target , nodes .AssignName )
2221
+ and isinstance (value .value , nodes .Name )
2222
+ and node .target .name = = value .value .name
2223
+ and iterating_object_name = = subscript .value .as_string ()
2221
2224
):
2222
2225
continue
2223
2226
@@ -2234,7 +2237,7 @@ def _check_unnecessary_dict_index_lookup(
2234
2237
2235
2238
# check if subscripted by 0 (key)
2236
2239
inferred = utils .safe_infer (value .slice )
2237
- if not isinstance (inferred , nodes .Const ) or inferred .value != 0 :
2240
+ if not ( isinstance (inferred , nodes .Const ) and inferred .value == 0 ) :
2238
2241
continue
2239
2242
2240
2243
if has_nested_loops :
@@ -2350,9 +2353,9 @@ def _check_unnecessary_list_index_lookup(
2350
2353
2351
2354
index = subscript .slice
2352
2355
if isinstance (index , nodes .Name ):
2353
- if (
2354
- index .name ! = name1
2355
- or iterating_object_name ! = subscript .value .as_string ()
2356
+ if not (
2357
+ index .name = = name1
2358
+ and iterating_object_name = = subscript .value .as_string ()
2356
2359
):
2357
2360
continue
2358
2361
0 commit comments