Repository navigation
fix: raise BackendVarFormatError for the remaining backend var UI uses #7908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ff1e31f
b042ce2
6b6244c
b82ca39
777e5aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -241,7 +241,7 @@ def serialize_point(point: Point) -> dict: | |||||
|
|
||||||
| ## Printing a backend var on a state class | ||||||
|
|
||||||
| A backend var read on the state class, such as `State._secret`, is a `Field` since 0.10. In 0.11, `print()`, logging and `str()` of it raise `BackendVarFormatError`, as f-strings already did ([#7651](https://github.com/reflex-dev/reflex/pull/7651)). The same error replaces the `TypeError` or `ValueError` raised when the var is passed where a frontend value is needed, such as a component prop, `rx.toast(State._secret)`, or the `title`, `description` or `image` of `app.add_page(...)`. | ||||||
| A backend var read on the state class, such as `State._secret`, is a `Field` since 0.10. In 0.11, `print()`, logging and `str()` of it raise `BackendVarFormatError`, as f-strings already did ([#7651](https://github.com/reflex-dev/reflex/pull/7651)). The same error replaces the `TypeError` or `ValueError` raised when the var is passed where a frontend value is needed, such as a component child or prop, an event handler argument, `rx.toast(State._secret)`, or the `title`, `description`, `image` or `on_load` of `app.add_page(...)`. Comparing it with a value, as in `rx.cond(State._secret == "x", ...)`, or truth-testing it also raises, where it used to give a plain `False` or `True`. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: Logging does not raise here. With %-style arguments, the stdlib logging handler catches the error and prints a Prompt for AI agents
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logging wording was already there before this PR, which only adds the sentences after it, so I've left it to keep this PR to #7843. |
||||||
|
|
||||||
| ```python | ||||||
| class State(rx.State): | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| `app.add_page(...)` now raises `BackendVarFormatError` when `on_load` is a backend var such as `State._handler`, instead of accepting it. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Using a backend var such as `State._secret` as a component child, a typed prop (`rx.el.div(title=State._secret)`, `rx.link(href=State._secret)`) or an event handler argument now raises `BackendVarFormatError`, which names the var and the fix. A child used to raise a `ChildrenTypeError` that named neither, and a typed prop or event argument a generic error that printed the var's default value. Comparing a backend var or a mixin's field with a value (`State._secret == "x"`) or truth-testing it now raises the same error, instead of a plain `False` or `True` that made `rx.cond` always take one branch. Fields still compare equal to themselves. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| """Expected messages of the error for a field with no frontend var.""" | ||
|
|
||
| _REPR_HINT = " To print or log the field itself in server code, use repr()." | ||
|
|
||
|
|
||
| def backend_var_error(path: str) -> str: | ||
| """Build the full error message for using a backend var in the UI. | ||
|
|
||
| Args: | ||
| path: The backend var as ``State._name``. | ||
|
|
||
| Returns: | ||
| The complete error message. | ||
| """ | ||
| return ( | ||
| f"Backend var '{path}' exists only on the server and has no frontend" | ||
| f" value, so it cannot be used in the UI. Use {path}.default_value() for" | ||
| " its default value, declare it as ClassVar[...] for a constant shared by" | ||
| " all sessions, or use a regular state var for a value the UI should show" | ||
| " and update." + _REPR_HINT | ||
| ) | ||
|
|
||
|
|
||
| def mixin_var_error(path: str) -> str: | ||
| """Build the full error message for using a mixin's field in the UI. | ||
|
|
||
| Args: | ||
| path: The field as ``Mixin.name``. | ||
|
|
||
| Returns: | ||
| The complete error message. | ||
| """ | ||
| return ( | ||
| f"Var '{path}' is declared on a mixin state, which has no frontend vars." | ||
| " Access it through a state that includes the mixin instead." + _REPR_HINT | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: This paragraph documents the
str()/f-string raising as 0.10 behavior, but the 0.11 guide and the v0.11.0 changelog put it in 0.11, and v0.10.1 does not mention it. Move thestr()/f-string sentence to the 0.11 guide only, or correct the version it names.Prompt for AI agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
str()and f-string sentence was already in this paragraph before this PR. This PR only rewrites the part about children and props, so I've left that sentence as it is.