Skip to main content

Type checker for your Jinja2 templates

Project description

Typja

Python Version Build and Test Typja codecov License: MIT

An experimental tool to check Jinja templates for type errors using Python's type hints. It parses Jinja templates, extracts variable usage, and checks them against provided type information.

Features

  • Type Checking for Jinja2 Templates: Validate that variables in your Jinja2 templates match their declared types
  • Python Type Hints Integration: Leverage your existing Python type annotations to catch template errors early
  • Pre-commit Integration: Built-in pre-commit hook for automated checking
  • Flexible Configuration: Customize checking behavior with typja.toml
  • Rich Error Reports: Detailed error messages with code snippets and suggestions
  • Watch Mode: Monitor templates for changes and run checks automatically

Quick Start

Installation

pip install typja

Basic Usage

# Check all templates in current directory
typja check

# Run in watch mode
typja watch

# Initialize a new typja.toml configuration
typja init

Configuration

Create a typja.toml file in your project root with typja init or manually:

[project]
root = "."
paths = ["./models/**/*.py"]
fail_on_warning = false

[environment]
template_dirs = ["./templates"]
include_patterns = ["*.html", "*.jinja", "*.jinja2"]

[linting]
strict = false
prefer_pep604_unions = true
validate_imports = true
validate_variables = true

[errors]
verbosity = "normal"
show_snippets = true
show_hints = true
color = "auto"

Type Annotations in Templates

Use special Jinja2 comments to declare variable types:

{# typja:var user: User #}
{# typja:var items: list[str] #}
{# typja:var count: int #}

<h1>{{ user.name }}</h1>
<ul>
  {% for item in items %}
    <li>{{ item }}</li>
  {% endfor %}
</ul>

Examples

See the examples/fastapi/ directory for a complete FastAPI application with Jinja2 template type checking.

Pre-commit Integration

Add to your .pre-commit-config.yaml:

repos:
  - repo: https://github.com/Daniel-Brai/Typja
    rev: v0.1.0
    hooks:
      - id: typja-check
        # With args
        # args: ["--strict", "--fix"]

Documentation

Contributing

See CONTRIBUTING.md for development instructions.

License

See LICENSE for details.

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

typja-0.1.2.tar.gz (27.3 kB view details)

Uploaded Source

Built Distribution

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

typja-0.1.2-py3-none-any.whl (36.1 kB view details)

Uploaded Python 3

File details

Details for the file typja-0.1.2.tar.gz.

File metadata

  • Download URL: typja-0.1.2.tar.gz
  • Upload date:
  • Size: 27.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for typja-0.1.2.tar.gz
Algorithm Hash digest
SHA256 cb1e678ead532667152a8925e033a76a12966a1492091cf49a901e4f41e8297c
MD5 545e1d109152688a463f3e14a1ef5bff
BLAKE2b-256 3a5b8637081c80905c508ac4694f9705c42b2b8e3aee7b710cb3fbf4dd4fac5e

See more details on using hashes here.

Provenance

The following attestation bundles were made for typja-0.1.2.tar.gz:

Publisher: ci.yml on Daniel-Brai/Typja

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file typja-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: typja-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 36.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for typja-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9d46ce3c4d54ed75a43df2cf1637c7e8ce7d6d9696dd409642f6663d8b8dae33
MD5 d10c466f63522b6cfc19824415b658e1
BLAKE2b-256 06d76cb209fdb00ef30d576a4d34c9160701ac1ff854a5f68975456de8317e39

See more details on using hashes here.

Provenance

The following attestation bundles were made for typja-0.1.2-py3-none-any.whl:

Publisher: ci.yml on Daniel-Brai/Typja

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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