{"record":{"id":"3fb2ed8cf1e424e9","repo":"reflex-dev/reflex","slug":"var-of-type-self-var-type-does-not-support-item","errorCode":null,"errorMessage":"Var of type {self._var_type} does not support item access.","messagePattern":"Var of type (.+?) does not support item access\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/reflex-base/src/reflex_base/vars/base.py","lineNumber":1455,"sourceCode":"        def __getitem__(self, key: Any) -> Var:\n            \"\"\"Get the item from the var.\n\n            Args:\n                key: The key to get.\n\n            Raises:\n                UntypedVarError: If the var type is Any.\n                TypeError: If the var type is Any.\n\n            # noqa: DAR101 self\n            \"\"\"\n            if self._var_type is Any:\n                raise exceptions.UntypedVarError(\n                    self,\n                    f\"access the item '{key}'\",\n                )\n            msg = f\"Var of type {self._var_type} does not support item access.\"\n            raise TypeError(msg)\n\n        def __getattr__(self, name: str):\n            \"\"\"Get an attribute of the var.\n\n            Args:\n                name: The name of the attribute.\n\n            Raises:\n                VarAttributeError: If the attribute does not exist.\n                UntypedVarError: If the var type is Any.\n                TypeError: If the var type is Any.\n\n            # noqa: DAR101 self\n            \"\"\"\n            if name.startswith(\"_\"):\n                msg = f\"Attribute {name} not found.\"\n                raise VarAttributeError(msg)\n","sourceCodeStart":1437,"sourceCodeEnd":1473,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-base/src/reflex_base/vars/base.py#L1437-L1473","documentation":"Subscripting a Reflex Var (var[key]) compiles to JS item access, which requires the Var's Python type to support __getitem__ (dict, list, tuple, etc.). If _var_type is a type without item access, Reflex raises this TypeError at Python render time. Untyped (Any) vars raise UntypedVarError instead.","triggerScenarios":"Writing state.items[0] or state.config['key'] where the state attribute is annotated as int, str (py<3.9 semantics differ), a dataclass, or any non-subscriptable type; annotating as a bare class then indexing in the UI.","commonSituations":"Annotating a field as the wrong type (e.g. str instead of dict) then indexing it; forgetting that Reflex type-checks attribute operations against the annotation; refactors that changed a field from dict to a custom object while the template still indexes it.","solutions":["Fix the state annotation to a subscriptable type (dict[str, int], list[str], etc.) matching how you use it","If the var genuinely is a container at runtime, keep the annotation accurate — never annotate it as the scalar type","Use attribute access (var.attr) instead of item access for objects/dataclasses","For Any-typed vars, annotate properly or use .to(dict) before indexing"],"exampleFix":"# before\nclass State(rx.State):\n    config: str\nrx.text(State.config['key'])\n# after\nclass State(rx.State):\n    config: dict[str, str]\nrx.text(State.config['key'])","handlingStrategy":"type-guard","validationCode":"cls = State.__annotations__['items']\nassert hasattr(cls, '__getitem__') or cls in (list, dict, tuple, str), f'{cls} is not subscriptable'","typeGuard":"def supports_item_access(var_type: object) -> bool:\n    return isinstance(var_type, type) and (var_type in (list, dict, tuple, str) or hasattr(var_type, '__getitem__'))","tryCatchPattern":null,"preventionTips":["Annotate state fields with the container type you actually index (dict[str, X], list[X])","Run mypy on state classes so wrong annotations surface early","Use .to(dict) when a var's type is unknown but indexing is needed"],"tags":["reflex","var","item-access","type-annotation"],"backgroundTag":"unsupported-operation-on-type","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}