Repository navigation
Labels
bugSomething isn't working
Description
Version
citry 0.4.6, citry-core 1.6.1
What happened?
A component reads a Kwargs value and passes it to <c-element c-is="...">.
Calling the component from Python works. Rendering the same component from a
template raises:
TypeError: expected string or bytes-like object, got '_ConstProxy'
The traceback ends in citry/components/dynamic.py:
def _validate_tag_name(tag: Any) -> None:
if not tag or not isinstance(tag, str):
msg = f"<c-element> requires an 'is' value naming the HTML tag, got {type(tag).__name__}."
raise TypeError(msg)
if not _VALID_NAME_RE.fullmatch(tag): # <- raises hereThe value is a _ConstProxy. It passes isinstance(tag, str), so the guard on
the line above does not catch it, but re.fullmatch rejects it.
Const is documented as "a transparent marker that a value is constant across
renders", and const_value() returns the underlying 'span':
>>> is_const(value)
True
>>> const_value(value)
'span'
>>> re.fullmatch(r"[a-z]+", const_value(value))
<re.Match object; span=(0, 4), match='span'>I expected rendering from a template to behave the same as calling the
component from Python.
Which forms trigger it:
| call | result |
|---|---|
Heading(as_="span") from Python |
works |
<c-heading as_="span" /> |
TypeError |
<c-heading c-as_="'span'" /> |
TypeError |
<c-heading c-as_="tag" /> with tag a render variable |
works |
<c-heading c-bind="d" /> with d = {"as_": "span"} |
works |
The failing cases are the ones where the value is constant-foldable.
Minimal reproduction
from citry import Citry, Component
app = Citry(autodiscover=False)
class Heading(Component):
citry = app
class Kwargs:
as_: str = "div"
def template_data(self, kwargs, slots):
return {"tag": kwargs.as_}
template = '<c-element c-is="tag">hello</c-element>'
print(str(Heading(as_="span")))
print(app.render_template('<c-heading as_="span" />', {}).serialize())
Output:
<span data-cid-cnjt5sc1u="">hello</span>
Traceback (most recent call last):
...
File ".../citry/components/dynamic.py", line 198, in _validate_tag_name
if not _VALID_NAME_RE.fullmatch(tag):
TypeError: An error occurred while rendering components TemplateRoot > Heading > DynamicElement:
expected string or bytes-like object, got '_ConstProxy'Environment
Python 3.12.9, macOS 15.3.2
Metadata
Metadata
Assignees
Labels
bugSomething isn't working
Activity
JuroOravec commentedon Sep 9, 2026
Hey Joey, thanks for reporting! I'm fixing it now, will release 0.5.0 with the fix later today.
JuroOravec commentedon Sep 10, 2026
Now fixed in v0.5.0 🎉
joeyjurjens commentedon Sep 10, 2026
Still some issues depending on python version
joeyjurjens commentedon Sep 11, 2026
@JuroOravec I'm still having some issues regarding this:
The 0.5.0 fix covers the path where citry itself consumes a marked value - this now works, where 0.4.6 raised
TypeError: ... got '_ConstProxy':But the marker still reaches
template_data, so component code gets it.Note the
c-, sofluidreally is the booleanTrue, not the string. No exception - just wrong output. Five components in my library render this way, and it took a while to find becauseisinstance(x, str)isTrueandstr(x),==andbool(x)all behave.It also raises. citry-phosphor-icons passes a kwarg to a path lookup:
Whether you get an exception or silence depends on whether the callee coerces.
django.utils.text.slugifystarts withvalue = str(value)and survives;re.sub,"".joinandos.fspathdo not. All of this behaves identically on 0.4.6 and 0.5.0.Workaround, which fixed it for me (for now).
This does break cache, cause citry can no longer tell the input was constant and potentially it breaks other things as well, not sure xD
JuroOravec commentedon Sep 11, 2026
Looking into this. This issue relates to the
Const()feature, which is for optimising rendering by telling Citry which inputs never change and can be reused across renders.Three relevant parts of the
Const()feature is that:<c-MyCard c-disabled="True" />, then Citry knows it's a literal PythonTrue, behind the scenes converts it to<c-MyCard c-disabled="Const(True)" />Const()should create an invisible proxyConst()values travel down the component tree just like regular values. This way, there's multiple places that can be optimized if a constant value lands there.However, @joeyjurjens what you discovered is that the proxying logic isn't working correctly, two issues:
iskeyword (value is True) bypasses the proxy logic, and sees_ConstProxyinstead of the underlying value.TypeError: expected str, bytes or os.PathLike object, not _ConstProxyYou've hit those bugs because Citry was passing those proxies directly to
template_data()and other methods. But this is a footgun. So instead,template_data()will receive the plain unwrapped values.So before:
Afterwards, reexported template variables will no longer keep their proxies:
This is something that will have to be fixed upstream either in wrapt or in Python itself. So I opened #124 to track that.
Note: Turning off the proxies adds about 25% to the overall render time, which is pretty bad. So I'm trying a smart approach where the inputs to methods like
template_data()are unwrapped, but if individual results fromtemplate_data()match what was originally passed to the component, then I re-wrap it. I'm yet to see if that helps with perf.joeyjurjens commentedon Sep 11, 2026
@JuroOravec Thanks, that plan matches what I did on my side as a temporary fix, so it sounds right to me.
Two things.
It is not only
is True.is Nonebreaks the same way, and it is much more common in real code. My Bootstrap port does 40 identity tests on its inputs, spread over 16 components, and 33 of them areis not None.Where you unwrap decides what gets fixed. If you unwrap at the
template_data()call, thenself.kwargs,js_data()andon_render()still get proxies, and so does the typedKwargs. That last one matters, because a PydanticKwargswith astrfield cannot render from a template at all right now:Only fields typed
strare affected, andstr | Nonetoo.int,float,bool,list,dictandLiteralall pass, because Pydantic either converts the value or compares it. That is why theage: intexample in the inputs and validation docs works.Unwrapping before
cls.Kwargs(**raw_kwargs)in_finalize_inputs()would cover all of it in one place, I think.This is the extension I use for now. It keeps the
Const()optimisation:extract_const_vars()sees the same variables with it as without.I match on identity, not on equality, so a value that only compares equal to an input is not wrapped again.
I'm not sure about the performance penalty and whether above introduces other bugs I haven't noticed yet.