diff --git a/pylint/checkers/match_statements_checker.py b/pylint/checkers/match_statements_checker.py index 18fd8a726a..99132fa964 100644 --- a/pylint/checkers/match_statements_checker.py +++ b/pylint/checkers/match_statements_checker.py @@ -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, ) diff --git a/pylint/extensions/_check_docs_utils.py b/pylint/extensions/_check_docs_utils.py index 9b4b3e0db6..a6c378b476 100644 --- a/pylint/extensions/_check_docs_utils.py +++ b/pylint/extensions/_check_docs_utils.py @@ -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 { diff --git a/pylint/extensions/for_any_all.py b/pylint/extensions/for_any_all.py index 2369a595dc..44644e6c19 100644 --- a/pylint/extensions/for_any_all.py +++ b/pylint/extensions/for_any_all.py @@ -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})" diff --git a/pylint/extensions/private_import.py b/pylint/extensions/private_import.py index 962bfe1f1c..4d535aae3a 100644 --- a/pylint/extensions/private_import.py +++ b/pylint/extensions/private_import.py @@ -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( @@ -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