reflex-dev/reflex · error · VarTypeError

Unsupported Operand type(s) for {operator}: {', '.join(t.__n

Error message

Unsupported Operand type(s) for {operator}: {', '.join(t.__name__ for t in operands_types)}

What it means

NumberVar arithmetic operators (__add__, __sub__, __mul__ and their reflected variants) are typed to accept only number types (int, float, decimal.Decimal vars). At the type level, unsupported operand types trigger this VarTypeError via raise_unsupported_operand_types, mirroring TypeError from Python arithmetic.

Source

Thrown at packages/reflex-base/src/reflex_base/vars/number.py:56

if TYPE_CHECKING:
    from .sequence import ArrayVar


def raise_unsupported_operand_types(
    operator: str, operands_types: tuple[type, ...]
) -> NoReturn:
    """Raise an unsupported operand types error.

    Args:
        operator: The operator.
        operands_types: The types of the operands.

    Raises:
        VarTypeError: The operand types are unsupported.
    """
    msg = f"Unsupported Operand type(s) for {operator}: {', '.join(t.__name__ for t in operands_types)}"
    raise VarTypeError(msg)


class NumberVar(Var[NUMBER_T], python_types=(int, float, decimal.Decimal)):
    """Base class for immutable number vars."""

    def __add__(self, other: number_types) -> NumberVar:
        """Add two numbers.

        Args:
            other: The other number.

        Returns:
            The number addition operation.
        """
        if not isinstance(other, NUMBER_TYPES):
            raise_unsupported_operand_types("+", (type(self), type(other)))
        return number_add_operation(self, +other)

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Convert the other operand to a number var first (e.g. `rx.to_number_var(x)` / int()/float() on the literal).
  2. For string building, use f-string or .concat()/str conversion instead of + on numbers.
  3. Annotate the operand correctly so a plain Python number is passed rather than a non-number Var.

Example fix

# before
count: rx.Var[int]
label = count + " items"  # unsupported operand

# after
label = f"{count} items"
Defensive patterns

Strategy: type-guard

Validate before calling

from reflex.vars import NumberVar

def is_number_operand(v) -> bool:
    return isinstance(v, (int, float, decimal.Decimal)) or isinstance(v, NumberVar)

Type guard

import decimal
from reflex.vars import NumberVar

def is_number_var_or_literal(v) -> bool:
    return isinstance(v, NumberVar) or isinstance(v, (int, float, decimal.Decimal))

Prevention

When it happens

Trigger: Adding/subtracting/multiplying a NumberVar by an operand whose static type is not int/float/Decimal — e.g. a StringVar, a boolean var, None, or an untyped Any that a type checker or runtime check rejects.

Common situations: Concatenating a number var with a string var using +; multiplying by a value from untyped JSON; pyright/mypy flagging the operation before runtime; version upgrades that tightened operand type annotations.

Related errors


AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28). Data as JSON: /api/errors/deb43e5f584f85a2. Report an issue: GitHub.