reflex-dev/reflex · error · TypeError
Expected return type of {fn.__name__} to be a Var, got {orig
Error message
Expected return type of {fn.__name__} to be a Var, got {origin}. What it means
Raised by the dispatcher-registration helper when a function annotated to overload Var operators does not return a Var (or a generic Var[...]). The decorator validates return annotations to build the operator dispatch table.
Source
Thrown at packages/reflex-base/src/reflex_base/vars/base.py:3361
Args:
fn: The function to register.
Returns:
The decorator.
Raises:
TypeError: If the return type of the function is not a Var.
TypeError: If the Var return type does not have a generic type.
ValueError: If a function for the generic type is already registered.
"""
types = get_type_hints(fn)
return_type = types["return"]
origin = get_origin(return_type)
if origin is not Var:
msg = f"Expected return type of {fn.__name__} to be a Var, got {origin}."
raise TypeError(msg)
generic_args = get_args(return_type)
if not generic_args:
msg = f"Expected Var return type of {fn.__name__} to have a generic type."
raise TypeError(msg)
generic_type = get_origin(generic_args[0]) or generic_args[0]
if generic_type in dispatchers:
msg = f"Function for {generic_type} already registered."
raise ValueError(msg)
dispatchers[generic_type] = fn
return fn
View on GitHub (pinned to 45b8ed5ab7)
Solutions
- Annotate the return type as Var[SomeType], e.g. def fn(x: Var[int]) -> Var[int]: ...
- If you don't need a custom dispatch, use existing operators instead of the registration API
Example fix
# before
def sqrt_op(v: Var[float]):
return v.to_operator('sqrt')
# after
def sqrt_op(v: Var[float]) -> Var[float]:
return v.to_operator('sqrt') Defensive patterns
Strategy: type-guard
Validate before calling
from typing import get_origin, get_args
from reflex.vars import Var
origin = get_origin(fn.__annotations__['return'])
assert origin is Var, f'return must be Var[...], got {origin}' Type guard
def returns_var(fn) -> bool:
from typing import get_origin
from reflex.vars import Var
return get_origin(fn.__annotations__.get('return')) is Var Prevention
- Annotate every custom operator function with Var[...] return types
- Type-check extension modules with pyright before runtime
When it happens
Trigger: Decorating a function whose return annotation is e.g. -> int or -> str (not -> Var[int]) with the internal operator-dispatch decorator in reflex.vars.base.
Common situations: Extending reflex's reactive operators with a custom dispatch function and forgetting to annotate the return as Var[T].
Related errors
- Expected return type of {fn.__name__} to be a Var, got {fn_r
- Expected generic type of {fn_return} to be a type.
- Expected first argument of {fn.__name__} to be a Var, got {f
- `@rx.memo` on `{fn.__name__}` must return `rx.Component` or
- `{handler_name}` handler should have a parameter annotated a
AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28).
Data as JSON: /api/errors/5e33e6f038482f71.
Report an issue: GitHub.