shared
_shared
Shared AST primitives for test_quality rules.
Registry-neutral helpers ported from the detect_pyramid_level_v6 and
triage_tautologies_v4 prototypes. Rules in this subpackage import
from here; no @register_rule lives here.
analyze_imports(tree, pkg_prefixes, init_all, pkg_root)
Classify imports + collect IO module names.
Returns (public, internal, modules, has_private, io_module_names,
io_signals).
Source code in packages/axm-audit/src/axm_audit/core/rules/test_quality/_shared.py
collect_pkg_contract_classes(pkg_root)
Contract classes (Protocol/ABC/TypedDict) in pkg_root and sibling packages.
Source code in packages/axm-audit/src/axm_audit/core/rules/test_quality/_shared.py
collect_pkg_public_symbols(pkg_root)
Collect top-level public function, class, and constant names across src/.
Walks every *.py file under {pkg_root}/src and returns the union of
non-underscore names defined at module top level — functions, async
functions, classes, and simple / annotated assignments to Name targets.
Source code in packages/axm-audit/src/axm_audit/core/rules/test_quality/_shared.py
current_level_from_path(test_file, tests_dir)
Map test_file to its pyramid level based on its location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
test_file
|
Path
|
Test file path to classify. |
required |
tests_dir
|
Path
|
Root tests directory used to compute the relative path. |
required |
Returns:
| Type | Description |
|---|---|
str
|
One of |
str
|
the level cannot be inferred. |
str
|
|
Source code in packages/axm-audit/src/axm_audit/core/rules/test_quality/_shared.py
detect_real_io(tree)
File-scope I/O detection from a parsed test module.
Scans the module for three kinds of evidence that a test performs real I/O:
fixtures listed in _IO_FIXTURES consumed by test_* functions,
dotted calls listed in _IO_CALLS (e.g. subprocess.run,
pathlib.Path.write_text), and CLI runner invocations such as
CliRunner().invoke(...). Attribute-IO inside helpers is intentionally
out of scope — see :func:func_attr_io_transitive.
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
signal was found. |
list[str]
|
|
tuple[bool, bool, list[str]]
|
list of evidence strings ( |
tuple[bool, bool, list[str]]
|
|
Source code in packages/axm-audit/src/axm_audit/core/rules/test_quality/_shared.py
extract_mock_targets(func)
Collect dotted mock/patch targets + mock-factory:Mock/... markers.
Source code in packages/axm-audit/src/axm_audit/core/rules/test_quality/_shared.py
fixture_does_io(fix_name, fixtures, visited, depth)
True if fixture body does real I/O.
Walks the fixture body for attr-IO, _IO_CALLS matches and
transitive tmp_path deps; depth-bounded by _FIXTURE_DEPTH_LIMIT.
Source code in packages/axm-audit/src/axm_audit/core/rules/test_quality/_shared.py
func_attr_io_transitive(func, helpers, max_depth=2)
Attr-IO signals over the function subtree + transitively reachable helpers.
Source code in packages/axm-audit/src/axm_audit/core/rules/test_quality/_shared.py
get_init_all(pkg_root)
Read __all__ from the first src/<pkg>/__init__.py that defines it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pkg_root
|
Path
|
Repository root expected to follow the |
required |
Returns:
| Type | Description |
|---|---|
set[str] | None
|
Set of exported names, or |
set[str] | None
|
declares |
Source code in packages/axm-audit/src/axm_audit/core/rules/test_quality/_shared.py
get_module_all(pkg_root, dotted)
Resolve __all__ for dotted module within the src tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pkg_root
|
Path
|
Repository root expected to follow the |
required |
dotted
|
str
|
Dotted module path relative to |
required |
Returns:
| Type | Description |
|---|---|
set[str] | None
|
Set of exported names, or |
set[str] | None
|
not declare |
Source code in packages/axm-audit/src/axm_audit/core/rules/test_quality/_shared.py
get_pkg_prefixes(pkg_root)
Return top-level package directory names under <pkg_root>/src.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pkg_root
|
Path
|
Repository root expected to follow the |
required |
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of directory names directly under |
set[str]
|
entries. Empty when |
Source code in packages/axm-audit/src/axm_audit/core/rules/test_quality/_shared.py
is_import_smoke_test(func)
Detect an import + weak-assert smoke test.
Returns True when the function body (docstrings excluded, ≤ 4 stmts)
contains at least one import/from … import … statement and at
least one weak assertion (assert name, assert x is not None,
assert isinstance(...), assert callable(...), or
self.assertIsNotNone(...)), with no statement stronger than these.
Source code in packages/axm-audit/src/axm_audit/core/rules/test_quality/_shared.py
iter_test_files(pkg_root)
Yield (path, ast) for every tests/**/test_*.py.
Source code in packages/axm-audit/src/axm_audit/core/rules/test_quality/_shared.py
target_matches_io(target)
Return True when target references a known I/O call or module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str
|
Dotted call expression captured from the AST (e.g.
|
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
its dotted segments matches the package's I/O catalog; |
bool
|
otherwise (including for empty input). |
Source code in packages/axm-audit/src/axm_audit/core/rules/test_quality/_shared.py
test_is_in_lazy_import_context(func, tree_module, test_file)
Detect when the import itself is the system-under-test.
Returns True when the surrounding test file, class, or module
docstring signals that the test exists to verify lazy/optional
imports — in which case bare import statements inside the
test body must not be flagged as smoke tests.