Skip to content

fix(memo): preserve runtime typing for unannotated params#6659

Open
harsh21234i wants to merge 3 commits into
reflex-dev:mainfrom
harsh21234i:fix/memo-untyped-var-regression-6631
Open

fix(memo): preserve runtime typing for unannotated params#6659
harsh21234i wants to merge 3 commits into
reflex-dev:mainfrom
harsh21234i:fix/memo-untyped-var-regression-6631

Conversation

@harsh21234i

Copy link
Copy Markdown

Summary

Fixes #6631.

@rx.memo component bodies with unannotated parameters were regressing to Var[Any] in 0.9.4, which caused downstream UntypedVarError failures when the memo body
tried to access object fields or call var methods.

This change preserves the legacy behavior for missing annotations by seeding the memo body with the first call’s runtime value type before the component is
materialized. That keeps the deprecation warning in place, but avoids the confusing compile-time failure for existing call sites.

Testing

  • uv run pytest -o addopts='' tests/units/components/test_memo.py -q

@harsh21234i harsh21234i requested a review from a team as a code owner June 12, 2026 08:52
@greptile-apps

greptile-apps Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a regression introduced in 0.9.4 where @rx.memo components with unannotated parameters silently produced Var[Any] placeholders, causing UntypedVarError failures at compile time when the memo body tried to access fields or call var methods on those placeholders.

  • Introduces a _runtime_param_values dict on MemoComponentDefinition that is populated with the call-site argument values just before the lazy component body is first evaluated, allowing _var_placeholder to use type(runtime_value) (or runtime_value._var_type for Var inputs) instead of falling back to Any.
  • Converts _create_component_definition from an eager _LazyBody.ready(...) instantiation to a lazy _LazyBody(lambda: ...), which is necessary to defer body evaluation until _runtime_param_values is seeded.
  • Adds two new tests covering the plain-Python-object branch and the Var-input branch of the runtime type inference path.

Confidence Score: 5/5

Safe to merge; the change is self-contained and well-tested, with no impact on annotated params or non-memo code paths.

The core mechanism — seeding a captured dict reference before the _LazyBody thunk runs and clearing it in a finally block — is sound for single-threaded compile use (which _LazyBody already documents). The fix correctly falls back to Any when no runtime value is available, preserving legacy behavior where the type cannot be inferred. Both affected code paths are handled consistently.

No files require special attention; both changed files are straightforward.

Important Files Changed

Filename Overview
packages/reflex-base/src/reflex_base/components/memo.py Adds runtime-type inference for unannotated @rx.memo parameters by seeding a shared _runtime_param_values dict before each lazy body evaluation; changes _create_component_definition from eager to lazy evaluation, which is required by the fix but slightly alters error-surface timing for that code path
tests/units/components/test_memo.py Adds two new tests covering plain-dict and Var runtime value type inference for unannotated memo params, addressing the previously flagged missing coverage for the Var branch

Reviews (3): Last reviewed commit: "add runtime var type assertion" | Re-trigger Greptile

Comment thread tests/units/components/test_memo.py
Comment thread tests/units/components/test_memo.py
@codspeed-hq

codspeed-hq Bot commented Jun 12, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 26 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing harsh21234i:fix/memo-untyped-var-regression-6631 (4690ee5) with main (7562344)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@harsh21234i harsh21234i requested a review from masenf June 13, 2026 03:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@rx.memo with unannotated params silently degrades to Var[Any] in 0.9.4 → UntypedVarError at compile (regression from 0.9.3)

2 participants