Typing Stubs for Chevron

0.14.2.20260408 · active · verified Thu Apr 16

types-chevron is a PEP 561 type stub package that provides static type annotations for the `chevron` templating library. It is part of the `typeshed` project, the central repository for third-party library stubs, and is used by static analysis tools like mypy, pyright, and PyCharm to enable type checking and autocompletion for code using `chevron`. This package specifically targets type annotations for `chevron` version `0.14.*`. It is actively maintained with daily releases from typeshed.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic usage of the `chevron` library, for which `types-chevron` provides static type information. The `types-chevron` package is automatically used by type checkers (like mypy or Pyright) and IDEs to provide type hints, autocompletion, and error detection without requiring direct imports of `types-chevron` itself. The example includes both string and file-based template rendering.

import chevron
from typing import Dict, Any

def render_template(template_str: str, data: Dict[str, Any]) -> str:
    """Renders a Mustache template using chevron."""
    rendered = chevron.render(template=template_str, data=data)
    return rendered

# Example 1: Basic string rendering
template_string = "Hello, {{ name }}!"
context_data = {'name': 'World'}
output = render_template(template_string, context_data)
print(f"Rendered output (string): {output}")

# Example 2: Rendering from a file (create a dummy file first)
with open("my_template.mustache", "w") as f:
    f.write("The secret number is {{ number }}.")

with open('my_template.mustache', 'r') as f:
    file_context_data = {'number': 42}
    file_output = render_template(template_str=f, data=file_context_data)
print(f"Rendered output (file): {file_output}")

# Type checking benefits:
# If 'number' was expected to be an int but a str was passed,
# a type checker (e.g., Mypy, Pyright) would flag a potential issue
# even though Python itself would run the code.
# Example: render_template(template_string, {'name': 123}) # A type checker might warn here.

view raw JSON →