Sub topic 9
Shipping type hints with py.typed
This chapter assumes you can write type hints, have built the greeter project from earlier chapters, and know that pyproject.toml controls what goes into a wheel. By the end you will know how to tell type checkers that your library is typed, and how to confirm the marker is in your build.
Why a marker file is needed
Your library may have full annotations, but type checkers such as mypy and pyright still ignore them by default when they find your package installed in a user's environment. [PEP 561](https://peps.python.org/pep-0561/) says an installed package is treated as untyped unless it opts in. The opt-in is an empty file named py.typed inside the import package.
Without it, a user who runs mypy on code that imports greeter sees an error like this:
error: Skipping analyzing "greeter": module is installed, but missing library stubs or py.typed markerWith the marker present, the checker reads your annotations and checks calls to greet() against name: str and -> str.
Add the marker
Place an empty py.typed file next to __init__.py. The src layout from the first chapter already has it:
src/
└── greeter/
├── __init__.py
├── cli.py
└── py.typedThe file can be empty. Create it with touch src/greeter/py.typed.
Make sure it ships
A file that is not Python code is package data, so a build backend may leave it out. Recent setuptools versions include py.typed automatically when pyproject.toml has no explicit package-data settings. If you set include-package-data = false or list package data yourself, add it explicitly:
[tool.setuptools.package-data]
greeter = ["py.typed"]Other backends such as Hatchling include all files in the package directory by default.
Verify the build
Build the wheel, then list its contents. A wheel is a zip file, so the standard library can read it:
python -m build --wheel
python -m zipfile -l dist/greeter-0.1.0-py3-none-any.whlThe listing should include greeter/py.typed. If it does not, the marker was dropped and users will not get type checking.
You can also test the result from a consumer's point of view. Install the wheel in a clean environment, then run mypy on a small file that calls your library:
# check_usage.py
from greeter import greet
greet(42) # mypy should report an argument type error here/tmp/verify/bin/pip install mypy
/tmp/verify/bin/mypy check_usage.pyIf mypy reports Argument 1 to "greet" has incompatible type "int"; expected "str", the marker works.
Common errors
- Marker outside the package.
py.typedmust sit insidesrc/greeter/, not at the project root. - Partial annotations. The marker is a promise. If half your functions lack annotations, checkers treat the missing ones as
Any, and users lose safety without a warning. - Private imports in signatures. Public functions that expose types from untyped dependencies pass those gaps on to users.
- Subpackages. One marker in the top-level package covers its subpackages.
Exercise
You added annotations to every function in your library but users report that mypy still says the module is missing a py.typed marker. The file exists at the project root. What is wrong, and how do you confirm the fix?
Show answer
The marker must be inside the import package (src/greeter/py.typed), not the project root. Move it, rebuild the wheel, and list the wheel's contents with python -m zipfile -l to confirm that greeter/py.typed appears. Then install the wheel in a clean environment and run mypy on code that imports the library.