Compatibility
chameleon-rs’s goal is to produce exactly what Chameleon produces, for every template, and to raise the same errors. This page says how that’s measured, and lists the few places where chameleon-rs knowingly differs.
“Chameleon” here is its master branch as of 2026-11-24: 4.6.1 plus three fixes merged since, which its next release will have. Where that differs from the released 4.6.1, chameleon-rs follows master: see Ahead of Chameleon 4.6.1.
How it’s measured
Each level is a check that runs on every change:
| Check | Result |
|---|---|
| Chameleon’s own test suite. All 244 tests, vendored unmodified, run against chameleon-rs. | Every test that passes on Chameleon passes on chameleon-rs: 238 of 238. The other 6 depend on the test environment and are skipped on both engines. |
| Fixture renders. Every input template in Chameleon’s test suite, rendered by both engines. | All 122 byte-identical. |
| Differential fuzzing. Generated templates, models and expressions, rendered by both engines and compared. | No differences in 20,000 examples per generator. |
Integration suites. The test suites of fastapi-chameleon, chameleon-flask, chameleon-robyn, chameleon_partials, litestar-chameleon, pyramid_chameleon, z3c.pt (Zope and Plone’s page templates) and Zope 6.3 (Products.PageTemplates, Products.Five), unmodified, on chameleon-rs. |
All pass. |
| Real sites. Every page of two production Flask/Quart apps, crawled with both engines: anonymous and signed in, with all their partials and layouts. | Every page byte-identical. |
The upstream suite and the fuzzers compare output byte for byte, and they also compare the type and message of any error a template raises.
What’s identical
- The template language: TAL, METAL, i18n,
${...}, every expression type, and whitespace and attribute handling, down to the byte. - The Python API: the template classes and their options, PageTemplateLoader, macros,
translatefunctions,tal:on-errorhandlers, andCHAMELEON_*environment variables. - The modules: chameleon_rs.compat.install() maps each Chameleon module to the chameleon-rs module of the same name.
- Errors: the same exception types, and the same formatted messages with the template position and expression.
- The helper libraries: the APIs of chameleon_partials, chameleon-flask, fastapi-chameleon and chameleon-robyn, built in.
Known differences
These are deliberate. Each comes from chameleon-rs compiling to a native program rather than to Python source code.
| Area | Chameleon | chameleon-rs |
|---|---|---|
template.source |
The generated Python module’s code. | With keep_source=True, a readable dump of the compiled program. It isn’t Python. |
| Custom expression types | A class whose compiler returns Python AST statements. | Supported through a bridge that wraps the AST in a Python function, at the speed of an ordinary Python call. Subclasses of Chameleon’s own types (PythonExpr, ExistsExpr, …, as z3c.pt has) work the same way. Types that depend on Chameleon’s compiler internals beyond that interface aren’t supported. See Custom expression types. |
CHAMELEON_CACHE |
Writes the generated modules to disk and loads them on the next start. | Checks the directory but writes nothing. Compiling is about 70x faster than Chameleon, so there’s little to cache. |
| Code-generation modules | chameleon.nodes, chameleon.program, chameleon.zpt.program, chameleon.benchmark. |
Not provided, since chameleon-rs doesn’t generate code. Importing them raises ModuleNotFoundError. |
Code blocks
Chameleon compiles <?python ... ?> blocks into the template’s render function, so what a block defines is a local of that call. chameleon-rs runs a block in a namespace kept per template and render, which differs in two ways:
- Recursive partials share it. Two frames of the same template in one render (a partial that renders itself) see each other’s block definitions, where each Chameleon call has its own.
defaultandattrsinside a block are ordinary names. Chameleon aliases them there as in expressions (the default marker and the element’s attributes).
render_partial in econtext
With chameleon_partials, render_partial is a model value, so it shows up in the template’s econtext and in the argument list of a render error. The built-in render_partial is a builtin of every template, so it is not in either.
Corner cases in slot bodies
Two cases, both involving the slot fills of a macro call together with tal:on-error or i18n settings, render differently. No real template we’ve seen does this; each is pinned by a test that fails if the behaviour changes.
- A handler on an
i18n:nameelement whose content calls a macro that fails partway. Chameleon writes a text node and its expressions as one piece, so when an expression fails, the text before it in the same node is dropped with it. chameleon-rs writes that text first, so it stays. - A fill no macro took, used by a later macro call while the calling template has different
i18n:domain,i18n:contextori18n:targetsettings: values converted by that fill use the settings where it was defined. Chameleon uses the settings at the later call.
Behaviour of the helper libraries
The built-ins differ from chameleon_partials, chameleon-flask, fastapi-chameleon and chameleon-robyn in a few ways that don’t affect templates that worked before:
- Partial paths. A path that isn’t in the configured template folder is also looked for next to the calling template, where chameleon_partials would raise.
configure(partial_search='registered')restores the original lookup. See Partials. - Async views await model values. An async view renders with render_async, so a model value that is awaitable (a coroutine, task or future, or a test double such as
AsyncMock()) is awaited where the template uses it. The original libraries rendered the object itself. - Setup. Every configure() call applies (register_extensions ignored later calls), and
configure(..., app=app)checks the decorator order at startup.
render_async and a replaced render
render_async() honours a render() that a subclass overrides, since the override may add defaults or post-process the output: it calls it, synchronously, so awaitable values are not awaited. Assigning a new function to PageTemplate.render (a monkeypatch, for example to time renders) counts as such an override. Every render_async(), including the async views of the framework integrations, then renders synchronously through your function. See Migrating for a way to time both.
Chameleon bugs fixed
Where Chameleon’s output is clearly broken, chameleon-rs writes what the template means. Most are Chameleon issues that chameleon-rs adopted.
| Template | Chameleon | chameleon-rs |
|---|---|---|
A valueless attribute given a value: <input disabled tal:attributes="disabled True"> or <img alt i18n:attributes="alt"> (#429) |
<input disableddisabled>, <img altAlt> |
<input disabled="disabled">, <img alt="Alt"> |
| A loop inside a loop of the same name, as in a recursive macro (#177) | After the inner loop ends, repeat.item is still the inner loop’s, so the outer loop’s next items get its index and length (often -1 and 0). |
repeat.item is the outer loop’s again. |
| A local variable with the same name as a global, then a macro call inside it (#445) | The macro call resets the local to the global’s value. | The local keeps its value, unless the macro sets the global. |
An error inside a metal:fill-slot body (#447, #291, #259) |
The error message points at the page’s metal:use-macro, often after the macro’s last expression, which didn’t fail. |
The message leads with the fill’s failing expression, then the metal:use-macro. |
A <?python ?> block that binds a name with except ... as, class, async def or a match capture |
Reading the name raises NameError (or reads render data of the same name). |
The name is the one the block bound. |
An assignment in a class body in a <?python ?> block: class C: v = 3 |
Stored in the template’s scope like any assignment: ${v} is 3 and C.v raises AttributeError, so a dataclass has no fields. |
The class’s attribute, as in Python: C.v is 3. A template that read ${v} afterwards must read ${C.v}. |
A type statement in a <?python ?> block: type Pair[T] = tuple[T, T] |
SyntaxError. |
The alias is a name of the template, as a def is. |
nonlocal in a <?python ?> block: a counter that a nested function increments |
SyntaxError: no binding for nonlocal, because the enclosing function’s assignment is stored in the template’s scope. |
The name is a local of the function that binds it, as in Python. |
A translation or an __html__ method that returns something other than a string, inside tal:on-error |
The TypeError is raised when the output is joined, after the handler has been left, so the render fails. |
The error is raised at the element, and the handler’s fallback is rendered. |
A tal:attributes dictionary whose value translates to None |
TypeError: can only concatenate str (not "NoneType") to str. |
The attribute is written with an empty value. |
Ahead of Chameleon 4.6.1
These are fixed on Chameleon’s master and not yet in a release. chameleon-rs behaves as master does, so a site moving from Chameleon 4.6.1 sees them change:
| Template | Chameleon 4.6.1 | Chameleon master and chameleon-rs |
|---|---|---|
A macro that calls itself without end inside tal:on-error (#462) |
Renders until the stack runs out, then the handler’s fallback: output that depends on how deep the caller is. | RecursionError. tal:on-error handles no RecursionError, from a partial either. |
A slot fill whose metal:define-slot is inside i18n:translate or i18n:name (#466) |
The fill comes out before the translated text. | The fill comes out where the slot is. |
A default that reads a name: lambda a=a: a, def f(a=a) in a <?python ?> block (#468) |
NameError. |
The default is the template’s a. |
Found a difference?
If a template renders differently on chameleon-rs than on Chameleon, that’s a bug. Please open an issue with the template, the model, and both outputs. Shadow mode can find such templates in a running app.