The aux, compressor and turn paths each grew "is this a <kind> error?" helpers that read an exception's status_code or grep its text; every copy drifted from classify_api_error until the previous commit moved the aux decisions onto the shared classifier. HX014 keeps new copies out. What it flags: a def or lambda with an exception parameter (exc/err/ error/e/ex/exception, *_err/*_error/*_exc, or an *Exception/*Error annotation; a ClassifiedError parameter is not one) whose own body reads <param>.status_code (also via getattr or .response) or probes the exception's text (`in str(exc)`, a local bound to that text, startswith/endswith, re.search/match/fullmatch, _contains_any). Scope: agent/auxiliary*.py, agent/context_compressor*.py, agent/turn_*.py, tests excluded. Like the other HX rules it ratchets base vs head, so existing hits are grandfathered. Waiver: `# health: allow HX014 -- <why>`. Tests: tests/scripts/test_code_health_error_predicate_rule.py covers flagged and passing shapes, scope and the base-vs-head verdict. Part of the Code Health plan.
43 lines
2.4 KiB
Python
43 lines
2.4 KiB
Python
"""HX014: a def/lambda deciding from an exception parameter's status code or text (``py_rules.CHECKERS``)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import ast
|
|
import re
|
|
from collections.abc import Iterable
|
|
|
|
from scripts.code_health.py_ast import _FUNCS, Ctx, _call_name
|
|
from scripts.code_health.py_rules_sleep import _own
|
|
|
|
_EXC_NAME = re.compile(r"(?:exc|err|error|e|ex|exception|\w+_(?:err|error|exc))$")
|
|
_EXC_TYPE = re.compile(r"(?!.*ClassifiedError)(?:(?:typing\.)?Optional\[)?[\w.]*(?:Exception|Error)\]?(?: \| None)?")
|
|
_PROBES = {"startswith", "endswith", "search", "match", "fullmatch", "_contains_any"}
|
|
|
|
|
|
def _exc_params(args: ast.arguments) -> str:
|
|
return "|".join(p.arg for p in (*args.posonlyargs, *args.args, *args.kwonlyargs)
|
|
if (_EXC_TYPE.fullmatch(ast.unparse(p.annotation).strip("'\"")) if p.annotation
|
|
else _EXC_NAME.match(p.arg)))
|
|
|
|
|
|
def private_error_predicate(tree: ast.Module, ctx: Ctx) -> Iterable[int]:
|
|
for func in ast.walk(tree):
|
|
if not isinstance(func, (*_FUNCS, ast.Lambda)) or not (exc := _exc_params(func.args)):
|
|
continue
|
|
own = rf"(?:{exc})(?:\.response)?|getattr\((?:{exc}), 'response'[^()]*\)"
|
|
status = re.compile(rf"(?:{own})\.status_code|getattr\((?:{own}), 'status_code'[^()]*\)")
|
|
nodes, texts = list(_own(func)), rf"(?:str|repr)\((?:{exc})\)"
|
|
for _ in range(4): # text locals, chained: msg = str(exc); low = msg.lower()
|
|
fold = re.compile(rf"(?:{texts})(?:\.(?:lower|upper|casefold|strip)\(\))*")
|
|
texts = "|".join({texts, *(t.id for n in nodes if isinstance(n, (ast.Assign, ast.NamedExpr))
|
|
for t in getattr(n, "targets", [getattr(n, "target", None)])
|
|
if isinstance(t, ast.Name) and fold.fullmatch(ast.unparse(n.value)))})
|
|
is_text = lambda n: n is not None and bool(fold.fullmatch(ast.unparse(n)))
|
|
for n in nodes:
|
|
if (isinstance(n, (ast.Attribute, ast.Call)) and status.fullmatch(ast.unparse(n))
|
|
or isinstance(n, ast.Compare) and any(isinstance(op, (ast.In, ast.NotIn)) and is_text(r)
|
|
for op, r in zip(n.ops, n.comparators))
|
|
or isinstance(n, ast.Call) and _call_name(n).rsplit(".", 1)[-1] in _PROBES
|
|
and any(map(is_text, (getattr(n.func, "value", None), *n.args)))):
|
|
yield n.lineno
|