types-jinja2 is a PEP 561 type stub package that provides type annotations for the Jinja2 templating engine. It enables static type-checking tools like mypy, PyCharm, and pytype to analyze code that uses Jinja2 for improved code quality and early error detection. This package is part of the `python/typeshed` monorepo and is automatically released multiple times a day when updates are needed.
pip install types-jinja2Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic Jinja2 usage. When `types-jinja2` is installed, a type checker like mypy will use these stubs to analyze the Jinja2 code, ensuring type correctness for `Environment`, `Template`, and their methods.
If your `Jinja2` version is 3.0 or higher, uninstall `types-jinja2`: `pip uninstall types-jinja2`.
Understand that type checking is a build-time or development-time activity. Always run your code and include runtime checks where appropriate, especially for external inputs.
It is recommended to pin the version of `types-jinja2` with the same bounds you use for `Jinja2` (e.g., `types-jinja2>=2.11.0,<2.12`). Regularly review typeshed's changelog for the `Jinja2` stubs when upgrading.
Shift complex data manipulation, business logic, or heavy conditional statements from Jinja2 templates into your Python application code. Prepare data thoroughly before passing it to the template for rendering.