Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions pylint/checkers/base/docstring_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ def _infer_dunder_doc_attribute(
return None

docstring = utils.safe_infer(docstring)
if not docstring:
return None
if not isinstance(docstring, nodes.Const):
return None
return str(docstring.value)
if isinstance(docstring, nodes.Const):
return str(docstring.value)
return None


class DocStringChecker(_BasicChecker):
Expand Down
2 changes: 1 addition & 1 deletion pylint/checkers/classes/class_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2215,7 +2215,7 @@ def _check_init(self, node: nodes.FunctionDef, klass_node: nodes.ClassDef) -> No
parents_with_called_inits: set[bases.UnboundMethod] = set()
for stmt in node.nodes_of_class(nodes.Call):
expr = stmt.func
if not isinstance(expr, nodes.Attribute) or expr.attrname != "__init__":
if not (isinstance(expr, nodes.Attribute) and expr.attrname == "__init__"):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think these changes here are a net improvement, but I'm curious if that's only my opinion or if others think so too.

Though it might be a stretch goal, we could think about if it makes sense to add a checker for these. Haven't looked into it if it would even be practical or if there are just too many conditions to check.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea was to write them just as I'd write the match case, e.g.

nodes.Attribute(attrname = "__init__")

--
If inline match expressions ever get added, this could be

if not (expr match nodes.Attribute(attrname = "__init__")): ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda like the final result more, but if the fix is not automated I'm not sure I'm going to bother.

continue
# skip the test if using super
match expr.expr:
Expand Down
4 changes: 1 addition & 3 deletions pylint/checkers/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,7 @@ def _check_format_string(self, node: nodes.Call, format_arg: Literal[0, 1]) -> N
def is_complex_format_str(node: nodes.NodeNG) -> bool:
"""Return whether the node represents a string with complex formatting specs."""
inferred = utils.safe_infer(node)
if inferred is None or not (
isinstance(inferred, nodes.Const) and isinstance(inferred.value, str)
):
if not (isinstance(inferred, nodes.Const) and isinstance(inferred.value, str)):
return True
try:
parsed = list(string.Formatter().parse(inferred.value))
Expand Down
12 changes: 6 additions & 6 deletions pylint/checkers/modified_iterating_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ def _modified_iterating_set_cond(
def _deleted_iteration_target_cond(
self, node: nodes.DelName, iter_obj: nodes.NodeNG
) -> bool:
if not isinstance(node, nodes.DelName):
return False
if not isinstance(iter_obj.parent, nodes.For):
return False
if not isinstance(
iter_obj.parent.target, (nodes.AssignName, nodes.BaseContainer)
if not (
isinstance(node, nodes.DelName)
and isinstance(iter_obj.parent, nodes.For)
and isinstance(
iter_obj.parent.target, (nodes.AssignName, nodes.BaseContainer)
)
):
return False
return any(
Expand Down
17 changes: 8 additions & 9 deletions pylint/checkers/refactoring/refactoring_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ def _check_stop_iteration_inside_generator(self, node: nodes.Raise) -> None:
if not node.exc:
return
exc = utils.safe_infer(node.exc)
if not exc or not isinstance(exc, (bases.Instance, nodes.ClassDef)):
if not isinstance(exc, (bases.Instance, nodes.ClassDef)):
return
if self._check_exception_inherit_from_stopiteration(exc):
self.add_message("stop-iteration-return", node=node, confidence=INFERENCE)
Expand Down Expand Up @@ -1712,7 +1712,7 @@ def _check_use_list_literal(self, node: nodes.Call) -> None:

def _check_use_dict_literal(self, node: nodes.Call) -> None:
"""Check if dict is created by using the literal {}."""
if not isinstance(node.func, astroid.Name) or node.func.name != "dict":
if not (isinstance(node.func, astroid.Name) and node.func.name == "dict"):
return
inferred = utils.safe_infer(node.func)
if (
Expand Down Expand Up @@ -1753,7 +1753,7 @@ def _name_to_concatenate(self, node: nodes.NodeNG) -> str | None:
values = [
value for value in node.values if isinstance(value, nodes.FormattedValue)
]
if len(values) != 1 or not isinstance(values[0].value, nodes.Name):
if not (len(values) == 1 and isinstance(values[0].value, nodes.Name)):
return None
# If there are more values in joined string than formatted values,
# they are probably separators.
Expand All @@ -1772,7 +1772,7 @@ def _check_consider_using_join(self, aug_assign: nodes.AugAssign) -> None:
result += number # aug_assign
"""
for_loop = aug_assign.parent
if not isinstance(for_loop, nodes.For) or len(for_loop.body) > 1:
if not (isinstance(for_loop, nodes.For) and len(for_loop.body) == 1):
return
assign = for_loop.previous_sibling()
if not isinstance(assign, nodes.Assign):
Expand Down Expand Up @@ -1810,11 +1810,10 @@ def visit_comprehension(self, node: nodes.Comprehension) -> None:
self._check_unnecessary_list_index_lookup(node)

def _check_unnecessary_comprehension(self, node: nodes.Comprehension) -> None:
if (
isinstance(node.parent, nodes.GeneratorExp)
or len(node.ifs) != 0
or len(node.parent.generators) != 1
or node.is_async
if isinstance(node.parent, nodes.GeneratorExp) or not (
len(node.ifs) == 0
and len(node.parent.generators) == 1
and node.is_async is False
):
return

Expand Down
15 changes: 6 additions & 9 deletions pylint/checkers/typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -2199,10 +2199,7 @@ def visit_subscript(self, node: nodes.Subscript) -> None:

@only_required_for_messages("dict-items-missing-iter")
def visit_for(self, node: nodes.For) -> None:
if not isinstance(node.target, nodes.Tuple):
# target is not a tuple
return
if not len(node.target.elts) == 2:
if not (isinstance(node.target, nodes.Tuple) and len(node.target.elts) == 2):
# target is not a tuple of two elements
return

Expand Down Expand Up @@ -2281,11 +2278,11 @@ def _is_asyncio_coroutine(node: nodes.NodeNG) -> bool:
return False
for decorator in inferred_func.decorators.nodes:
inferred_decorator = safe_infer(decorator)
if not isinstance(inferred_decorator, nodes.FunctionDef):
continue
if inferred_decorator.qname() != ASYNCIO_COROUTINE:
continue
return True
if (
isinstance(inferred_decorator, nodes.FunctionDef)
and inferred_decorator.qname() == ASYNCIO_COROUTINE
):
return True
return False

def _check_iterable(self, node: nodes.NodeNG, check_async: bool = False) -> None:
Expand Down
11 changes: 5 additions & 6 deletions pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ def is_registered_in_singledispatch_function(node: nodes.FunctionDef) -> bool:
case _:
continue

if not isinstance(func, nodes.Attribute) or func.attrname != "register":
if not (isinstance(func, nodes.Attribute) and func.attrname == "register"):
continue

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

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

func_def = safe_infer(func.expr)
Expand Down Expand Up @@ -2169,10 +2169,9 @@ def is_terminating_func(node: nodes.Call) -> bool:
"""Detect call to exit(), quit(), os._exit(), sys.exit(), or
functions annotated with `typing.NoReturn` or `typing.Never`.
"""
if (
not isinstance(node.func, nodes.Attribute)
and not (isinstance(node.func, nodes.Name))
) or isinstance(node.parent, nodes.Lambda):
if not isinstance(node.func, (nodes.Attribute, nodes.Name)) or isinstance(
node.parent, nodes.Lambda
):
return False

try:
Expand Down
9 changes: 5 additions & 4 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1687,7 +1687,7 @@ def visit_name(self, node: nodes.Name | nodes.AssignName | nodes.DelName) -> Non

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

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

defstmt_frame = defstmt.frame()
Expand Down Expand Up @@ -3472,8 +3472,9 @@ def _check_potential_index_error(
) -> None:
"""Check for the potential-index-error message."""
# Currently we only check simple slices of a single integer
if not isinstance(inferred_slice, nodes.Const) or not isinstance(
inferred_slice.value, int
if not (
isinstance(inferred_slice, nodes.Const)
and isinstance(inferred_slice.value, int)
):
return

Expand Down
8 changes: 3 additions & 5 deletions pylint/extensions/_check_docs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ def returns_something(return_node: nodes.Return) -> bool:
False otherwise.
"""
returns = return_node.value

if returns is None:
return False

return not (isinstance(returns, nodes.Const) and returns.value is None)
return not (
returns is None or (isinstance(returns, nodes.Const) and returns.value is None)
)


def _get_raise_target(node: nodes.NodeNG) -> nodes.NodeNG | UninferableBase | None:
Expand Down
9 changes: 4 additions & 5 deletions pylint/extensions/consider_ternary_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ def visit_if(self, node: nodes.If) -> None:
return

for bname, oname in zip(bst.targets, ost.targets):
if not isinstance(bname, nodes.AssignName) or not isinstance(
oname, nodes.AssignName
if not (
isinstance(bname, nodes.AssignName)
and isinstance(oname, nodes.AssignName)
and bname.name == oname.name
):
return

if bname.name != oname.name:
return

self.add_message("consider-ternary-expression", node=node)


Expand Down
Loading