Repository navigation
feat: typed access to dynamic route args with rx.route_arg - #7943
benedikt-bartscher wants to merge 6 commits into
Conversation
Adding a page at `/post/[pid]` installs `rx.State.pid` at runtime, so type
checkers do not know the attribute and code using it needs an ignore.
`rx.State.route_arg("pid")` returns the same var as a `StringVar`, and
`rx.State.route_arg("splat")` the one of the `[[...splat]]` catch-all as
an `ArrayVar[list[str]]`. An arg no added page declares raises
DynamicRouteArgNotFoundError.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
[Medium impact] The PR appears safe to merge; no actionable new issues were found. SummaryAdds typed access to dynamic route arguments through
Reviews (6) · Last reviewed commit: "Replace the route arg classmethods with ..." · Reviewed by Greptile |
…full errors The name alone does not decide a route arg's type: `/post/[splat]` is a regular arg named `splat`, so `route_arg(Literal["splat"])` promised an ArrayVar for a StringVar, and `route_arg(name: str)` a StringVar for the catch-all when called with a `str` that is "splat". `route_arg(name)` is now the regular arg as a StringVar and `route_catch_all()` the `[[...splat]]` catch-all as an ArrayVar[list[str]]; each checks the installed arg is of its kind. reflex imports DynamicRouteArgNotFoundError from the unreleased reflex-base, so its floor is reflex-base >= 0.11.1.dev0. The tests compare the complete error messages. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merging this PR will degrade performance by 6.43%2 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
There was a problem hiding this comment.
All reported issues were addressed across 8 files
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
About the CodSpeed failure: this looks like runner noise rather than this change. Details:
Could a maintainer acknowledge it on CodSpeed? |
…ch_all
`rx.State.route_arg` and `rx.State.route_catch_all` made both names
framework members of every state: a state var or computed var named
`route_arg`, an event handler named `route_catch_all`, or a page at
`/x/[route_arg]` worked before and raised once they existed. The
accessors are now the module-level functions `rx.route_arg(name)` and
`rx.route_catch_all()`, resolved against `rx.State`, the only root state
an app can have. They reserve no names.
Their docstrings and the docs say that they return a var for building
components, not the value in an event handler or a computed var. The
docs show `rx.route_arg("pid")` in the `/post/[pid]` example, and the
catch-all accessor moves to the Catch-All Routes section with an
`rx.foreach(rx.route_catch_all(), rx.text)` example.
The errors say what is installed and point to the other accessor.
`_route_arg_type` classifies an installed route var for these and for
`_dynamic_route_arg_types`, and `RouteRegex.SPLAT_ARG` names the
catch-all arg for them and for `get_route_args`.
The tests move to test_state.py, next to the code. They resolve the
accessors against a fresh root state rather than `rx.State` or the shared
`EmptyState`, compare the vars with `equals`, and cover a substate, an
optional `[[lang]]` arg, the errors, and a state that declares
`route_arg` and `route_catch_all`.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
1 issue found across 11 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="tests/units/test_state.py">
<violation number="1" location="tests/units/test_state.py:4478">
P2: This fixture restores only `reflex.state.State`, but each test registers `RouteArgRoot` (and the first test registers `RouteArgSubState`) in shared state registries that survive fixture teardown. Later tests therefore see stale route-test classes, making the suite order-dependent; isolate these tests with a registration context and restore the shared state registries and related caches in teardown.
(Based on your team's feedback about restoring shared State registries.) .</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
| class RouteArgRoot(BaseState): | ||
| """Root state standing in for `rx.State`.""" | ||
|
|
||
| monkeypatch.setattr("reflex.state.State", RouteArgRoot) |
There was a problem hiding this comment.
P2: This fixture restores only reflex.state.State, but each test registers RouteArgRoot (and the first test registers RouteArgSubState) in shared state registries that survive fixture teardown. Later tests therefore see stale route-test classes, making the suite order-dependent; isolate these tests with a registration context and restore the shared state registries and related caches in teardown.
(Based on your team's feedback about restoring shared State registries.) .
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At tests/units/test_state.py, line 4478:
<comment>This fixture restores only `reflex.state.State`, but each test registers `RouteArgRoot` (and the first test registers `RouteArgSubState`) in shared state registries that survive fixture teardown. Later tests therefore see stale route-test classes, making the suite order-dependent; isolate these tests with a registration context and restore the shared state registries and related caches in teardown.
(Based on your team's feedback about restoring shared State registries.) .</comment>
<file context>
@@ -4455,6 +4458,125 @@ def test_router_field_names_are_reserved(name):
+ class RouteArgRoot(BaseState):
+ """Root state standing in for `rx.State`."""
+
+ monkeypatch.setattr("reflex.state.State", RouteArgRoot)
+ return RouteArgRoot
+
</file context>
Adding a page at
/post/[pid]installsrx.State.pidat runtime (State.setup_dynamic_args). Type checkers can't see that attribute, so code that reads a dynamic route arg the documented way needs an ignore:There is currently no typed alternative.
router.page.paramsis deprecated and only typed asAnypast.params, androuter.urlhas no path params.This PR adds two functions that return the same vars, typed:
A regular arg and the catch-all get separate functions because the name alone doesn't decide the type:
/post/[splat]is a regular arg namedsplat. Each function checks that the installed arg is of its kind.DynamicRouteArgNotFoundError. It is anAttributeError, like the attribute access it replaces, and its message says what is installed and points to the other function.rx.State.pid, the arg has to be installed when it is accessed.Why module-level functions. An earlier version of this PR added them as classmethods on
BaseState. That maderoute_argandroute_catch_allframework members of every state. A state var or computed var namedroute_arg, an event handler namedroute_catch_all, or a page at/x/[route_arg]all work onmain, and all would have raised. Module-level functions reserve no names. They resolve againstrx.State, the only root state an app can have outside tests.A
_route_arg_typehelper now classifies installed route vars for these functions and for_dynamic_route_arg_types. A newRouteRegex.SPLAT_ARGconstant names the catch-all arg for them and forget_route_args. reflex-base gets the exception and the constant; reflex's existing floorreflex-base >= 0.11.1.dev0covers both.Tests
tests/units/test_state.pyresolves the functions against a fresh root state, so no route args leak ontorx.State. It compares vars withequalsand covers:[[lang]]arg;[splat];route_argandroute_catch_all, and a page at/x/[route_arg].tests/type_checking/state.pyasserts the types; it passes under thetyhook, ty 0.0.107, andpyright-type-checking-3.11/3.14.reflexandreflex-basecover the new functions.All pre-commit hooks, full-repo pyright, the docs app tests and
reflex-release changelog-checkpass.🤖 Generated with Claude Code