Help with type checking of variables contexts passed into Jinja templates using only type hints
Project description
🏷️ Typed Jinja Templates
Tiny Python library to help with type checking of variables contexts passed into Jinja templates using only type hints
Installation
This project's distribution packages are published on PyPI: typed-jinja-templates on PyPI.
While it can be installed using pip install typed-jinja-templates, it is only useful as a dependency in a project context.
Therefore, add typed-jinja-templates as a dependency to an existing project.
For uv-managed projects use:
uv add typed-jinja-templates
Usage
Just add one of the parametrized type hints that this library provides to variables with instances of the original types:
- Type
Jinja2TemplatesfromstarletteasTypedJinjaTemplates[TemplateName, ModelType] - Type
Environmentfromjinja2asTypedJinjaEnvironment[TemplateName, ModelType] - Type
Templatefromjinja2asTypedJinjaTemplate[TemplateName, ModelType]
Insert a typing.Literal string type for TemplateName to constrain to the expected template name or template names for type checking.
Insert a typing.TypedDict sub class for ModelType to constrain the template context to a dict conforming to the schema provided for type checking.
Example for TypedJinjaTemplates
from typing import Literal, NotRequired, TypedDict
from starlette.requests import Request
from starlette.responses import HTMLResponse
from starlette.templating import Jinja2Templates
from typed_jinja_templates import TypedJinjaTemplates
TEMPLATES = Jinja2Templates("...") # Contains "hello.jinja.html"
class GreetingContext(TypedDict):
greeting: NotRequired[str]
greetee: str
capitalize: NotRequired[Literal[True]]
# @APP.get("/hello", ...)
def get_hello(request: Request) -> HTMLResponse:
templates: TypedJinjaTemplates[
Literal["hello.jinja.html"], # --> Constrains template name
GreetingContext, # --> Constrains template context
] = TEMPLATES # type: ignore[assignment]
return templates.TemplateResponse(
request=request,
name="hello.jinja.html",
context={
"greetee": "World",
"capitalize": True,
},
)
# @APP.get("/hello", ...)
def get_hello_with_error(request: Request) -> HTMLResponse:
templates: TypedJinjaTemplates[
Literal["hello.jinja.html"], # --> Constrains template name
GreetingContext, # --> Constrains template context
] = TEMPLATES # type: ignore[assignment]
return templates.TemplateResponse(
request=request,
name="hell.jinja.html", # --> Shows error mypy(arg-type)!
context={ # --> Shows error mypy(arg-type)!
"greetee": 42,
"capitalize": True,
},
)
Screenshot of Type Checking Errors
License: MIT
Project details
Release history Release notifications | RSS feed
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 typed_jinja_templates-0.1.2.tar.gz.
File metadata
- Download URL: typed_jinja_templates-0.1.2.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf1cce7dcf13e4594e34fc016fd7e327e329dd5a67f545dab2a60df4b8c79401
|
|
| MD5 |
2c947ec187250fdcd337b1f5b61a1ee3
|
|
| BLAKE2b-256 |
48591fb56aa907593170f825be50dede3467a1b39668397d6fc59c9cd44247dc
|
File details
Details for the file typed_jinja_templates-0.1.2-py3-none-any.whl.
File metadata
- Download URL: typed_jinja_templates-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ceb8f42ef2e9d714e1b4243714516ab5c59ea7133f0f9e28f0f8cdbf63b0175
|
|
| MD5 |
b3019668e92566c21e18345ea58525bb
|
|
| BLAKE2b-256 |
d1d3b3df4c9ff2dc7fd3ed75fcaf81d2e4da4d85d9ea23df40cd05fd1dbf9c79
|