Sub topic 14
Typing third-party libraries: stubs and py.typed
This chapter assumes you can run mypy or Pyright on a project and read their errors. It covers what the checkers do when your code imports a library, and how to give them type information when the library has none.
Where type information comes from
A checker needs types for every import. For an installed package, it looks in three places:
- The package itself, if it ships inline annotations and a
py.typedmarker file (PEP 561). The marker tells checkers the package's annotations are meant to be used. - A separate stub package, such as
types-requests. Stubs are.pyifiles that hold only signatures. These packages come from the typeshed project. - Stubs you write yourself or generate locally.
The error you will see
Without type information, mypy reports one of two errors:
Library stubs not installed for "yaml"means a stub package exists. The hint names it, heretypes-PyYAML.module is installed, but missing library stubs or py.typed markermeans no stubs are available. The error code isimport-untyped.
Install the stubs as development dependencies:
python -m pip install types-requests types-PyYAML
mypy --install-types src/--install-types installs the missing stub packages that mypy reports. In CI, prefer pinning the types-* packages in your dev requirements so runs are repeatable.
Pyright bundles typeshed's third-party stubs. If it finds a stub but cannot find the real package, it warns with reportMissingModuleSource. That usually means you are checking with the wrong virtual environment.
Libraries with no types at all
When no stubs exist, you have three options, from least to most effort.
First, mark the import as untyped for specific modules only. Avoid a global ignore_missing_imports, because it hides typos in import names too.
[[tool.mypy.overrides]]
module = ["legacy_lib.*"]
ignore_missing_imports = trueEverything imported from legacy_lib becomes Any, so calls into it are not checked.
Second, write a small stub for the parts you use. For Pyright, put .pyi files in a typings directory (the default stubPath). For mypy, set mypy_path = "typings".
# typings/legacy_lib/__init__.pyi
def parse(text: str) -> dict[str, int]: ...Pyright can also generate a starting point with pyright --createstub legacy_lib. Review the result, because generated stubs are often incomplete.
Third, contribute the stubs to typeshed, or ask the library authors to ship py.typed.
Gotchas
- A stub only describes the library. If it is wrong, the checker trusts the stub, not the code.
- A
py.typedpackage can still have incomplete annotations. Pyright'sreportMissingTypeStubsand strict mode flag imports from packages that lack types. - Stub package versions can lag behind the library. Upgrading the library may produce new errors until the stubs catch up.
Exercise
Your project imports requests, and mypy reports Library stubs not installed for "requests". What do you do, and why is that better than a global ignore_missing_imports?
Show answer
Install types-requests as a dev dependency (or run mypy --install-types). The checker then sees real signatures for requests calls and catches wrong arguments and return-value misuse. A global ignore would make every requests call Any and also hide misspelled import names.