TypedJinja

May 23, 2025 ยท View on GitHub

Python companion package for the VSCode extension that adds type safety and LSP features to Jinja2 templates by generating Python stub files (.pyi).

Installation

pip install typedjinja

Usage

1. Annotate Types

In your Jinja2 template, declare variables using an @types block:

{# @types
from mytypes import User
user: User
#}
Hello, {{ user.name }}!

Generate a stub:

python -m typedjinja path/to/sample_template.html

This creates sample_template.pyi alongside your template.

2. Annotate Macros

Use an @typedmacro block to define macro signatures:

{# @typedmacro
one_macro(name: str = "world")
This macro greets a user.
#}
{% macro one_macro(name = "world") %}
  Hello, {{ name }}!
{% endmacro %}

Stub output includes:

def one_macro(name: str = "world"): ...

Examples

Browse samples/templates:

  • sample_template.html for @types
  • another_template.html for @typedmacro

License

MIT