reflex-dev/reflex · error · VarValueError

Dependency detection cannot identify get_state class from a

Error message

Dependency detection cannot identify get_state class from a code object.

What it means

When a cached computed var calls get_state(SomeState), the tracker must resolve the state class from the instruction. If the tracked function is a raw code object (not a function with __globals__/closure), the class cannot be identified, so VarValueError is raised.

Source

Thrown at packages/reflex-base/src/reflex_base/vars/dep_tracking.py:265

    def handle_getting_state(self, instruction: dis.Instruction) -> None:
        """Handle bytecode analysis when `get_state` was called in the function.

        If the wrapped function is getting an arbitrary state and saving it to a
        local variable, this method associates the local variable name with the
        state class in self.tracked_locals.

        When an attribute/method is accessed on a tracked local, it will be
        associated with this state.

        Args:
            instruction: The dis instruction to process.

        Raises:
            VarValueError: if the state class cannot be determined from the instruction.
        """
        if isinstance(self.func, CodeType):
            msg = "Dependency detection cannot identify get_state class from a code object."
            raise VarValueError(msg)
        if instruction.opname in ("LOAD_FAST", "LOAD_FAST_BORROW"):
            self._getting_state_class = self.get_tracked_local(
                local_name=instruction.argval,
            )
        elif instruction.opname == "LOAD_GLOBAL":
            # Special case: referencing state class from global scope.
            try:
                self._getting_state_class = self._get_globals()[instruction.argval]
            except (ValueError, KeyError) as ve:
                msg = f"Cached var {self!s} cannot access arbitrary state `{instruction.argval}`, not found in globals."
                raise VarValueError(msg) from ve
        elif instruction.opname == "LOAD_DEREF":
            # Special case: referencing state class from closure.
            try:
                self._getting_state_class = self._get_closure()[instruction.argval]
            except (ValueError, KeyError) as ve:
                msg = f"Cached var {self!s} cannot access arbitrary state `{instruction.argval}`, is it defined yet?"
                raise VarValueError(msg) from ve

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Pass a real function (with globals and closure intact) to the computed var instead of a CodeType
  2. Avoid get_state in code-object-based computed vars; declare deps explicitly
Defensive patterns

Strategy: fallback

Validate before calling

import types
assert not isinstance(func, types.CodeType), 'pass a real function, not a code object'

Type guard

def is_real_function(f) -> bool:
    import types, inspect
    return inspect.isfunction(f) and not isinstance(f, types.CodeType)

Prevention

When it happens

Trigger: Dependency scanning a function compiled from a bare code object (e.g. types.FunctionType built from a code object, or reconstructed/pickled functions) that calls self.get_state(...).

Common situations: Advanced usage passing code objects to ComputedVar; hot-reload/pickling edge cases that lose function metadata.

Related errors


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