reflex icon indicating copy to clipboard operation
reflex copied to clipboard

reflex.utils.exceptions.VarTypeError: Unsupported Operand type(s) for []: ObjectCastedVar, ObjectCastedVar

Open SheldonHolmgren opened this issue 10 months ago β€’ 3 comments

Describe the Features

Allow state dicts to be indexed by complex types (i.e., types other than string and int).

Example:

class Canonical_name(rx.Base):
  prefix: str
  name: str
  postfix: str

class Variable(rx.Base):
  canonical_name: Canonical_name
  type: str

class Contract(rx.Base):
  variables: dict[Canonical_name, Variable] = rx.field({})

This is currently disallowed in https://github.com/reflex-dev/reflex/blob/6c6201fbb016afc8d3236c262e8008952cf675e3/reflex/vars/object.py#L225

SheldonHolmgren avatar Mar 27 '25 10:03 SheldonHolmgren

state variables have to be json serializable, so this cannot be supported (we're barely allowing it for numbers)

adhami3310 avatar Mar 27 '25 21:03 adhami3310

Maybe this could be supported with deterministic methods converting to string representation and parsing back, either implicit (generated) or explicitly implemented by the user? The latter would probably require duplicate implementation: for both the backend and the frontend.

SheldonHolmgren avatar Apr 01 '25 13:04 SheldonHolmgren

javascript isn't well built for this :/ even equality doesn't work, so we cannot do much with this one

adhami3310 avatar May 17 '25 02:05 adhami3310