Skip to content

Commit 923bd4c

Browse files
committed
Update pylint conditionals
1 parent 30e24c0 commit 923bd4c

18 files changed

+96
-87
lines changed

pylint/checkers/base/name_checker/checker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def _redefines_import(node: nodes.AssignName) -> bool:
9999
current = node
100100
while current and not isinstance(current.parent, nodes.ExceptHandler):
101101
current = current.parent
102-
if not current or not utils.error_of_type(current.parent, ImportError):
102+
if not (current and utils.error_of_type(current.parent, ImportError)):
103103
return False
104104
try_block = current.parent.parent
105105
for import_node in try_block.nodes_of_class((nodes.ImportFrom, nodes.Import)):
@@ -160,7 +160,7 @@ def _is_multi_naming_match(
160160
match is not None
161161
and match.lastgroup is not None
162162
and match.lastgroup not in EXEMPT_NAME_CATEGORIES
163-
and (node_type != "method" or confidence != interfaces.INFERENCE_FAILURE)
163+
and not (node_type == "method" and confidence == interfaces.INFERENCE_FAILURE)
164164
)
165165

166166

pylint/checkers/classes/class_checker.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,10 @@ def _is_trivial_super_delegation(function: nodes.FunctionDef) -> bool:
186186
# Should be a super call with the MRO pointer being the
187187
# current class and the type being the current instance.
188188
current_scope = function.parent.scope()
189-
if (
190-
super_call.mro_pointer != current_scope
191-
or not isinstance(super_call.type, astroid.Instance)
192-
or super_call.type.name != current_scope.name
189+
if not (
190+
super_call.mro_pointer == current_scope
191+
and isinstance(super_call.type, astroid.Instance)
192+
and super_call.type.name == current_scope.name
193193
):
194194
return False
195195

@@ -1139,8 +1139,9 @@ def _check_unused_private_variables(self, node: nodes.ClassDef) -> None:
11391139

11401140
def _check_unused_private_attributes(self, node: nodes.ClassDef) -> None:
11411141
for assign_attr in node.nodes_of_class(nodes.AssignAttr):
1142-
if not is_attr_private(assign_attr.attrname) or not isinstance(
1143-
assign_attr.expr, nodes.Name
1142+
if not (
1143+
is_attr_private(assign_attr.attrname)
1144+
and isinstance(assign_attr.expr, nodes.Name)
11441145
):
11451146
continue
11461147

@@ -1928,7 +1929,7 @@ def _check_protected_attribute_access(
19281929
callee = node.expr.as_string()
19291930
parents_callee = callee.split(".")
19301931
for callee in reversed(parents_callee):
1931-
if not outer_klass or callee != outer_klass.name:
1932+
if not (outer_klass and callee == outer_klass.name):
19321933
inside_klass = False
19331934
break
19341935

pylint/checkers/dataclass_checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def _check_invalid_field_call(self, node: nodes.Call) -> None:
8282
self._check_invalid_field_call_within_call(node, scope_node)
8383
return
8484

85-
if not scope_node or not scope_node.is_dataclass:
85+
if not (scope_node and scope_node.is_dataclass):
8686
self.add_message(
8787
"invalid-field-call",
8888
node=node,

pylint/checkers/design_analysis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,8 @@ def visit_if(self, node: nodes.If) -> None:
660660
self._check_boolean_expressions(node)
661661
branches = 1
662662
# don't double count If nodes coming from some 'elif'
663-
if node.orelse and (
664-
len(node.orelse) > 1 or not isinstance(node.orelse[0], nodes.If)
663+
if node.orelse and not (
664+
len(node.orelse) == 1 and isinstance(node.orelse[0], nodes.If)
665665
):
666666
branches += 1
667667
self._inc_branch(node, branches)

pylint/checkers/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def _check_misplaced_bare_raise(self, node: nodes.Raise) -> None:
348348
current = current.parent
349349

350350
expected = (nodes.ExceptHandler,)
351-
if not current or not isinstance(current.parent, expected):
351+
if not (current and isinstance(current.parent, expected)):
352352
self.add_message("misplaced-bare-raise", node=node, confidence=HIGH)
353353

354354
def _check_bad_exception_cause(self, node: nodes.Raise) -> None:

pylint/checkers/imports.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,10 +1172,13 @@ def _report_dependencies_graph(
11721172
) -> None:
11731173
"""Write dependencies as a dot (graphviz) file."""
11741174
dep_info = self.linter.stats.dependencies
1175-
if not dep_info or not (
1176-
self.linter.config.import_graph
1177-
or self.linter.config.ext_import_graph
1178-
or self.linter.config.int_import_graph
1175+
if not (
1176+
dep_info
1177+
and (
1178+
self.linter.config.import_graph
1179+
or self.linter.config.ext_import_graph
1180+
or self.linter.config.int_import_graph
1181+
)
11791182
):
11801183
raise EmptyReportError()
11811184
filename = self.linter.config.import_graph

pylint/checkers/refactoring/recommendation_checker.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,10 @@ def _check_consider_using_dict_items(self, node: nodes.For) -> None:
281281
continue
282282

283283
value = subscript.slice
284-
if (
285-
not isinstance(value, nodes.Name)
286-
or value.name != node.target.name
287-
or iterating_object_name != subscript.value.as_string()
284+
if not (
285+
isinstance(value, nodes.Name)
286+
and value.name == node.target.name
287+
and iterating_object_name == subscript.value.as_string()
288288
):
289289
continue
290290
last_definition_lineno = value.lookup(value.name)[1][-1].lineno
@@ -334,10 +334,10 @@ def _check_consider_using_dict_items_comprehension(
334334
continue
335335

336336
value = subscript.slice
337-
if (
338-
not isinstance(value, nodes.Name)
339-
or value.name != node.target.name
340-
or iterating_object_name != subscript.value.as_string()
337+
if not (
338+
isinstance(value, nodes.Name)
339+
and value.name == node.target.name
340+
and iterating_object_name == subscript.value.as_string()
341341
):
342342
continue
343343

@@ -428,8 +428,9 @@ def _detect_replacable_format_call(self, node: nodes.Const) -> None:
428428
return
429429

430430
# If % applied to another type than str, it's modulo and can't be replaced by formatting
431-
if not hasattr(node.parent.left, "value") or not isinstance(
432-
node.parent.left.value, str
431+
if not (
432+
hasattr(node.parent.left, "value")
433+
and isinstance(node.parent.left.value, str)
433434
):
434435
return
435436

pylint/checkers/refactoring/refactoring_checker.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ def _check_simplifiable_if(self, node: nodes.If) -> None:
634634
for target in else_branch.targets
635635
if isinstance(target, nodes.AssignName)
636636
]
637-
if not first_branch_targets or not else_branch_targets:
637+
if not (first_branch_targets and else_branch_targets):
638638
return
639639
if sorted(first_branch_targets) != sorted(else_branch_targets):
640640
return
@@ -645,7 +645,7 @@ def _check_simplifiable_if(self, node: nodes.If) -> None:
645645
case _:
646646
return
647647

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):
649649
return
650650
if not first_branch.value.value:
651651
# This is a case that can't be easily simplified and
@@ -1053,7 +1053,7 @@ def visit_raise(self, node: nodes.Raise) -> None:
10531053
def _check_stop_iteration_inside_generator(self, node: nodes.Raise) -> None:
10541054
"""Check if an exception of type StopIteration is raised inside a generator."""
10551055
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()):
10571057
return
10581058
if utils.node_ignores_exception(node, StopIteration):
10591059
return
@@ -1319,11 +1319,11 @@ def _duplicated_isinstance_types(node: nodes.BoolOp) -> dict[str, set[str]]:
13191319
all_types: collections.defaultdict[str, set[str]] = collections.defaultdict(set)
13201320

13211321
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):
13231323
continue
13241324

13251325
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)):
13271327
continue
13281328

13291329
if inferred.name != "isinstance":
@@ -1365,7 +1365,7 @@ def _check_consider_merging_isinstance(self, node: nodes.BoolOp) -> None:
13651365
def _check_consider_using_in(self, node: nodes.BoolOp) -> None:
13661366
allowed_ops = {"or": "==", "and": "!="}
13671367

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):
13691369
return
13701370

13711371
for value in node.values:
@@ -1416,7 +1416,7 @@ def _check_chained_comparison(self, node: nodes.BoolOp) -> None:
14161416
14171417
Care is taken to avoid simplifying a < b < c and b < d.
14181418
"""
1419-
if node.op != "and" or len(node.values) < 2:
1419+
if not (node.op == "and" and len(node.values) >= 2):
14201420
return
14211421

14221422
def _find_lower_upper_bounds(
@@ -1566,7 +1566,7 @@ def _is_simple_assignment(node: nodes.NodeNG | None) -> bool:
15661566
return False
15671567

15681568
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()):
15701570
return
15711571
assignments = [node, node.next_sibling(), node.next_sibling().next_sibling()]
15721572
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:
16431643
if not isinstance(value, nodes.Call):
16441644
continue
16451645
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))
16501650
):
16511651
continue
16521652
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:
16851685
# checked when leaving the scope.
16861686
return
16871687
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+
)
16901693
):
16911694
return
16921695
could_be_used_in_with = (
@@ -2178,12 +2181,12 @@ def _check_unnecessary_dict_index_lookup(
21782181

21792182
# Case where .items is assigned to k,v (i.e., for k, v in d.items())
21802183
if isinstance(value, nodes.Name):
2181-
if (
2182-
not isinstance(node.target, nodes.Tuple)
2184+
if not (
2185+
isinstance(node.target, nodes.Tuple)
21832186
# 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()
21872190
):
21882191
continue
21892192

@@ -2213,11 +2216,11 @@ def _check_unnecessary_dict_index_lookup(
22132216

22142217
# Case where .items is assigned to single var (i.e., for item in d.items())
22152218
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()
22212224
):
22222225
continue
22232226

@@ -2234,7 +2237,7 @@ def _check_unnecessary_dict_index_lookup(
22342237

22352238
# check if subscripted by 0 (key)
22362239
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):
22382241
continue
22392242

22402243
if has_nested_loops:
@@ -2350,9 +2353,9 @@ def _check_unnecessary_list_index_lookup(
23502353

23512354
index = subscript.slice
23522355
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()
23562359
):
23572360
continue
23582361

pylint/checkers/spelling.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,11 @@ def next(self) -> tuple[str, int]:
161161
pre_text, post_text = self._text.split("/", 1)
162162
self._text = post_text
163163
self._offset = 0
164-
if (
165-
not pre_text
166-
or not post_text
167-
or not pre_text[-1].isalpha()
168-
or not post_text[0].isalpha()
164+
if not (
165+
pre_text
166+
and post_text
167+
and pre_text[-1].isalpha()
168+
and post_text[0].isalpha()
169169
):
170170
self._text = ""
171171
self._offset = 0
@@ -177,9 +177,9 @@ def _next(self) -> tuple[str, Literal[0]]:
177177
if "/" not in self._text:
178178
return self._text, 0
179179
pre_text, post_text = self._text.split("/", 1)
180-
if not pre_text or not post_text:
180+
if not (pre_text and post_text):
181181
break
182-
if not pre_text[-1].isalpha() or not post_text[0].isalpha():
182+
if not (pre_text[-1].isalpha() and post_text[0].isalpha()):
183183
raise StopIteration()
184184
self._text = pre_text + " " + post_text
185185
raise StopIteration()

pylint/checkers/stdlib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ def _check_bad_thread_instantiation(self, node: nodes.Call) -> None:
641641
if "target" in func_kwargs:
642642
return
643643

644-
if len(node.args) < 2 and (not node.kwargs or "target" not in func_kwargs):
644+
if len(node.args) < 2 and not (node.kwargs and "target" in func_kwargs):
645645
self.add_message(
646646
"bad-thread-instantiation", node=node, confidence=interfaces.HIGH
647647
)
@@ -885,7 +885,7 @@ def _check_open_call(
885885

886886
if not mode_arg or (
887887
isinstance(mode_arg, nodes.Const)
888-
and (not mode_arg.value or "b" not in str(mode_arg.value))
888+
and not (mode_arg.value and "b" in str(mode_arg.value))
889889
):
890890
confidence = HIGH
891891
try:

0 commit comments

Comments
 (0)