Sub topic 10
Letting linters and type checkers do the cheap work
This chapter assumes you can read a Git diff, run Python from a terminal, and write a basic unit test. You should also have read the earlier chapters on the diff and on edge cases.
Why run tools before you read
Reading a diff costs attention. A linter, a type checker, and a static analyzer cost almost nothing, and they catch a class of mistakes that people miss when they skim plausible-looking code. Run them first so your own review can go to intent, logic, and design.
| Tool type | Example | What it catches in AI output |
|---|---|---|
| Linter | ruff | Unused imports, undefined names, shadowed variables, risky patterns |
| Type checker | mypy | Wrong argument types, missing `None` handling, calls to functions with the wrong signature |
| Security scanner | bandit | `eval`, weak hashing, SQL built from strings, hardcoded passwords |
A worked example
An assistant wrote this helper. It reads well, and it has three problems.
import os
import json
def load_port(path: str) -> int:
with open(path) as f:
config = json.load(f)
return config.get("port")
def describe(path: str) -> str:
port = load_port(path)
return "listening on " + portRun both tools from the project root:
ruff check .
mypy --strict .Typical output:
example.py:1:8: F401 [*] `os` imported but unused
example.py:8: error: Incompatible return value type (got "Any | None", expected "int")
example.py:13: error: Unsupported operand types for + ("str" and "int")The unused os import suggests the assistant copied a template. The first type error shows that a missing port key returns None, which breaks the declared int. The second error shows that string concatenation with an integer fails at runtime. All three were found without reading the logic closely.
Here is a corrected version:
import json
def load_port(path: str) -> int:
with open(path) as f:
config = json.load(f)
port = config.get("port")
if not isinstance(port, int):
raise ValueError("config must contain an integer 'port'")
return port
def describe(path: str) -> str:
return f"listening on {load_port(path)}"How the tools fit into the review
Pasting the tool output back to the assistant is a good way to get a fix, because the messages are exact and the assistant can act on them.
Common errors when relying on these tools
- Treating a clean run as approval. These tools check syntax, types, and known risky patterns. They do not know your requirement. A function can pass every check and still compute the wrong thing.
- Letting the assistant silence the tool. Watch for
# type: ignore,# noqa,Any, or edits to the lint configuration in the diff. Each one hides a finding instead of fixing it. Ask why it is there. - Weak configuration. Without
--strictor an equivalent setting, mypy skips unannotated functions entirely, so AI code with no annotations passes without being checked. - Running only on changed files. A signature change can break callers in files the diff never touched. Run the checks on the whole project.
Exercise
An assistant's diff makes mypy pass by adding # type: ignore to three lines. What should you do, and why does a passing mypy run not settle the matter?
Show answer
Treat each ignore comment as a hidden finding. Remove it and ask for a real fix, or require a written reason for keeping it. A passing run only means the remaining code satisfies the checker. The suppressed lines were never checked, and the checker cannot tell whether the code meets the requirement.
Exercise
Name two ways a diff can pass ruff and mypy and still be wrong.
Show answer
It can implement the wrong behavior, such as computing the wrong value with correct types. It can also miss an edge case, such as an empty list, that no rule flags. A call to a real function with a valid but inappropriate argument also passes.