Sub topic 11
Build a review checklist and CI guardrails
This chapter assumes you can read a Python project, run pytest, and edit a CI configuration file. It also assumes you know the checks from earlier chapters: introspection, versioned docs, type checkers, deprecation warnings, tests, and dependency verification. Here you combine them into one routine. A person follows part of it. The CI server enforces the rest.
Why a checklist and CI both matter
A checklist alone depends on reviewers remembering it. CI alone only catches what a machine can test. Split the work by what each does well:
- CI handles mechanical checks that give a clear pass or fail.
- The reviewer handles judgment: whether the library is the right one, whether the behavior matches the docs, and whether the tests assert anything meaningful.
The review checklist
Use this list for any change that includes AI-generated code. Keep it short enough that people actually use it.
- Does every imported package exist, and was it added on purpose? Check the name for typos and look-alikes.
- Are new dependencies pinned in the lockfile or requirements file?
- Does every called function or method exist in the installed version? If you are unsure, check with
inspect.signatureor theresolvehelper. - Does the behavior match the documentation for the version in use, not the latest version?
- Does the change add or update tests that call the new code path?
- Does the test run show no deprecation warnings from the new code?
Automate the import check
Type checkers catch many missing attributes. A small script adds a second layer for names that are imported directly. It parses each file, then confirms every absolute import and from ... import resolves.
import ast
import importlib
import sys
from pathlib import Path
def check_file(path):
problems = []
tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
try:
importlib.import_module(alias.name)
except ImportError:
problems.append(f"{path}:{node.lineno}: cannot import {alias.name}")
elif isinstance(node, ast.ImportFrom) and node.level == 0 and node.module:
try:
module = importlib.import_module(node.module)
except ImportError:
problems.append(f"{path}:{node.lineno}: cannot import {node.module}")
continue
for alias in node.names:
if alias.name == "*" or hasattr(module, alias.name):
continue
try:
importlib.import_module(f"{node.module}.{alias.name}")
except ImportError:
problems.append(
f"{path}:{node.lineno}: {node.module} has no name {alias.name}"
)
return problems
def main(roots):
sys.path.insert(0, ".")
problems = []
for root in roots:
for path in sorted(Path(root).rglob("*.py")):
problems.extend(check_file(path))
for line in problems:
print(line)
return 1 if problems else 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:] or ["."]))Save it as tools/check_imports.py. Run it with python tools/check_imports.py src. It exits with status 1 when any import fails, which makes the CI step fail.
Wire the checks into CI
This GitHub Actions workflow runs the checks in order. The last step uses the -W option from the deprecation chapter, so a deprecated call fails the build.
name: ai-code-guardrails
on: pull_request
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install pinned dependencies
run: |
pip install -r requirements.txt
pip install ruff mypy pytest
- name: Lint
run: ruff check .
- name: Type check
run: mypy src
- name: Check imports resolve
run: python tools/check_imports.py src
- name: Tests with deprecations as errors
run: python -W error::DeprecationWarning -m pytestKeep the guardrails honest
- Run the workflow on every pull request, not only on branches labeled as AI-assisted. Authors do not always know which suggestions came from a model.
- Make the checks required for merging. An optional check gets skipped under deadline pressure.
- When a bad API call reaches review, add the check that would have caught it. The checklist should grow from real misses.
Exercise
A pull request adds import fastjsonx and a test that mocks every call to it. CI passes the type check and the test run. Which checklist items should still stop the merge, and why?
Show answer
Item 1 should stop it. The package may not exist, or may be a look-alike for a real one, and the import check would fail if the package is not installed. If CI installed it anyway, the reviewer must verify the package on the index and confirm it was added deliberately. Item 2 applies because the dependency is not pinned. Item 5 applies because mocking every call means the test never exercises the real library, so wrong API usage would not fail.