reflex-dev/reflex · error · TypeError
Expected Var return type of {fn.__name__} to have a generic
Error message
Expected Var return type of {fn.__name__} to have a generic type. What it means
The operator-dispatch registration requires a fully generic return annotation Var[T]; a bare -> Var without a generic parameter cannot tell reflex which Python type the dispatcher handles.
Source
Thrown at packages/reflex-base/src/reflex_base/vars/base.py:3367
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
def dispatch(
field_name: str,
var_data: VarData,
result_var_type: GenericType,
existing_var: Var | None = None,
) -> Var:View on GitHub (pinned to 45b8ed5ab7)
Solutions
- Change the return annotation to a parameterized Var, e.g. -> Var[int] or -> Var[str]
- Add from __future__ import annotations if typing forward references were the issue
Example fix
# before def to_title(v: Var[str]) -> Var: ... # after def to_title(v: Var[str]) -> Var[str]: ...
Defensive patterns
Strategy: type-guard
Validate before calling
from typing import get_args assert get_args(fn.__annotations__['return']), 'Var return must be parameterized (Var[T])'
Type guard
def returns_parameterized_var(fn) -> bool:
from typing import get_origin, get_args
from reflex.vars import Var
rt = fn.__annotations__.get('return')
return get_origin(rt) is Var and bool(get_args(rt)) Prevention
- Never use bare Var in annotations for dispatch functions
When it happens
Trigger: Annotating the dispatch function's return as plain Var (no [T]) when registering it in the dispatcher table.
Common situations: Custom operator functions written quickly with a bare Var return type.
Related errors
- Expected generic type of {fn_first_arg_type} to be a type.
- `@rx.memo` on `{fn.__name__}` must return `rx.Component` or
- `{handler_name}` handler should have a parameter annotated a
- Could not compare types {args_types_without_vars[i]} and {ca
- MissingAnnotationError
AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28).
Data as JSON: /api/errors/f5d8fdd449b84b2c.
Report an issue: GitHub.