Sub topic 6
Let type checkers and linters flag missing attributes
This chapter assumes you can run Python scripts, install packages with pip, and read a traceback. You do not need prior experience with type checkers.
Why static analysis fits this problem
An invented API call such as requests.get_json(...) only fails when that line runs. If it sits in an error handler or a rarely used branch, you may not see the failure for weeks. A static analyzer reads your code without running it. It looks up each name in the installed library and reports names that do not exist.
Two kinds of tools help here:
- A type checker such as mypy or pyright resolves attributes against the library's source or type stubs. It reports a missing attribute and a wrong argument name or count.
- A linter such as Ruff finds undefined names and, with the right rules, deprecated usage.
Catching a missing attribute
Save this file as fetch.py. It uses the invented get_json function from earlier in this tutorial.
import requests
def fetch(url: str) -> dict:
return requests.get_json(url)Run mypy against it:
pip install mypy types-requests
mypy fetch.pymypy reports an error like this:
fetch.py:4: error: Module has no attribute "get_json" [attr-defined]The check never made a network request. It matched the name against the type stubs for requests. Pyright gives the equivalent report, "get_json" is not a known attribute of module "requests".
The same tools check signatures. If a call passes a keyword that the function does not accept, the checker reports it:
import json
def dump(data: dict) -> str:
return json.dumps(data, pretty=True)mypy reports Unexpected keyword argument "pretty" for "dumps" [call-arg]. This is the static counterpart of the inspect.signature check from the previous chapter, applied to every call in the file at once.
Linters for undefined names
Ruff checks for names that are never defined or imported. The F rules come from pyflakes, and F821 flags an undefined name.
pip install ruff
ruff check --select F,B,UP .The UP rules (pyupgrade) flag some outdated patterns when you set a target Python version. For example, with target-version = "py312" set in pyproject.toml, Ruff's UP017 suggests datetime.UTC in place of timezone.utc. Ruff also has a DTZ003 rule that flags datetime.utcnow().
[tool.ruff]
target-version = "py312"
[tool.ruff.lint]
select = ["F", "B", "UP", "DTZ"]Linters cover patterns they have rules for. They do not look up each library attribute the way a type checker does. Use both.
Common errors when using these tools
- Missing stubs. If a library ships no type information, mypy reports
import-untypedor treats the module asAny. Install a stub package such astypes-requestswhere one exists. Without stubs, attribute checks on that library silently pass. - Ignoring errors. A generated
# type: ignorecomment hides exactly the error you want to see. Review any added ignore comments as carefully as new code. - Dynamic attributes. Libraries that build attributes at runtime, or code that uses
getattr, can produce false positives or false negatives. Treat a clean result as a good sign and not as proof. - Wrong environment. The checker must run in the environment where the library is installed. If it analyzes a different version than your project uses, it checks the wrong API.
- Unannotated functions. By default mypy skips the bodies of functions with no annotations. Run it with
--check-untyped-defsso those bodies are checked too.
A practical configuration
A small mypy configuration makes the checks stricter without requiring full annotations everywhere:
[tool.mypy]
check_untyped_defs = true
warn_unused_ignores = true
disallow_any_unimported = truewarn_unused_ignores reports # type: ignore comments that no longer suppress anything, which keeps stale ignores from piling up.
Exercise
A teammate pastes AI-generated code that calls json.dumps(data, pretty=True). mypy reports no error for the file. Give two likely reasons, and say what you would check.
Show answer
Likely reasons: the function containing the call has no annotations and mypy is skipping its body, or the line carries a # type: ignore comment. A third possibility is that the checker runs against a different environment. Run mypy with --check-untyped-defs, search the file for type: ignore, and confirm the checker runs in the project's environment. Then rerun it, and expect Unexpected keyword argument "pretty" for "dumps".