Type safety and editor intelligence for Jinja2 templates.
Project description
TypedJinja
Project Vision
TypedJinja brings type safety and editor intelligence to Jinja2 templates by allowing developers to annotate available variables and their types (including custom Python types) directly in template files. The tool parses these annotations and generates Python type stubs (.pyi files) for each template, enabling Language Server Protocol (LSP) support for completions, hover, and type checking in editors.
Key Principles
-
Type Annotations in Templates:
- Use a special comment block (e.g.,
{# @types ... #}) at the top of Jinja2 templates to declare available variables and their types. - Support both built-in and custom Python types, imported from user-defined
.pyfiles.
- Use a special comment block (e.g.,
-
Stub Generation:
- Parse type annotations and generate
.pyistubs for each template. - Stubs are placed in the same directory as the template file, with the same base name and a
.pyiextension. - No runtime Python modules are generated; this is a static analysis and developer tooling solution only.
- Parse type annotations and generate
-
LSP/Editor Integration:
.pyistubs enable completions, hover, and type checking for template variables in supported editors (VS Code, PyCharm, etc.).- LSP implementation may be in TypeScript for best editor compatibility.
- Editors and type checkers will automatically discover stubs placed next to the template files.
-
Extensibility:
- Designed for easy extension to new types, frameworks, and editor features.
-
Performance:
- Uses caching and incremental parsing to keep editor feedback fast.
Usage
-
Define Types in Python:
# mytypes.py from typing import TypedDict class User(TypedDict): name: str age: int
-
Annotate Template Context:
{# @types from mytypes import User user: User #} Hello, {{ user.name }}!
-
Generate Stubs: Run the CLI:
python -m typedjinja path/to/template.jinja
This creates
path/to/template.pyiin the same directory as your template. -
Editor Integration:
- Editors and type checkers (e.g., mypy, Pyright, PyCharm) will automatically use
.pyistubs placed next to your template files. - Enjoy completions and type checking for template variables!
- Editors and type checkers (e.g., mypy, Pyright, PyCharm) will automatically use
Development Guidelines
- Write clear, well-documented code and tests.
- Prefer Python for core parsing and stub generation; TypeScript for LSP/editor integration.
- Keep the codebase modular: separate parsing, stub generation, and LSP logic.
- Document all annotation syntax and usage in the README.
- Prioritize developer experience: fast feedback, clear errors, and easy onboarding.
Features
- Type Annotations in Templates:
- Declare available variables and their types (including custom Python types) at the top of your Jinja2 templates using a special comment block.
- Stub Generation:
- Generate Python
.pyistubs for each template for static analysis and editor intelligence.
- Generate Python
- LSP Integration:
- Get completions, hover, and type checking for template variables in supported editors (VS Code, PyCharm, etc.).
- Extensible & Fast:
- Designed for easy extension and fast feedback in the editor.
- VSCode Extension & LSP Features:
- Completions: Context-aware completions for variables, attributes, and macro arguments powered by Python stub generation.
- Hover Information: Hover over variables, macros, or includes to see full type signatures and documentation.
- Go to Definition:
- Jump to variable definitions in your Python types or stubs.
- Jump to macro definitions (same-file or imported) using Tree-sitter parsing.
- Jump to included templates via
{% include %}.
- Diagnostics: Real-time error squiggles for invalid attribute access or calls based on Python reflection.
- @types Block Navigation: Hover and go-to-definition inside the
@typesblock, resolving to real Python definitions via Jedi.
Example
{# @types
from mytypes import User, Item
user: User
items: list[Item]
show_details: bool
#}
<h1>Hello, {{ user.name }}!</h1>
<ul>
{% for item in items %}
<li>{{ item.title }}</li>
{% endfor %}
</ul>
Roadmap
- Annotation Syntax & Parser
- Stub Generation
- LSP Plugin for Editor Support
- Documentation & Examples
- Advanced LSP: Find References, Rename, Symbols
Contributing
Contributions are welcome! Please see CURSOR_RULES.md for development guidelines and open an issue or pull request to get started.
Packaging & Publishing
- Build extension:
pnpm run compile
- Package VSIX:
npx vsce package --no-dependencies
- Publish to Marketplace: Update
package.jsonwith properpublisher,repository, andlicense, then:vsce publish
Usage
-
Annotate Template Context:
{# @types from datetime import datetime created_at: datetime data: dict[str, str] #} {% from "another_template.jinja" import one_macro %}
-
Stub Generation (offline or on save):
python -m typedjinja path/to/template.jinja
-
Install VSCode Extension:
- Download the
.vsixpackage from the releases or build locally:cd typedjinja-vscode pnpm install pnpm run compile npx vsce package --no-dependencies
- In VSCode:
Extensions: Install from VSIX..., select the generatedtypedjinja-vscode-0.0.1.vsix.
- Download the
-
Editor Experience:
- Open your
.jinjafile. - Completions, hover, go-to-definition, and diagnostics will work out of the box for variables, macros, includes, and types.
- Open your
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file typedjinja-0.1.0.tar.gz.
File metadata
- Download URL: typedjinja-0.1.0.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e2d5c24246ffb9b9d9003d7e691c2f75ab8c85122bbe273a8e0c5143bf8f12b1
|
|
| MD5 |
48df1cfb265a959309ba8642f5c8452f
|
|
| BLAKE2b-256 |
8fa1dc8da45d334a2d9439c8617f8f0f37675b835ed4f54047c9a2cc441a08d3
|
File details
Details for the file typedjinja-0.1.0-py3-none-any.whl.
File metadata
- Download URL: typedjinja-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
93a57c06407ec638fb6983d1c7bdd5782a360f7c9d45db5ab17ffd989a92b221
|
|
| MD5 |
68b3f390cad69dbd98627931f2143edd
|
|
| BLAKE2b-256 |
1c746844b7f96e8d18a805e64df31fbdb22f0fcb18d00fa5f74f7484cc6fb96f
|