Typing Stubs for Werkzeug
types-werkzeug is a PEP 561 type stub package providing static type annotations for the Werkzeug WSGI utility library. It is used by type-checking tools like MyPy, PyCharm, and Pytype to verify the correctness of code that utilizes Werkzeug. The stubs are sourced from the typeshed project, which automatically releases updates to PyPI, often daily. Note that Werkzeug versions 2.0 and newer include their own inline type annotations, making this stub package redundant for those versions.
Warnings
- breaking Werkzeug versions 2.0 and newer ship with their own inline type annotations. If you are using Werkzeug >= 2.0, you should uninstall `types-werkzeug` to avoid conflicts and ensure your type checker uses the native types.
- gotcha `types-werkzeug` provides only type hints; it does not include the actual runtime code for Werkzeug. The core `werkzeug` library must be installed alongside `types-werkzeug` for your application to function.
- gotcha Stub package versions are generally tied to the runtime package versions they're meant to type. Using an older `types-werkzeug` with a much newer (or vice-versa) `werkzeug` could lead to inaccurate or incorrect type checking results.
- gotcha For type checkers like MyPy, sometimes strictness in stubs can expose existing 'type errors' in code that previously passed. Updates to `types-werkzeug` might introduce new checks that break existing type-checking passes.
- gotcha When using Werkzeug's `abort()` function (often via Flask), MyPy might report issues if the return type is not correctly annotated. This is because type checkers need to understand that `abort()` prevents further execution in the function.
Install
-
pip install types-werkzeug
Imports
- Request, Response
from werkzeug.wrappers import Request, Response
Quickstart
import os
from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple
@Request.application
def application(request: Request) -> Response:
"""A simple Werkzeug application demonstrating type hints."""
name = request.args.get('name', 'World')
return Response(f"Hello, {name}!")
if __name__ == "__main__":
# This part is for running the server, not directly for type checking.
# To check types, save this as `app.py` and run `mypy app.py`
# You'd typically need `pip install werkzeug mypy` and optionally `pip install types-werkzeug`
# (unless Werkzeug >= 2.0 is used, which has its own types).
print("To run the app: http://127.0.0.1:5000/")
# run_simple("127.0.0.1", 5000, application)