Registry / type-stubs / types-jinja2

types-jinja2

JSON →
library2.11.9pypypi✓ verified 26d ago

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-jinja2
INSTALL
IMPORT
SIG · TYPES-JINJA2
T
types-jinja2
type-stubspythonv2.11.9
Install
1.6s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.11.9 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Environment
from jinja2_stubs import Environment
from jinja2-stubs import Environment

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.

from jinja2 import Environment, FileSystemLoader import os # Create a dummy template file for the example with open('hello.html', 'w') as f: f.write('Hello, {{ name }}! Today is {{ date }}.') # Set up the Jinja2 environment # In a real application, ensure `template_dir` points to your templates directory template_dir = os.path.dirname(os.path.abspath(__file__)) env = Environment(loader=FileSystemLoader(template_dir)) # Load and render a template template = env.get_template('hello.html') output = template.render(name='World', date='2026-04-09') print(output) # Example with missing variable (mypy would flag this if types-jinja2 is active) # try: # template.render(name='Jinja') # 'date' is missing, mypy would warn # except Exception as e: # print(f"Rendering with missing data (expected by mypy): {e}") # Clean up the dummy file os.remove('hello.html')
Debug
Known issues
breaking`types-jinja2` is redundant and should be uninstalled if using Jinja2 version 3.0 or newer. Jinja2 itself includes comprehensive type annotations from version 3.0 onwards, making this stub package unnecessary and potentially conflict-prone.
fix
If your `Jinja2` version is 3.0 or higher, uninstall `types-jinja2`: `pip uninstall types-jinja2`.
affects: Jinja2 >= 3.0
gotchaType stubs (`.pyi` files) provide type information solely for static analysis and have no impact on runtime behavior. Errors caught by a type checker using `types-jinja2` will not prevent the code from running if the underlying `Jinja2` operation is syntactically valid but semantically incorrect (e.g., wrong type passed at runtime).
fix
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.
affects: All
gotchaThe versioning of `types-jinja2` is `X.Y.Z.YYYYMMDD`, where `X.Y.Z` aligns with the `Jinja2` runtime package version. While typeshed aims for minimal breaking changes, any stub version bump might cause type-checking failures in your code due to stricter annotations.
fix
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.
affects: All
gotchaComplex logic within Jinja2 templates can make type checking difficult and is generally considered a bad practice. Jinja2's template language is intentionally limited compared to full Python.
fix
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.
affects: All
Upgrade
Version history
2.11.9latest on PyPI · released Nov 26, 2021
Audit
Dependencies
Jinja2requiredProvides type stubs for the Jinja2 templating engine.
Agent activity
38 hits · last 30 days
node
34
OpenAI (training)
1
Resources
types-jinja2 — pip install types-jinja2 · libregistry