Implement census analysis: read, count, summarise, write and CLI

Closes #11
Closes #12
Closes #13
Closes #14
Closes #15

Task: MIL-002#4
Task: MIL-002#5
Task: MIL-002#6
Task: MIL-002#7
Task: MIL-002#8
This commit is contained in:
2026-10-13 15:37:51 +08:00
parent 259d017ad2
commit 7459711243
9 changed files with 364 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
"""Every module of the package imports only the standard library or itself."""
import ast
import pathlib
import sys
import squirrel
PACKAGE_NAME = squirrel.__name__
PACKAGE_DIR = pathlib.Path(squirrel.__file__).parent
ALLOWED = sys.stdlib_module_names | {PACKAGE_NAME}
def _imported_top_level_names(source: str) -> set[str]:
imported: set[str] = set()
for node in ast.walk(ast.parse(source)):
if isinstance(node, ast.Import):
imported.update(alias.name.split(".")[0] for alias in node.names)
elif (
isinstance(node, ast.ImportFrom)
and node.module is not None
and node.level == 0
):
imported.add(node.module.split(".")[0])
return imported
def test_package_has_modules_to_check() -> None:
assert list(PACKAGE_DIR.glob("*.py"))
def test_every_module_imports_only_standard_library_or_squirrel() -> None:
offenders: dict[str, set[str]] = {}
for module in PACKAGE_DIR.rglob("*.py"):
source = module.read_text(encoding="utf-8")
foreign = _imported_top_level_names(source) - ALLOWED
if foreign:
offenders[module.name] = foreign
assert offenders == {}