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
4 changes: 2 additions & 2 deletions pylint/checkers/match_statements_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ def visit_match(self, node: nodes.Match) -> None:
"""
for idx, case in enumerate(node.cases):
match case.pattern:
case nodes.MatchAs(pattern=None, name=nodes.AssignName()) if (
case nodes.MatchAs(pattern=None, name=nodes.AssignName(name=name)) if (
idx < len(node.cases) - 1
):
self.add_message(
"bare-name-capture-pattern",
node=case.pattern,
args=case.pattern.name.name,
args=name,
confidence=HIGH,
)

Expand Down
35 changes: 18 additions & 17 deletions pylint/extensions/_check_docs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,24 @@ def possible_exc_types(node: nodes.NodeNG) -> set[nodes.ClassDef]:
except astroid.InferenceError:
pass
else:
target = _get_raise_target(node)
if isinstance(target, nodes.ClassDef):
exceptions = [target]
elif isinstance(target, nodes.FunctionDef):
for ret in target.nodes_of_class(nodes.Return):
if ret.value is None:
continue
if ret.frame() != target:
# return from inner function - ignore it
continue

val = utils.safe_infer(ret.value)
if val and utils.inherit_from_std_ex(val):
if isinstance(val, nodes.ClassDef):
exceptions.append(val)
elif isinstance(val, astroid.Instance):
exceptions.append(val.getattr("__class__")[0])
match target := _get_raise_target(node):
case nodes.ClassDef():
exceptions = [target]
case nodes.FunctionDef():
for ret in target.nodes_of_class(nodes.Return):
if ret.value is None:
continue
if ret.frame() != target:
# return from inner function - ignore it
continue

val = utils.safe_infer(ret.value)
if val and utils.inherit_from_std_ex(val):
match val:
case nodes.ClassDef():
exceptions.append(val)
case astroid.Instance():
exceptions.append(val.getattr("__class__")[0])

try:
return {
Expand Down
13 changes: 7 additions & 6 deletions pylint/extensions/for_any_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,13 @@ def _build_suggested_string(node: nodes.For, final_return_bool: bool) -> str:
loop_iter = node.iter.as_string()
test_node = next(node.body[0].get_children())

if isinstance(test_node, nodes.UnaryOp) and test_node.op == "not":
# The condition is negated. Advance the node to the operand and modify the suggestion
test_node = test_node.operand
suggested_function = "all" if final_return_bool else "not all"
else:
suggested_function = "not any" if final_return_bool else "any"
match test_node:
case nodes.UnaryOp(op="not"):
# The condition is negated. Advance the node to the operand and modify the suggestion
test_node = test_node.operand
suggested_function = "all" if final_return_bool else "not all"
case _:
suggested_function = "not any" if final_return_bool else "any"

test = test_node.as_string()
return f"{suggested_function}({test} for {loop_var} in {loop_iter})"
Expand Down
54 changes: 28 additions & 26 deletions pylint/extensions/private_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,15 @@ def _populate_type_annotations(
if isinstance(usage_node, nodes.AssignName) and isinstance(
usage_node.parent, (nodes.AnnAssign, nodes.Assign)
):
assign_parent = usage_node.parent
if isinstance(assign_parent, nodes.AnnAssign):
name_assignments.append(assign_parent)
private_name = self._populate_type_annotations_annotation(
usage_node.parent.annotation, all_used_type_annotations
)
elif isinstance(assign_parent, nodes.Assign):
name_assignments.append(assign_parent)
match assign_parent := usage_node.parent:
case nodes.AnnAssign():
name_assignments.append(assign_parent)
private_name = self._populate_type_annotations_annotation(
assign_parent.annotation,
all_used_type_annotations,
)
case nodes.Assign():
name_assignments.append(assign_parent)

if isinstance(usage_node, nodes.FunctionDef):
self._populate_type_annotations_function(
Expand Down Expand Up @@ -194,24 +195,25 @@ def _populate_type_annotations_annotation(
"""Handles the possibility of an annotation either being a Name, i.e. just type,
or a Subscript e.g. `Optional[type]` or an Attribute, e.g. `pylint.lint.linter`.
"""
if isinstance(node, nodes.Name) and node.name not in all_used_type_annotations:
all_used_type_annotations[node.name] = True
return node.name # type: ignore[no-any-return]
if isinstance(node, nodes.Subscript): # e.g. Optional[List[str]]
# slice is the next nested type
self._populate_type_annotations_annotation(
node.slice, all_used_type_annotations
)
# value is the current type name: could be a Name or Attribute
return self._populate_type_annotations_annotation(
node.value, all_used_type_annotations
)
if isinstance(node, nodes.Attribute):
# An attribute is a type like `pylint.lint.pylinter`. node.expr is the next level
# up, could be another attribute
return self._populate_type_annotations_annotation(
node.expr, all_used_type_annotations
)
match node:
case nodes.Name() if node.name not in all_used_type_annotations:
all_used_type_annotations[node.name] = True
return node.name # type: ignore[no-any-return]
case nodes.Subscript(): # e.g. Optional[List[str]]
# slice is the next nested type
self._populate_type_annotations_annotation(
node.slice, all_used_type_annotations
)
# value is the current type name: could be a Name or Attribute
return self._populate_type_annotations_annotation(
node.value, all_used_type_annotations
)
case nodes.Attribute():
# An attribute is a type like `pylint.lint.pylinter`. node.expr is the next level
# up, could be another attribute
return self._populate_type_annotations_annotation(
node.expr, all_used_type_annotations
)
return None

@staticmethod
Expand Down
Loading