Sub topic 12
Know what these checks will not catch
This chapter assumes you have worked through the earlier checks in this tutorial: introspection, versioned docs, type checkers, deprecation warnings, tests, and dependency verification. It also assumes you can read Python and run pytest. The aim is to show where those checks stop, so a green build does not give you more confidence than it has earned.
Why a passing check is not proof
Each check answers a narrow question. Introspection asks whether a name exists. A type checker asks whether the types line up. A deprecation filter asks whether the library has announced a removal. None of them asks whether the code does what you meant.
Real calls with the wrong meaning
A function can exist, accept your arguments, and still do something other than what the assistant implied. Type checkers cannot see this, because the call is valid.
# Intended: remove the "http://" prefix.
print("http://thread.example".lstrip("http://")) # read.example
# Correct: removeprefix (Python 3.9+) removes the exact prefix.
print("http://thread.example".removeprefix("http://")) # thread.examplelstrip treats its argument as a set of characters, not a prefix. The first line passes every existence and type check and still returns the wrong string. Only a test with a well-chosen input, or a reviewer who knows the API, catches it.
Missing safety arguments belong here too. requests.get(url) without timeout is valid, passes linting in many setups, and can hang forever.
Silent changes between versions
Deprecation warnings only cover changes the maintainers announced. A library can change a default value, a sort order, or an error type without any warning. Generated code that relied on the old behavior will import cleanly, call cleanly, and return different results.
The changelog check helps here, but only if you read the release notes for every version between the one the assistant was trained on and the one you run. Pinning versions makes these changes happen on your schedule. It does not stop them.
Tests that cannot fail
Tests help only if they can fail when the API is wrong. Mocks often cannot:
from unittest.mock import Mock, create_autospec
import requests
loose = Mock()
loose.get_json("https://example.com") # passes, though no such function exists
strict = create_autospec(requests, spec_set=True)
try:
strict.get_json("https://example.com")
except AttributeError as exc:
print("caught:", exc)A plain Mock accepts any attribute and any arguments, so a test built on it will happily confirm an invented API. create_autospec copies the real object's shape and rejects names and signatures that are not there. Use it, or patch(..., autospec=True), whenever generated code talks to a library you mock.
Coverage is a second limit. A test can only expose the code paths it runs. An invented call inside an error handler stays hidden until the error happens in production.
Packages that exist but are not what you want
An existence check on a package name proves only that someone published something under that name. Look-alike and typosquatted packages exist by design. Pinning and hash checking confirm you install the same artifact each time. They do not confirm that the artifact is trustworthy. Reviewing the project's maintainers, history, and download base remains a human judgment.
Gaps that tools do not cover
| Gap | Why checks miss it | What helps |
|---|---|---|
| Valid call, wrong meaning | The name, types, and signature all match | Targeted tests, reading the docs for the call |
| Silent default or behavior change | No warning is emitted | Release-note review, pinned versions |
| Dynamic attributes | Type checkers cannot resolve `getattr` or runtime-built names | Run the code path, use autospec |
| Untested branches | Tests never execute the bad call | Coverage review, error-path tests |
| Malicious or look-alike package | The package exists and installs | Source review, an allow-list of dependencies |
| Security and performance flaws | Neither is an API question | Security review, profiling |
Using the residual risk
Treat the checks as a filter that removes the cheap failures, so that review time goes to the expensive ones. In practice:
- Mark generated changes that touch parsing, money, time zones, or authentication for a closer human read. These are the places where valid but wrong calls cost the most.
- Ask what input would make the code behave differently from your intent, and write that test first.
- Keep mocks strict so they enforce the real interface.
- Record the library versions a change was verified against, so a later upgrade triggers a re-check.
Exercise
A generated helper calls text.rstrip(".csv") to remove a file extension. It exists, type checks, and passes a test that uses "data.csv". Why is it still wrong, and what test would expose it?
Show answer
rstrip removes any trailing characters from the set ".", "c", "s", "v", not the exact suffix. "analysis.csv" becomes "analysi". A test with a name ending in those letters, such as "analysis.csv" or "docs.csv", fails and exposes the bug. removesuffix(".csv") is the correct call.
Exercise
Why does a test that uses Mock() for a client library fail to catch an invented method, and what change fixes it?
Show answer
Mock() creates any attribute or call on demand, so a nonexistent method returns a mock instead of raising. Using create_autospec or patch(..., autospec=True) copies the real interface, so an invented name raises AttributeError.