Sub topic 10
Letting linters and type checkers do the cheap work
This chapter assumes you can read a Git diff, run a Python test suite, and have used an AI coding assistant. The examples use Python, but the ideas apply to any language that has a linter and a type checker.
Why cheap checks come first
A review costs attention. Attention is limited, and AI-generated code arrives in volume. If you spend it on unused imports, wrong argument types, or misspelled attribute names, you have less left for the questions only a person can answer: does this meet the requirement, and does it fit the codebase?
Linters, type checkers, and other static analysis tools read code without running it. They are fast, repeatable, and indifferent to how confident the code looks. That last property matters for AI output, which tends to look polished whether it is correct or not.
Run these tools before you read the diff in detail. Each failure they report is a concrete lead. Each pass is weak evidence that a whole class of mistakes is absent.
What each tool catches
| Tool type | Example tools | What it finds | What it cannot find |
|---|---|---|---|
| Formatter | Black, Prettier, gofmt | Style drift, inconsistent layout | Anything about behavior |
| Linter | Ruff, Flake8, ESLint | Unused names, undefined names, risky constructs, bad patterns | Whether the logic meets the requirement |
| Type checker | mypy, Pyright, TypeScript | Wrong argument types, missing attributes, `None` used as a value | Wrong values of the correct type |
| Security scanner | Bandit, Semgrep, CodeQL | Known dangerous patterns such as `eval` or string-built SQL | Flaws specific to your business rules |
Each row covers a different failure. A clean linter run says nothing about types, and a clean type check says nothing about security. Use the tools together.
Failure modes these tools expose
Earlier chapters described how AI code goes wrong. Several of those failures leave a mark that a tool can see.
- Hallucinated attributes and methods. The model calls a method that does not exist on that object. A type checker reports it as an unknown attribute.
- Undefined names. A helper the model assumed existed, or a variable it renamed halfway through. A linter reports it as an undefined name.
- Wrong signatures. The call passes arguments in the wrong order or with the wrong keywords. A type checker compares the call with the function definition.
- Unhandled
None. The code uses the result of a lookup as if it always succeeds. A type checker flags the optional value. - Leftover code. Unused imports and variables from an abandoned approach. A linter flags them.
- Risky constructs. Bare
except:clauses, mutable default arguments,eval, and string-built SQL. Linters and security scanners flag these.
Static tools do not prove the code is correct. A function can pass every check and still return the wrong answer. Treat a failure as a strong signal and a pass as a modest one.
A worked example
Suppose you asked an assistant for a function that returns the display name of a user, using a lookup table. It produced this:
import os
import json
def display_name(users: dict[int, str], user_id: int) -> str:
name = users.get(user_id)
return name.title()
def label(users: dict[int, str], user_id: int) -> str:
return display_name(user_id, users)The code reads cleanly. A quick skim might accept it. Now run the tools:
ruff check example.py
mypy example.pyRuff reports the two unused imports:
example.py:1:8: F401 `os` imported but unused
example.py:2:8: F401 `json` imported but unusedmypy reports two real defects:
example.py:7: error: Item "None" of "str | None" has no attribute "title" [union-attr]
example.py:11: error: Argument 1 to "display_name" has incompatible type "int"; expected "dict[int, str]" [arg-type]
example.py:11: error: Argument 2 to "display_name" has incompatible type "dict[int, str]"; expected "int" [arg-type]Two findings need a person to interpret them:
users.get(user_id)returnsNonewhen the id is missing, soname.title()raisesAttributeErrorfor unknown users. The requirement never said what should happen then. That is a decision for you, not for the tool.labelswaps the arguments. The code would fail at the first call.
Here is a corrected version. It raises a clear error for unknown users, which you chose because the caller can handle it:
def display_name(users: dict[int, str], user_id: int) -> str:
try:
name = users[user_id]
except KeyError:
raise KeyError(f"unknown user id: {user_id}") from None
return name.title()
def label(users: dict[int, str], user_id: int) -> str:
return display_name(users, user_id)Notice the split of work. The tools found the problems in seconds. You decided the intended behavior for a missing user.
Setting the tools up for AI review
Tools help most when they are configured once and run the same way every time. A few choices make them work for review.
Run them on the changed code, and on the whole project
Run the type checker on the whole project, not only the changed file. A change in one function can break callers elsewhere, and the checker can only see that if it reads them. Linters can run on the changed files only, which keeps output short.
Use a strict enough configuration
A type checker that ignores unannotated functions will skip much of what AI code does. In mypy, --strict turns on a group of stricter checks. You do not need it on a legacy codebase on day one, but you can enable it per module for new code:
# pyproject.toml
[tool.mypy]
python_version = "3.11"
warn_unused_ignores = true
warn_return_any = true
[[tool.mypy.overrides]]
module = "billing.*"
disallow_untyped_defs = true# pyproject.toml
[tool.ruff.lint]
select = ["E", "F", "B", "S", "UP"]In that Ruff configuration, E and F cover basic errors and undefined or unused names, B covers likely bugs such as mutable default arguments, S covers security patterns, and UP flags outdated syntax. The UP rules help with the "outdated pattern" failure mode, because models often reproduce older idioms from their training data.
Run them automatically
If the checks only run when you remember, they will be skipped under time pressure. Put them in a pre-commit hook or in your continuous integration pipeline so every change meets the same bar, whether a person or an assistant wrote it.
ruff check .
ruff format --check .
mypy .Gotchas
Suppression comments
An assistant that cannot satisfy a tool may silence it instead. Look for # type: ignore, # noqa, # nosec, @ts-ignore, and any annotations in the diff. Each one is a place where the code was made to pass without being made correct. Searching the diff for them takes seconds:
git diff | grep -E '^\+.*(type: ignore|noqa|nosec|@ts-ignore)'Setting warn_unused_ignores = true in mypy, as shown above, also reports ignores that no longer suppress anything.
Configuration edits
Check whether the change touches pyproject.toml, .ruff.toml, mypy.ini, or CI files. A model asked to "make the build pass" may loosen the rules instead of fixing the code. Any change to tool configuration deserves its own review, separate from the feature.
Auto-fix without reading
Many linters can rewrite code with a --fix option. That is convenient, but the result is another unreviewed change. Run git diff after auto-fixing, and read what changed.
Types that lie
A type annotation is a claim, and the checker trusts it. If the model annotates a function as returning str but the body returns whatever a loose library call produces, the checker may accept it. Annotations using Any or unchecked casts switch off checking for that value. In the diff, look at Any and cast(...) the way you look at suppression comments.
Missing stubs
If a third-party library has no type information, mypy treats its values as Any, and mistakes in how you use it go unreported. A hallucinated method on such a library passes the type check. For these libraries, the check in the dependency chapter, reading the real documentation, is the real safeguard.
Where this fits in the review
When a check fails, send the exact tool output back to the assistant and ask for a fix. Then run the checks again yourself. Do not accept "I fixed it" as a result.
Using the results as evidence
For the final accept decision, record what you ran. A short note in the pull request is enough:
- Which commands ran and on which commit.
- Whether the configuration changed.
- How many suppression comments the change adds.
This makes your decision checkable by someone else, and it reminds you that a clean run supports only the claims these tools can make.
Exercise
An AI assistant's change makes the CI type check pass. The diff contains # type: ignore on three lines and adds ignore_errors = true for one module in mypy.ini. What do you do, and why?
Show answer
Do not accept the passing result as evidence. The ignores and the configuration change mean the code was made to pass, not made correct. Remove the # type: ignore comments and revert the mypy.ini change, then run mypy again to see the real errors. Send the actual output back for a proper fix, or fix it yourself. Review any configuration change separately, because loosening the checker affects every later change.
Exercise
mypy reports no errors on a function that calls client.fetch_all(page_size=50) from a third-party library, but the call fails at runtime with AttributeError. Give two likely reasons the checker missed it, and one way to confirm the method exists.
Show answer
The library may ship no type stubs, so mypy treats client as Any and skips attribute checks. Alternatively, client may be annotated or cast as Any in the code. To confirm the method exists, check the library's official documentation or inspect the installed package, for example with python -c "import lib; print(dir(lib.Client))" or by reading the source in your environment.