Skip to content

Commit a60b85d

Browse files
Improve conditionals (#10581)
Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 4cea011 commit a60b85d

File tree

10 files changed

+42
-53
lines changed

10 files changed

+42
-53
lines changed

pylint/checkers/base/docstring_checker.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ def _infer_dunder_doc_attribute(
3535
return None
3636

3737
docstring = utils.safe_infer(docstring)
38-
if not docstring:
39-
return None
40-
if not isinstance(docstring, nodes.Const):
41-
return None
42-
return str(docstring.value)
38+
if isinstance(docstring, nodes.Const):
39+
return str(docstring.value)
40+
return None
4341

4442

4543
class DocStringChecker(_BasicChecker):

pylint/checkers/classes/class_checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2216,7 +2216,7 @@ def _check_init(self, node: nodes.FunctionDef, klass_node: nodes.ClassDef) -> No
22162216
parents_with_called_inits: set[bases.UnboundMethod] = set()
22172217
for stmt in node.nodes_of_class(nodes.Call):
22182218
expr = stmt.func
2219-
if not isinstance(expr, nodes.Attribute) or expr.attrname != "__init__":
2219+
if not (isinstance(expr, nodes.Attribute) and expr.attrname == "__init__"):
22202220
continue
22212221
# skip the test if using super
22222222
match expr.expr:

pylint/checkers/logging.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,7 @@ def _check_format_string(self, node: nodes.Call, format_arg: Literal[0, 1]) -> N
374374
def is_complex_format_str(node: nodes.NodeNG) -> bool:
375375
"""Return whether the node represents a string with complex formatting specs."""
376376
inferred = utils.safe_infer(node)
377-
if inferred is None or not (
378-
isinstance(inferred, nodes.Const) and isinstance(inferred.value, str)
379-
):
377+
if not (isinstance(inferred, nodes.Const) and isinstance(inferred.value, str)):
380378
return True
381379
try:
382380
parsed = list(string.Formatter().parse(inferred.value))

pylint/checkers/modified_iterating_checker.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,12 @@ def _modified_iterating_set_cond(
180180
def _deleted_iteration_target_cond(
181181
self, node: nodes.DelName, iter_obj: nodes.NodeNG
182182
) -> bool:
183-
if not isinstance(node, nodes.DelName):
184-
return False
185-
if not isinstance(iter_obj.parent, nodes.For):
186-
return False
187-
if not isinstance(
188-
iter_obj.parent.target, (nodes.AssignName, nodes.BaseContainer)
183+
if not (
184+
isinstance(node, nodes.DelName)
185+
and isinstance(iter_obj.parent, nodes.For)
186+
and isinstance(
187+
iter_obj.parent.target, (nodes.AssignName, nodes.BaseContainer)
188+
)
189189
):
190190
return False
191191
return any(

pylint/checkers/refactoring/refactoring_checker.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ def _check_stop_iteration_inside_generator(self, node: nodes.Raise) -> None:
10611061
if not node.exc:
10621062
return
10631063
exc = utils.safe_infer(node.exc)
1064-
if not exc or not isinstance(exc, (bases.Instance, nodes.ClassDef)):
1064+
if not isinstance(exc, (bases.Instance, nodes.ClassDef)):
10651065
return
10661066
if self._check_exception_inherit_from_stopiteration(exc):
10671067
self.add_message("stop-iteration-return", node=node, confidence=INFERENCE)
@@ -1712,7 +1712,7 @@ def _check_use_list_literal(self, node: nodes.Call) -> None:
17121712

17131713
def _check_use_dict_literal(self, node: nodes.Call) -> None:
17141714
"""Check if dict is created by using the literal {}."""
1715-
if not isinstance(node.func, astroid.Name) or node.func.name != "dict":
1715+
if not (isinstance(node.func, astroid.Name) and node.func.name == "dict"):
17161716
return
17171717
inferred = utils.safe_infer(node.func)
17181718
if (
@@ -1753,7 +1753,7 @@ def _name_to_concatenate(self, node: nodes.NodeNG) -> str | None:
17531753
values = [
17541754
value for value in node.values if isinstance(value, nodes.FormattedValue)
17551755
]
1756-
if len(values) != 1 or not isinstance(values[0].value, nodes.Name):
1756+
if not (len(values) == 1 and isinstance(values[0].value, nodes.Name)):
17571757
return None
17581758
# If there are more values in joined string than formatted values,
17591759
# they are probably separators.
@@ -1772,7 +1772,7 @@ def _check_consider_using_join(self, aug_assign: nodes.AugAssign) -> None:
17721772
result += number # aug_assign
17731773
"""
17741774
for_loop = aug_assign.parent
1775-
if not isinstance(for_loop, nodes.For) or len(for_loop.body) > 1:
1775+
if not (isinstance(for_loop, nodes.For) and len(for_loop.body) == 1):
17761776
return
17771777
assign = for_loop.previous_sibling()
17781778
if not isinstance(assign, nodes.Assign):
@@ -1810,11 +1810,10 @@ def visit_comprehension(self, node: nodes.Comprehension) -> None:
18101810
self._check_unnecessary_list_index_lookup(node)
18111811

18121812
def _check_unnecessary_comprehension(self, node: nodes.Comprehension) -> None:
1813-
if (
1814-
isinstance(node.parent, nodes.GeneratorExp)
1815-
or len(node.ifs) != 0
1816-
or len(node.parent.generators) != 1
1817-
or node.is_async
1813+
if isinstance(node.parent, nodes.GeneratorExp) or not (
1814+
len(node.ifs) == 0
1815+
and len(node.parent.generators) == 1
1816+
and node.is_async is False
18181817
):
18191818
return
18201819

pylint/checkers/typecheck.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,10 +2198,7 @@ def visit_subscript(self, node: nodes.Subscript) -> None:
21982198

21992199
@only_required_for_messages("dict-items-missing-iter")
22002200
def visit_for(self, node: nodes.For) -> None:
2201-
if not isinstance(node.target, nodes.Tuple):
2202-
# target is not a tuple
2203-
return
2204-
if not len(node.target.elts) == 2:
2201+
if not (isinstance(node.target, nodes.Tuple) and len(node.target.elts) == 2):
22052202
# target is not a tuple of two elements
22062203
return
22072204

@@ -2280,11 +2277,11 @@ def _is_asyncio_coroutine(node: nodes.NodeNG) -> bool:
22802277
return False
22812278
for decorator in inferred_func.decorators.nodes:
22822279
inferred_decorator = safe_infer(decorator)
2283-
if not isinstance(inferred_decorator, nodes.FunctionDef):
2284-
continue
2285-
if inferred_decorator.qname() != ASYNCIO_COROUTINE:
2286-
continue
2287-
return True
2280+
if (
2281+
isinstance(inferred_decorator, nodes.FunctionDef)
2282+
and inferred_decorator.qname() == ASYNCIO_COROUTINE
2283+
):
2284+
return True
22882285
return False
22892286

22902287
def _check_iterable(self, node: nodes.NodeNG, check_async: bool = False) -> None:

pylint/checkers/utils.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ def is_registered_in_singledispatch_function(node: nodes.FunctionDef) -> bool:
15261526
case _:
15271527
continue
15281528

1529-
if not isinstance(func, nodes.Attribute) or func.attrname != "register":
1529+
if not (isinstance(func, nodes.Attribute) and func.attrname == "register"):
15301530
continue
15311531

15321532
try:
@@ -1549,7 +1549,7 @@ def find_inferred_fn_from_register(node: nodes.NodeNG) -> nodes.FunctionDef | No
15491549
case _:
15501550
return None
15511551

1552-
if not isinstance(func, nodes.Attribute) or func.attrname != "register":
1552+
if not (isinstance(func, nodes.Attribute) and func.attrname == "register"):
15531553
return None
15541554

15551555
func_def = safe_infer(func.expr)
@@ -2168,10 +2168,9 @@ def is_terminating_func(node: nodes.Call) -> bool:
21682168
"""Detect call to exit(), quit(), os._exit(), sys.exit(), or
21692169
functions annotated with `typing.NoReturn` or `typing.Never`.
21702170
"""
2171-
if (
2172-
not isinstance(node.func, nodes.Attribute)
2173-
and not (isinstance(node.func, nodes.Name))
2174-
) or isinstance(node.parent, nodes.Lambda):
2171+
if not isinstance(node.func, (nodes.Attribute, nodes.Name)) or isinstance(
2172+
node.parent, nodes.Lambda
2173+
):
21752174
return False
21762175

21772176
try:

pylint/checkers/variables.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,7 +1687,7 @@ def visit_name(self, node: nodes.Name | nodes.AssignName | nodes.DelName) -> Non
16871687

16881688
@utils.only_required_for_messages("redefined-outer-name")
16891689
def visit_excepthandler(self, node: nodes.ExceptHandler) -> None:
1690-
if not node.name or not isinstance(node.name, nodes.AssignName):
1690+
if not isinstance(node.name, nodes.AssignName):
16911691
return
16921692

16931693
for outer_except, outer_except_assign_name in self._except_handler_names_queue:
@@ -2481,7 +2481,7 @@ def _is_only_type_assignment(
24812481
defstmt: _base_nodes.Statement,
24822482
) -> bool:
24832483
"""Check if variable only gets assigned a type and never a value."""
2484-
if not isinstance(defstmt, nodes.AnnAssign) or defstmt.value:
2484+
if not (isinstance(defstmt, nodes.AnnAssign) and defstmt.value is None):
24852485
return False
24862486

24872487
defstmt_frame = defstmt.frame()
@@ -3472,8 +3472,9 @@ def _check_potential_index_error(
34723472
) -> None:
34733473
"""Check for the potential-index-error message."""
34743474
# Currently we only check simple slices of a single integer
3475-
if not isinstance(inferred_slice, nodes.Const) or not isinstance(
3476-
inferred_slice.value, int
3475+
if not (
3476+
isinstance(inferred_slice, nodes.Const)
3477+
and isinstance(inferred_slice.value, int)
34773478
):
34783479
return
34793480

pylint/extensions/_check_docs_utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,9 @@ def returns_something(return_node: nodes.Return) -> bool:
8181
False otherwise.
8282
"""
8383
returns = return_node.value
84-
85-
if returns is None:
86-
return False
87-
88-
return not (isinstance(returns, nodes.Const) and returns.value is None)
84+
return not (
85+
returns is None or (isinstance(returns, nodes.Const) and returns.value is None)
86+
)
8987

9088

9189
def _get_raise_target(node: nodes.NodeNG) -> nodes.NodeNG | UninferableBase | None:

pylint/extensions/consider_ternary_expression.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,13 @@ def visit_if(self, node: nodes.If) -> None:
3838
return
3939

4040
for bname, oname in zip(bst.targets, ost.targets):
41-
if not isinstance(bname, nodes.AssignName) or not isinstance(
42-
oname, nodes.AssignName
41+
if not (
42+
isinstance(bname, nodes.AssignName)
43+
and isinstance(oname, nodes.AssignName)
44+
and bname.name == oname.name
4345
):
4446
return
4547

46-
if bname.name != oname.name:
47-
return
48-
4948
self.add_message("consider-ternary-expression", node=node)
5049

5150

0 commit comments

Comments
 (0)