Contributing to Starlette
Relevant source files
The following files were used as context for generating this wiki page:
Contributing to Starlette involves maintaining a lightweight, high-performance ASGI toolkit designed for building asynchronous web services in Python. The project prioritizes high technical standards, including 100% test coverage and a 100% type-annotated codebase.
Contributors work within a modular architecture that supports both a full framework experience and independent toolkit components. The project is compatible with both asyncio and trio backends and maintains minimal hard dependencies to ensure low complexity.
Sources: README.md:15-35, pyproject.toml:29-33
Development Environment Setup
Starlette uses standardized scripts for managing the development lifecycle, following the "Scripts to Rule Them All" pattern. This ensures a consistent environment for all contributors.
Essential Development Scripts
| Script | Description |
|---|---|
scripts/install | Installs project dependencies in a virtual environment. |
scripts/test | Executes the full test suite using pytest. |
scripts/lint | Runs automated code linting and formatting tools. |
scripts/check | Validates that code passes linting and type checks without making changes. |
scripts/coverage | Verifies that code coverage remains complete (100%). |
scripts/build | Builds the source and wheel packages for distribution. |
Sources: scripts/README.md:3-10
Dependency Management
The project uses hatchling as the build backend and uv for dependency synchronization. Development dependencies are categorized into specific groups to facilitate different contribution tasks.
- Core Dependencies:
anyio(version 3.6.2 to 5.0) andtyping_extensions. - Optional Extras:
itsdangerous(Sessions),jinja2(Templates),python-multipart(Forms),pyyaml(Schemas), andhttpx(TestClient). - Dev Tools:
coverage,mypy,ruff, andpytest.
Sources: pyproject.toml:1-42, pyproject.toml:44-59
Coding Standards and Quality Control
Starlette enforces strict quality control through automated tooling defined in the project configuration.
Static Analysis and Linting
Contributors must adhere to the rules configured for ruff and mypy.
- Ruff: Configured with a line length of 120. It enforces import sorting (
isort), code upgrades (UP), and general error checking (E,F). - Mypy: Enforces
strict = trueacross the codebase, ensuring that all functions and variables are properly typed.
Sources: pyproject.toml:84-110
Testing and Coverage
The project maintains a strict requirement for 100% test coverage. Testing is performed using pytest with specific configurations to treat warnings as errors.
The diagram above illustrates the verification flow required before a contribution is considered ready for submission.
Sources: scripts/README.md:3-10, pyproject.toml:112-125
Core Architecture for Contributors
When contributing new features or middleware, it is essential to understand how the Starlette application class constructs its internal state and middleware stack.
The Middleware Stack
Starlette automatically wraps user-defined middleware with internal safety layers. The ServerErrorMiddleware is always the outermost layer to catch uncaught exceptions, while the ExceptionMiddleware is the innermost layer to handle expected HTTP or WebSocket exceptions.
The diagram shows the order of execution for a request passing through the middleware stack as defined in the build_middleware_stack method.
Sources: starlette/applications.py:70-93
Key Classes for Implementation
| Class | File Path | Purpose |
|---|---|---|
Starlette | starlette/applications.py | The primary application interface for routing and middleware. |
Router | starlette/routing.py | Handles logic for matching paths to endpoints. |
Middleware | starlette/middleware/__init__.py | Data structure for wrapping middleware classes with arguments. |
Response | starlette/responses.py | Base class for all HTTP responses. |
Sources: starlette/applications.py:22-50, README.md:65-73
Documentation and Release
The project's documentation is built using MkDocs with the Material theme. The structure is defined in mkdocs.yml.
- Theme: Material for MkDocs with support for light and dark modes.
- Extensions: Includes
admonition,pymdownx.superfences(for diagrams), andmkdocstringsfor automated API documentation from Python docstrings. - Localization: All documentation and contributions must be in English.
Sources: mkdocs.yml:10-30, mkdocs.yml:85-95
Contributing to Starlette requires a focus on maintaining its status as "the little ASGI library that shines" by ensuring modularity, performance, and absolute type safety.
Sources: README.md:1-10