Skip to content

Commit 162c3ec

Browse files
Add AST import-helper import boundary architecture guard
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent cb81d51 commit 162c3ec

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ This runs lint, format checks, compile checks, tests, and package build.
9191
- `tests/test_ast_function_source_helper_usage.py` (shared AST function-source helper usage enforcement across architecture guard suites),
9292
- `tests/test_ast_function_source_import_boundary.py` (shared AST function-source helper import boundary enforcement across test modules),
9393
- `tests/test_ast_function_source_utils.py` (shared AST function-source helper contract validation),
94+
- `tests/test_ast_import_helper_import_boundary.py` (shared AST import-helper import boundary enforcement across test modules),
9495
- `tests/test_ast_import_helper_usage.py` (shared AST import-helper usage enforcement across AST import-boundary guard suites),
9596
- `tests/test_binary_file_open_helper_usage.py` (shared binary file open helper usage enforcement),
9697
- `tests/test_browser_use_payload_helper_usage.py` (browser-use payload helper usage enforcement),

tests/test_architecture_marker_usage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"tests/test_ast_function_source_helper_usage.py",
2121
"tests/test_ast_function_source_import_boundary.py",
2222
"tests/test_ast_function_source_utils.py",
23+
"tests/test_ast_import_helper_import_boundary.py",
2324
"tests/test_ast_import_helper_usage.py",
2425
"tests/test_guardrail_ast_utils.py",
2526
"tests/test_helper_transport_usage_boundary.py",
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import ast
2+
from pathlib import Path
3+
4+
import pytest
5+
6+
pytestmark = pytest.mark.architecture
7+
8+
9+
EXPECTED_AST_IMPORT_HELPER_IMPORTERS = (
10+
"tests/test_ast_function_source_helper_usage.py",
11+
"tests/test_ast_function_source_import_boundary.py",
12+
)
13+
14+
15+
def _imports_ast_import_helper(module_text: str) -> bool:
16+
module_ast = ast.parse(module_text)
17+
for node in module_ast.body:
18+
if not isinstance(node, ast.ImportFrom):
19+
continue
20+
if node.module != "tests.ast_import_utils":
21+
continue
22+
if any(alias.name == "imports_collect_function_sources" for alias in node.names):
23+
return True
24+
return False
25+
26+
27+
def test_ast_import_helper_imports_are_centralized():
28+
discovered_modules: list[str] = []
29+
for module_path in sorted(Path("tests").glob("test_*.py")):
30+
module_text = module_path.read_text(encoding="utf-8")
31+
if not _imports_ast_import_helper(module_text):
32+
continue
33+
discovered_modules.append(module_path.as_posix())
34+
35+
assert discovered_modules == list(EXPECTED_AST_IMPORT_HELPER_IMPORTERS)

0 commit comments

Comments
 (0)