py.typed
Overview
The file **`py.typed`** is a special marker file used in Python projects to indicate that the package contains [type hints](https://www.python.org/dev/peps/pep-0561/) compliant with [PEP 561](https://www.python.org/dev/peps/pep-0561/). Its presence signals to type checkers (like `mypy`, `pyright`, or IDEs with type inference) that the package includes inline type annotations or stub files, enabling enhanced static type checking for consumers of the package.
Unlike typical Python source code files, `py.typed` is usually empty and does not contain executable code, classes, or functions. Instead, it serves as a metadata marker recognized by tooling in the Python ecosystem.
Purpose and Functionality
Purpose:
To declare that the Python package is typed and supports static type checking.Functionality:
When included in the root directory of a Python package (alongside__init__.py),py.typedtells type checkers that the package's.pyfiles contain type annotations or that stub files (.pyi) are available. This lets tools provide accurate type checking and autocomplete for users of the package.File Content:
Typically empty, or sometimes it may contain a single line with the string"partial"to indicate partial typing coverage as per PEP 561. However, the file content is optional.
Usage Example
Suppose you publish a package named `example_pkg` with type annotations:
example_pkg/
├── __init__.py
├── module.py # contains type hints in its functions and classes
└── py.typed # marker file (empty)
When users install
example_pkg, type checkers detect thepy.typedfile and enable type checking for this package automatically.Without
py.typed, the package is treated as untyped, and type checkers won't validate or infer types from it.
Important Implementation Details
Location:
Thepy.typedfile must reside in the root directory of the Python package directory (the same directory as__init__.py).File Content Variants:
Empty file: Indicates full typing coverage.
Contains
"partial"(case insensitive): Indicates partial typing coverage, meaning some modules may not be fully typed.
Packaging:
The file must be included in the package distribution (e.g., in the source distribution and wheel) and declared inMANIFEST.inor package configuration files (setup.cfg,pyproject.toml) to ensure it is installed with the package.Compliance:
This mechanism aligns with PEP 561 — Distributing and Packaging Type Information.
Interaction with Other Parts of the System
Type Checkers and IDEs:
Tools likemypy,pyright, PyCharm, and VSCode look for thepy.typedmarker to enable type checking against the package's code or stubs.Packaging Tools:
Packaging tools likesetuptoolsorflitmust include thepy.typedfile inside the package when building distributions.Consumers of the Package:
Developers importing the package get improved type inference and error detection thanks to the presence ofpy.typed.
Diagram
Since this file is a simple marker file without classes or functions, a flowchart illustrating its role in the type checking workflow is more appropriate than a class or component diagram.
flowchart TD
A[Python Package]
B[py.typed file present?]
C[Type Checker (mypy, pyright)]
D[Enable static type checking]
E[Assume untyped package]
A --> B
B -- Yes --> C
B -- No --> E
C --> D
If the
py.typedfile is detected in the package directory, type checkers enable static type checking for that package.Otherwise, the package is treated as untyped.
Summary
Aspect | Description |
|---|---|
File type | Marker file (empty or contains "partial") |
Purpose | Indicate package contains type information |
Location | Root directory of Python package |
Usage | Enables type checkers to apply static analysis |
Related PEP | |
Packaging Requirement | Must be included in distributed package |
References
This concludes the comprehensive documentation for the `py.typed` file.