Skip to content

Backend var used as a child, typed prop or event argument still gives a cryptic error, and State._x == ... is silently False #7843

Open

Description

@masenf

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 by Component.get_ref for id=.
  • Child validation (ChildrenTypeError in the component child checks), prop type validation (the "Invalid var passed for prop" path in packages/reflex-base/src/reflex_base/components/component.py), event argument conversion (EventHandlerTypeError path in packages/reflex-base/src/reflex_base/event/__init__.py) and on_load normalization in reflex/app.py: check for a Field before the generic error and raise _no_frontend_var_error().
  • Field.__eq__ / __ne__ / __bool__: raising here is the only way to catch the silent rx.cond case. Field objects are also compared and hashed internally (dataclass machinery, dict keys), so make the comparison raise only when the other operand is not a Field and 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 BackendVarFormatError unit tests (they cover the LiteralVar paths) with one case per row.

Not a regression: 0.10.1 behaves the same for these rows.

Activity

  1. added
    bugSomething isn't working
    release-trainIssues found during agentic /prerelease-testing fanout, not necessarily validated bugs.
    on Nov 11, 2026
  2. linear-code commented on Nov 11, 2026

    @linear-code
  3. ShaanGS commented on Nov 15, 2026

    @ShaanGS
    Contributor

    I'd like to take this one. I've reproduced the rows on current main and will open a PR today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingrelease-trainIssues found during agentic /prerelease-testing fanout, not necessarily validated bugs.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions