Skip to main content

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 .py files.
  • Stub Generation:

    • Parse type annotations and generate .pyi stubs for each template.
    • Stubs are placed in the same directory as the template file, with the same base name and a .pyi extension.
    • No runtime Python modules are generated; this is a static analysis and developer tooling solution only.
  • LSP/Editor Integration:

    • .pyi stubs 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

  1. Define Types in Python:

    # mytypes.py
    from typing import TypedDict
    class User(TypedDict):
        name: str
        age: int
    
  2. Annotate Template Context:

    {# @types
    from mytypes import User
    user: User
    #}
    Hello, {{ user.name }}!
    
  3. Generate Stubs: Run the CLI:

    python -m typedjinja path/to/template.jinja
    

    This creates path/to/template.pyi in the same directory as your template.

  4. Editor Integration:

    • Editors and type checkers (e.g., mypy, Pyright, PyCharm) will automatically use .pyi stubs placed next to your template files.
    • Enjoy completions and type checking for template variables!

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 .pyi stubs for each template for static analysis and editor intelligence.
  • 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 @types block, 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

  1. Annotation Syntax & Parser
  2. Stub Generation
  3. LSP Plugin for Editor Support
  4. Documentation & Examples
  5. 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.json with proper publisher, repository, and license, then:
    vsce publish
    

Usage

  1. Annotate Template Context:

    {# @types
    from datetime import datetime
    
    created_at: datetime
    data: dict[str, str]
    #}
    {% from "another_template.jinja" import one_macro %}
    
  2. Stub Generation (offline or on save):

    python -m typedjinja path/to/template.jinja
    
  3. Install VSCode Extension:

    • Download the .vsix package 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 generated typedjinja-vscode-0.0.1.vsix.
  4. Editor Experience:

    • Open your .jinja file.
    • Completions, hover, go-to-definition, and diagnostics will work out of the box for variables, macros, includes, and types.

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

typedjinja-0.1.0.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typedjinja-0.1.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

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

Hashes for typedjinja-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e2d5c24246ffb9b9d9003d7e691c2f75ab8c85122bbe273a8e0c5143bf8f12b1
MD5 48df1cfb265a959309ba8642f5c8452f
BLAKE2b-256 8fa1dc8da45d334a2d9439c8617f8f0f37675b835ed4f54047c9a2cc441a08d3

See more details on using hashes here.

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

Hashes for typedjinja-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 93a57c06407ec638fb6983d1c7bdd5782a360f7c9d45db5ab17ffd989a92b221
MD5 68b3f390cad69dbd98627931f2143edd
BLAKE2b-256 1c746844b7f96e8d18a805e64df31fbdb22f0fcb18d00fa5f74f7484cc6fb96f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page