Repository navigation
Description
Problem
Reading a backend (underscore) var on the state class returns its Field descriptor, which has no frontend Var. 0.11 (#7651, follow-up of #7459 / #7732) turns most UI uses into a clear BackendVarFormatError naming the var and the fix ("cannot be used in the UI ... declare it as ClassVar[...] ..., or use a regular state var"). These uses still fail with a generic error that prints the field's default value and names neither the var nor a fix, or do not fail at all:
| Code | Result on 0.11 |
|---|---|
rx.text(State._secret) (child) |
ChildrenTypeError: Component Text received child Field(default='s3cr3t-default', is_var=True, annotated_type=<class 'str'>) of type <class '...Field'> |
rx.el.div(title=State._secret), rx.link(href=State._secret), add_page(meta=[{"content": State._secret}]) (typed props) |
TypeError: Invalid var passed for prop Div.title, expected type Var[str], got value Field(default=...) |
State.take(State._secret) (event argument) |
EventHandlerTypeError: Arguments to event handlers must be Vars or JSON-serializable. Got Field(default=...) |
rx.cond(State._secret == "x", a, b) |
compiles; State._secret == "x" is a plain Python False, so the page always shows b |
bool(State._secret) |
True |
app.add_page(index, on_load=State._secret) |
compiles, exit 0 |
The first three print the default value into the error (which can be a secret, given that backend vars are where people keep them) and the traceback usually has no frame in the user's file. The last three are silent wrong behaviour.
Repro
import reflex as rx
class State(rx.State):
_secret: str = "s3cr3t-default"
@rx.event
def take(self, value: str):
pass
def index():
return rx.vstack(
rx.text(State._secret), # cryptic ChildrenTypeError
# rx.el.div(title=State._secret), # cryptic prop TypeError
# rx.button(on_click=State.take(State._secret)), # cryptic EventHandlerTypeError
# rx.cond(State._secret == "x", rx.text("a"), rx.text("b")), # silently "b"
)
app = rx.App()
app.add_page(index)Expected
Every row raises the same BackendVarFormatError the other UI paths raise: it names State._secret, says a backend var has no frontend value, and suggests ClassVar / a regular state var; it does not print the default value.
Fix direction
- The existing error is built by
Field._no_frontend_var_error()(packages/reflex-base/src/reflex_base/vars/base.py), already used byComponent.get_refforid=. - Child validation (
ChildrenTypeErrorin the component child checks), prop type validation (the "Invalid var passed for prop" path inpackages/reflex-base/src/reflex_base/components/component.py), event argument conversion (EventHandlerTypeErrorpath inpackages/reflex-base/src/reflex_base/event/__init__.py) andon_loadnormalization inreflex/app.py: check for aFieldbefore the generic error and raise_no_frontend_var_error(). Field.__eq__/__ne__/__bool__: raising here is the only way to catch the silentrx.condcase.Fieldobjects are also compared and hashed internally (dataclass machinery, dict keys), so make the comparison raise only when the other operand is not aFieldand the field is a backend var accessed from the class, or restrict the check to the Var-building entry points (rx.cond,rx.match) instead. Pick whichever keeps internal uses working; measure that hashing and identity comparisons are unaffected.- Non-goals: no change to how backend vars behave on instances, or to
repr()(which deliberately still works). - Tests: extend the
BackendVarFormatErrorunit tests (they cover theLiteralVarpaths) with one case per row.
Not a regression: 0.10.1 behaves the same for these rows.
Activity
linear-code commentedon Nov 11, 2026
ENG-13991
ShaanGS commentedon Nov 15, 2026
I'd like to take this one. I've reproduced the rows on current main and will open a PR today.