|
| 1 | +"""foo.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import ast |
| 6 | +from typing import TYPE_CHECKING, Any |
| 7 | + |
| 8 | +from .flake8asyncvisitor import Flake8AsyncVisitor |
| 9 | +from .helpers import error_class |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from collections.abc import Mapping |
| 13 | + |
| 14 | + |
| 15 | +@error_class |
| 16 | +class Visitor123(Flake8AsyncVisitor): |
| 17 | + error_codes: Mapping[str, str] = { |
| 18 | + "ASYNC123": ( |
| 19 | + "Raising a child exception of an exception group loses" |
| 20 | + " context, cause, and/or traceback of the exception inside the group." |
| 21 | + ) |
| 22 | + } |
| 23 | + |
| 24 | + def __init__(self, *args: Any, **kwargs: Any): |
| 25 | + super().__init__(*args, **kwargs) |
| 26 | + self.try_star = False |
| 27 | + self.exception_group_names: set[str] = set() |
| 28 | + self.child_exception_list_names: set[str] = set() |
| 29 | + self.child_exception_names: set[str] = set() |
| 30 | + |
| 31 | + def _is_exception_group(self, node: ast.expr) -> bool: |
| 32 | + return ( |
| 33 | + (isinstance(node, ast.Name) and node.id in self.exception_group_names) |
| 34 | + or ( |
| 35 | + # a child exception might be an ExceptionGroup |
| 36 | + self._is_child_exception(node) |
| 37 | + ) |
| 38 | + or ( |
| 39 | + isinstance(node, ast.Call) |
| 40 | + and isinstance(node.func, ast.Attribute) |
| 41 | + and self._is_exception_group(node.func.value) |
| 42 | + and node.func.attr in ("subgroup", "split") |
| 43 | + ) |
| 44 | + ) |
| 45 | + |
| 46 | + def _is_exception_list(self, node: ast.expr | None) -> bool: |
| 47 | + return ( |
| 48 | + isinstance(node, ast.Name) and node.id in self.child_exception_list_names |
| 49 | + ) or ( |
| 50 | + isinstance(node, ast.Attribute) |
| 51 | + and node.attr == "exceptions" |
| 52 | + and self._is_exception_group(node.value) |
| 53 | + ) |
| 54 | + |
| 55 | + def _is_child_exception(self, node: ast.expr | None) -> bool: |
| 56 | + return ( |
| 57 | + isinstance(node, ast.Name) and node.id in self.child_exception_names |
| 58 | + ) or (isinstance(node, ast.Subscript) and self._is_exception_list(node.value)) |
| 59 | + |
| 60 | + def visit_Raise(self, node: ast.Raise): |
| 61 | + if self._is_child_exception(node.exc): |
| 62 | + self.error(node) |
| 63 | + |
| 64 | + def visit_ExceptHandler(self, node: ast.ExceptHandler): |
| 65 | + self.save_state( |
| 66 | + node, |
| 67 | + "exception_group_names", |
| 68 | + "child_exception_list_names", |
| 69 | + "child_exception_names", |
| 70 | + copy=True, |
| 71 | + ) |
| 72 | + if node.name is None or ( |
| 73 | + not self.try_star |
| 74 | + and (node.type is None or "ExceptionGroup" not in ast.unparse(node.type)) |
| 75 | + ): |
| 76 | + self.novisit = True |
| 77 | + return |
| 78 | + self.exception_group_names = {node.name} |
| 79 | + |
| 80 | + # ast.TryStar added in py311 |
| 81 | + # we run strict codecov on all python versions, this one doesn't run on <py311 |
| 82 | + def visit_TryStar(self, node: ast.TryStar): # type: ignore[name-defined] # pragma: no cover |
| 83 | + self.save_state(node, "try_star", copy=False) |
| 84 | + self.try_star = True |
| 85 | + |
| 86 | + def visit_Assign(self, node: ast.Assign | ast.AnnAssign): |
| 87 | + if node.value is None or not self.exception_group_names: |
| 88 | + return |
| 89 | + targets = (node.target,) if isinstance(node, ast.AnnAssign) else node.targets |
| 90 | + if self._is_child_exception(node.value): |
| 91 | + # not normally possible to assign single exception to multiple targets |
| 92 | + if len(targets) == 1 and isinstance(targets[0], ast.Name): |
| 93 | + self.child_exception_names.add(targets[0].id) |
| 94 | + elif self._is_exception_list(node.value): |
| 95 | + if len(targets) == 1 and isinstance(targets[0], ast.Name): |
| 96 | + self.child_exception_list_names.add(targets[0].id) |
| 97 | + # unpacking tuples and Starred and shit. Not implemented |
| 98 | + elif self._is_exception_group(node.value): |
| 99 | + for target in targets: |
| 100 | + if isinstance(target, ast.Name): |
| 101 | + self.exception_group_names.add(target.id) |
| 102 | + elif isinstance(target, ast.Tuple): |
| 103 | + for t in target.elts: |
| 104 | + if isinstance(t, ast.Name): |
| 105 | + self.exception_group_names.add(t.id) |
| 106 | + |
| 107 | + visit_AnnAssign = visit_Assign |
| 108 | + |
| 109 | + def visit_For(self, node: ast.For): |
| 110 | + if self._is_exception_list(node.iter) and isinstance(node.target, ast.Name): |
| 111 | + self.child_exception_names.add(node.target.id) |
0 commit comments