Repository navigation
fix: raise BackendVarFormatError when a backend var is used in the UI - #7651
FarhanAliRaza merged 9 commits into
Conversation
Field.__format__ now raises an error built by Field._no_frontend_var_error, so other code paths that reject a Field can reuse the same message.
A backend var (or a mixin field) passed where a Var is built from a Python value, such as rx.Var.create([State._secret]), rx.toast(State._secret) or rx.image(src=State._secret), reached the generic fallthrough of LiteralVar.create and _get_all_var_data_without_creating_var_dispatch and failed with TypeError: Unsupported type ... for LiteralVar. Both now raise the same BackendVarFormatError that formatting the field raises.
[Medium impact] The PR appears safe to merge; no blocking issue was found. SummaryThe PR gives clearer errors when a backend field is used where a frontend value is needed.
No actionable new issues were found. No previous review threads were supplied. Reviews (6) · Last reviewed commit: "test: cover backend vars passed to the e..." · Reviewed by Greptile |
There was a problem hiding this comment.
All reported issues were addressed across 5 files
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
Merging this PR will not alter performance1 warning
Comparing |
Build the LiteralVar fallthrough error in one helper, so both dispatchers keep their previous complexity. Update the BackendVarFormatError docstring, which is now raised outside string formatting, and the 0.10 upgrade guide, which still quoted the old TypeError for a backend var used as a prop.
There was a problem hiding this comment.
All reported issues were addressed across 3 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
A backend var given as a component id, as on_upload_progress, or as page title, description or image failed with an unrelated TypeError, ValueError or ChildrenTypeError that did not name the var. These now raise BackendVarFormatError. Page metadata is checked in add_page, so the error points at the user's call. A state var's field read from get_fields() no longer claims to have no frontend var; the message says to use the var instead. Add a test for get_upload_url with a backend var.
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
# Conflicts: # packages/reflex-base/src/reflex_base/event/__init__.py
… str()
Field.__str__ now raises the same error as Field.__format__, so str(F),
"%s" % F and f"{F!s}" no longer put the Field repr in the page. The
error sites that used !s to keep their own error (class_name lists,
untyped Var item access, Button variant/size, event triggers) now raise
BackendVarFormatError, which names the var and the fix. repr() and !r
still give the Field text.
There was a problem hiding this comment.
All reported issues were addressed across 8 files (changes from recent commits).
Tip: Review your code locally with the cubic CLI to iterate faster.
View guided diff | Re-trigger cubic
…eck at runtime (#7818) * Point to repr() when a backend var field is printed in server code Since #7651, `str()` of a class-level backend var such as `State._secret` raises `BackendVarFormatError`, so `print()`, `%` formatting and logging of it in server code raise too, with a message that only talks about the UI. The error now ends with how to inspect the field there, and the unreleased #7651 changelog entry says that print and logging are affected. Found in the reflex 0.11.0a1 pre-release testing (FINDING-037). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * Skip the storage default_factory check on the runtime path The compile calls a str-compatible `default_factory` to reject one that produces browser storage. `_shared_storage_groups` reran the same compile step in every backend worker's first event, calling each such factory once more per worker. The runtime path now skips the factory call; the compile still checks. Found in the reflex 0.11.0a1 pre-release testing (FINDING-038). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi --------- Co-authored-by: Claude <[email protected]>
…eck at runtime (#7818) * Point to repr() when a backend var field is printed in server code Since #7651, `str()` of a class-level backend var such as `State._secret` raises `BackendVarFormatError`, so `print()`, `%` formatting and logging of it in server code raise too, with a message that only talks about the UI. The error now ends with how to inspect the field there, and the unreleased Found in the reflex 0.11.0a1 pre-release testing (FINDING-037). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * Skip the storage default_factory check on the runtime path The compile calls a str-compatible `default_factory` to reject one that produces browser storage. `_shared_storage_groups` reran the same compile step in every backend worker's first event, calling each such factory once more per worker. The runtime path now skips the factory call; the compile still checks. Found in the reflex 0.11.0a1 pre-release testing (FINDING-038). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi --------- Co-authored-by: Claude <[email protected]> (cherry picked from commit 10dd495)
All Submissions:
Type of change
Changes To Core Features:
Description
Class access to a field with no frontend Var (a backend var such as
State._secret, anis_var=Falsefield, or any field of a mixin state) returns the rawField. When thatFieldis passed where a Var gets built from a Python value, it reaches the end ofLiteralVar.create(_create_literal_var), or of its var-data twin_get_all_var_data_without_creating_var_dispatch. Neither recognises aField, so both fall through to the genericTypeError: Unsupported type ... for LiteralVar. That coversrx.Var.create([F]),rx.Var.create({"a": F}),rx.toast(F),rx.console_log(F),rx.image(src=F)andrx.data_table(data=F)from the issue. Formatting the same field withf"{F}"already raises a clearBackendVarFormatErrorfromField.__format__.This PR follows the proposal in the issue and gives the
LiteralVarpath the same wording:Field.__format__now builds its message in a small helper,Field._no_frontend_var_error(). The text is unchanged.LiteralVarfallthroughs checkisinstance(value, Field)right before the genericTypeErrorand raise that error. The check runs only on the path that was about to fail, so supported values pay nothing.BackendVarFormatErroris aVarTypeError, which is aTypeError, soexcept TypeErrorcode still catches it.test_backend_field_literal_var_reports_reprasserted the old generic message, so it is replaced by tests that expectBackendVarFormatError.Before / After (
F = S._secret,Sa state with_secret: str = "x"):The error still comes up where the Var is built (stringified or rendered), not at the
rx.toast(...)/rx.image(...)call. That is the same place as before, but now with the explanation.Other paths (#7732)
Later commits give the same error to the paths that do not go through
LiteralVar:rx.box(id=F)renderedTypeError: expected string or bytes-like object, got 'Field'BackendVarFormatErrorfromComponent.get_refapp.add_page(index, title=F)(alsodescription,image)ChildrenTypeErrorat compile time, with no frame from the app fileBackendVarFormatErrorfromadd_page, at the user's linerx.upload_files(upload_id="u", on_upload_progress=F)bound to a handlerValueError: Field(...) is not a valid event handler.BackendVarFormatErrorstr(F) + "px","%s" % F,f"{F!s}"Field(default=...)px.BackendVarFormatErrorfrom the newField.__str__f"{S.get_fields()['shown']}"(the field of a regular var)'S.shown' has no frontend var(false)'S.shown' is the field of a state var, not the var. Use S.shown to use it in the UI.Field.__str__also changes the error at the sites that used!sto show the field's repr: aclass_namelist, untyped Var item access, the internalButtonvariant and size checks, and event triggers (Invalid event chain/Invalid event). These sites now raiseBackendVarFormatError, which names the var and the fix.print(State._x)now raises, andloggingwith%sprints a logging error.repr()and!rstill give theFieldtext. The 0.10 upgrade guide is updated to match.Not covered: a typed prop such as
rx.heading(size=F).Component._post_initcatches theTypeErrorand raisesInvalid var passed for prop ... got value Field(...).Tests:
tests/units/reflex_base/vars/test_base.py:LiteralVar.create(F),str(Var.create([F])),str(Var.create({"a": F})), and_get_all_var_data()on the list and dict literals (this reaches the var-data twin). Plus a mixin field case for the mixin wording.tests/units/test_event.py:rx.toast(F)andrx.console_log(F).tests/units/components/test_component.py:rx.image(src=F)andrx.data_table(data=F)rendered.With
vars/base.pyreverted tomain, all 10 new cases fail with the genericTypeError: Unsupported type <class 'reflex_base.vars.base.Field'> for LiteralVar(8 raised at base.py:1932, 2 at :2011). With the fix they pass.Tests for the other paths:
tests/units/reflex_base/vars/test_base.py:f"{F}",f"{F!s}",str(F)and"%s" % F; the field of a regular var fromget_fields(); untyped Var item access.tests/units/test_event.py:on_upload_progress=F, andFas an event trigger.tests/units/components/test_component.py:rx.box(id=F)rendered, andFin aclass_namelist.tests/units/test_app.py:add_pagewithtitle,descriptionorimageset toF.tests/units/components/core/test_upload.py:rx.get_upload_url(F).tests/units/experimental/test_hooks.py:Fpassed toconst,useCallback,useContext,useRefanduseState.tests/units/reflex_components_internal/components/base/test_button.py:Fas aButtonvariant or size.At
224db98cc:The pre-commit hooks (
ruff format,ruff check,codespell,update-pyi-files,pyright,ty) pass.reflex-release changelog-checkandcheck-headingspass with the news fragmentspackages/reflex-base/news/+backend-var-literal-var.bugfix.mdandnews/+backend-var-page-meta.bugfix.md.Closes #7459
Closes #7732