{"record":{"id":"9cc710d71d3ab72a","repo":"reflex-dev/reflex","slug":"cached-var-self-s-cannot-access-arbitrary-state-9cc710","errorCode":null,"errorMessage":"Cached var {self!s} cannot access arbitrary state `{instruction.argval}`, not found in globals.","messagePattern":"Cached var (.+?) cannot access arbitrary state `(.+?)`, not found in globals\\.","errorType":"exception","errorClass":"VarValueError","httpStatus":null,"severity":"error","filePath":"packages/reflex-base/src/reflex_base/vars/dep_tracking.py","lineNumber":276,"sourceCode":"            instruction: The dis instruction to process.\n\n        Raises:\n            VarValueError: if the state class cannot be determined from the instruction.\n        \"\"\"\n        if isinstance(self.func, CodeType):\n            msg = \"Dependency detection cannot identify get_state class from a code object.\"\n            raise VarValueError(msg)\n        if instruction.opname in (\"LOAD_FAST\", \"LOAD_FAST_BORROW\"):\n            self._getting_state_class = self.get_tracked_local(\n                local_name=instruction.argval,\n            )\n        elif instruction.opname == \"LOAD_GLOBAL\":\n            # Special case: referencing state class from global scope.\n            try:\n                self._getting_state_class = self._get_globals()[instruction.argval]\n            except (ValueError, KeyError) as ve:\n                msg = f\"Cached var {self!s} cannot access arbitrary state `{instruction.argval}`, not found in globals.\"\n                raise VarValueError(msg) from ve\n        elif instruction.opname == \"LOAD_DEREF\":\n            # Special case: referencing state class from closure.\n            try:\n                self._getting_state_class = self._get_closure()[instruction.argval]\n            except (ValueError, KeyError) as ve:\n                msg = f\"Cached var {self!s} cannot access arbitrary state `{instruction.argval}`, is it defined yet?\"\n                raise VarValueError(msg) from ve\n        elif instruction.opname in (\"LOAD_ATTR\", \"LOAD_METHOD\"):\n            self._getting_state_class = getattr(\n                self._getting_state_class,\n                instruction.argval,\n            )\n        elif instruction.opname == \"GET_AWAITABLE\":\n            # Now inside the `await` machinery, subsequent instructions\n            # operate on the result of the `get_state` call.\n            self.scan_status = ScanStatus.GETTING_STATE_POST_AWAIT\n            if self._getting_state_class is not None:\n                self.top_of_stack = \"_\"","sourceCodeStart":258,"sourceCodeEnd":294,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-base/src/reflex_base/vars/dep_tracking.py#L258-L294","documentation":"When a @rx.var(cached_var) function is scanned, Reflex walks its bytecode to find which state vars it depends on. If the function references a name via LOAD_GLOBAL (e.g. `OtherState.some_var`), Reflex looks that name up in the function module's globals to resolve the state class. If the name is not present there, dependency tracking cannot proceed and VarValueError is raised.","triggerScenarios":"A cached var function references a state class or other global (e.g. `MyState.foo`) where `MyState` is not actually in that module's globals — typically because of circular imports, the class being defined later, a typo in the state name, or the function being exec'd from a context whose globals don't contain the name.","commonSituations":"Splitting state classes across modules with circular imports; renaming a state class but not its uses inside cached vars; defining cached vars in a different module from where the state is imported under a `if TYPE_CHECKING` guard; interactive/REPL or dynamically exec'd code where globals are unavailable.","solutions":["Make sure the state class referenced inside the cached var is a real (non-TYPE_CHECKING-only) import at the top of the module where the cached var is defined.","Fix typos in the state class name inside the cached var body.","Break circular imports by moving shared state classes into their own module imported by both parties.","If the global is genuinely unavailable, pass the needed value in as a state var access on `self` instead of a free global reference."],"exampleFix":"# before\nfrom typing import TYPE_CHECKING\nif TYPE_CHECKING:\n    from .other import OtherState\n\n@rx.var(cached=True)\ndef combined(self) -> int:\n    return self.value + OtherState.value  # OtherState missing at runtime\n\n# after\nfrom .other import OtherState\n\n@rx.var(cached=True)\ndef combined(self) -> int:\n    return self.value + OtherState.value","handlingStrategy":"validation","validationCode":"import sys\n\ndef state_class_available(name: str, func) -> bool:\n    mod = func.__globals__ if hasattr(func, \"__globals__\") else {}\n    return name in mod or name in sys.modules","typeGuard":null,"tryCatchPattern":"from reflex.exceptions import VarValueError\n\ntry:\n    State.computed  # access to trigger dependency tracking at compile\nexcept VarValueError as e:\n    if \"not found in globals\" in str(e):\n        # fix imports of the referenced state class\n        ...","preventionTips":["Always import state classes used inside cached vars at module top level (not under TYPE_CHECKING).","Avoid circular imports by placing shared state classes in a dedicated module.","Run a smoke compile (rx.App build / reflex export) in CI to catch tracking errors early."],"tags":["reflex","cached-var","dependency-tracking","import-error"],"backgroundTag":"cached-var-unresolved-global","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}