Typing Stubs for Chevron
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
-
ModuleNotFoundError: No module named 'chevron'
cause You have installed `types-chevron` but not the actual `chevron` runtime library. `types-chevron` only provides type hints and does not include the functional code.fixInstall the `chevron` library: `pip install chevron`. -
error: Library "chevron" has no attribute "render" [attr-defined] (or similar type checker error)
cause Your type checker (e.g., Mypy, Pyright) is unable to find the correct type information for `chevron.render`. This can be caused by a version incompatibility between `chevron` and `types-chevron`, or an issue with the type checker resolving the stub package.fix1. Ensure both `chevron` and `types-chevron` are installed. 2. Verify that your `chevron` version is compatible with the `types-chevron` version you have installed (e.g., `types-chevron` provides stubs for `chevron==0.14.*`). If not, adjust versions to a compatible set. 3. Check your type checker's configuration to ensure it is correctly discovering and using installed stub packages.
Warnings
- gotcha The `types-chevron` package aims to provide accurate annotations for `chevron==0.14.*`. Using a `chevron` version significantly different from this target might lead to inaccurate or missing type hints, potentially causing false positives or negatives in your static type checks.
- gotcha `types-chevron` is a stub-only package and contains no runtime code. Installing it without the actual `chevron` library will not provide any templating functionality, only type information for a non-existent runtime dependency, leading to `ModuleNotFoundError` at runtime if `chevron` itself is not installed.
- breaking Due to the nature of type stubs, any version bump of `types-chevron` can introduce changes that might make your code fail to type check, even if the underlying `chevron` runtime library has not changed. This is because stubs define the expected interface for static analysis, and changes to these definitions can affect type checker output.
Install
-
pip install chevron types-chevron
Imports
- chevron
import chevron
Quickstart
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.