The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(compat-audit): treat with suppress(Exception) as an import guard
teknium1f43b70f25d8e
diff --git a/scripts/audit-old-updater-imports.py b/scripts/audit-old-updater-imports.pyindex 9344a47080c..215a1d77c2c 100644--- a/scripts/audit-old-updater-imports.py+++ b/scripts/audit-old-updater-imports.py@@ -257,6 +257,18 @@ def _string_constants(tree: ast.AST) -> dict[str, list[str]]:return out+def _suppresses(expr: ast.expr, caught_by: set[str]) -> bool:+ """``suppress(...)`` / ``contextlib.suppress(...)`` naming an exception in *caught_by*."""+ if not isinstance(expr, ast.Call):+ return False+ func = expr.func+ name = func.id if isinstance(func, ast.Name) else func.attr if isinstance(func, ast.Attribute) else None+ return name == "suppress" and any(+ (arg.id if isinstance(arg, ast.Name) else arg.attr if isinstance(arg, ast.Attribute) else None) in caught_by+ for arg in expr.args+ )++def _guarded_spans(func: _AnyFunc, failure: str = "ImportError") -> list[tuple[int, int]]:"""Try bodies whose first matching handler swallows this load failure.@@ -269,6 +281,11 @@ def _guarded_spans(func: _AnyFunc, failure: str = "ImportError") -> list[tuple[icaught_by.add("ImportError")spans: list[tuple[int, int]] = []for node in ast.walk(func):+ # ``with contextlib.suppress(Exception):`` swallows the failure exactly like a+ # non-raising ``except`` arm does.+ if isinstance(node, ast.With) and node.body and any(_suppresses(item.context_expr, caught_by) for item in node.items):+ spans.append((node.body[0].lineno, max(stmt.end_lineno or stmt.lineno for stmt in node.body)))+ continueif not isinstance(node, ast.Try):continuefor handler in node.handlers:diff --git a/tests/test_audit_old_updater_imports.py b/tests/test_audit_old_updater_imports.pyindex c2aa3ff0ad7..b2129e42205 100644--- a/tests/test_audit_old_updater_imports.py+++ b/tests/test_audit_old_updater_imports.py@@ -273,6 +273,26 @@ def test_only_swallowed_matching_load_failures_are_guarded(audit):assert {("hermes_constants", s) for s in ("wrong_exception", "name_not_module", "reraised")} <= surface.required.keys()+def test_suppress_context_guards_like_a_swallowing_except(audit):+ root = audit.REPO_ROOT+ put(root, "hermes_cli/update_cmd.py", """import contextlib+from contextlib import suppress+++def cmd_update():+ with suppress(Exception):+ from hermes_constants import via_suppress+ with contextlib.suppress(ImportError):+ from hermes_constants import via_qualified_suppress+ with suppress(AttributeError):+ from hermes_constants import wrong_suppress+""")+ commit(root, "suppress boundaries")+ surface = audit.audit_history()+ assert {("hermes_constants", "via_suppress"), ("hermes_constants", "via_qualified_suppress")} <= surface.guarded_only+ assert ("hermes_constants", "wrong_suppress") in surface.required.keys()++def test_lazy_module_facade_follows_called_helper_not_unrelated_commands(audit):root = audit.REPO_ROOTput(root, "hermes_cli/entry.py", """def _m():