def _isinstance(
    obj: Any,
    cls: GenericType,
    *,
    nested: int = 0,
    treat_var_as_type: bool = True,
    treat_mutable_obj_as_immutable: bool = False,
) -> bool:
    """Check if an object is an instance of a class.

    Args:
        obj: The object to check.
        cls: The class to check against.
        nested: How many levels deep to check.
        treat_var_as_type: Whether to treat Var as the type it represents, i.e. _var_type.
        treat_mutable_obj_as_immutable: Whether to treat mutable objects as immutable. Useful if a component declares a mutable object as a prop, but the value is not expected to change.

    Returns:
        Whether the object is an instance of the class.
    """
    if cls is Any:
        return True

    from reflex.vars import LiteralVar, Var

    if cls is Var:
        return isinstance(obj, Var)
    if isinstance(obj, LiteralVar):
        return treat_var_as_type and _isinstance(
            obj._var_value, cls, nested=nested, treat_var_as_type=True
        )
    if isinstance(obj, Var):
        return treat_var_as_type and typehint_issubclass(
            obj._var_type,
            cls,
            treat_mutable_superclasss_as_immutable=treat_mutable_obj_as_immutable,
            treat_literals_as_union_of_types=True,
            treat_any_as_subtype_of_everything=True,
        )

    if cls is None or cls is type(None):
        return obj is None

    if cls is not None and is_union(cls):
        return any(
            _isinstance(obj, arg, nested=nested, treat_var_as_type=treat_var_as_type)
            for arg in get_args(cls)
        )

    if is_literal(cls):
        return obj in get_args(cls)

    origin = get_origin(cls)

    if origin is None:
        # cls is a typed dict
        if is_typeddict(cls):
            if nested:
                return does_obj_satisfy_typed_dict(obj, cls)
            return isinstance(obj, dict)

        # cls is a float
        if cls is float:
            return isinstance(obj, (float, int))

        # cls is a simple class
        return isinstance(obj, cls)

    args = get_args(cls)

    if not args:
        if treat_mutable_obj_as_immutable:
            if origin is dict:
                origin = Mapping
            elif origin is list or origin is set:
                origin = Sequence
        # cls is a simple generic class
        return isinstance(obj, origin)

    if origin is Var and args:
        # cls is a Var
        return _isinstance(
            obj,
            args[0],
            nested=nested,
            treat_var_as_type=treat_var_as_type,
            treat_mutable_obj_as_immutable=treat_mutable_obj_as_immutable,
        )

    if nested > 0 and args:
        if origin is list:
            expected_class = Sequence if treat_mutable_obj_as_immutable else list
            return isinstance(obj, expected_class) and all(
                _isinstance(
                    item,
                    args[0],
                    nested=nested - 1,
                    treat_var_as_type=treat_var_as_type,
                )
                for item in obj
            )
        if origin is tuple:
            if args[-1] is Ellipsis:
                return isinstance(obj, tuple) and all(
                    _isinstance(
                        item,
                        args[0],
                        nested=nested - 1,
                        treat_var_as_type=treat_var_as_type,
                    )
                    for item in obj
                )
            return (
                isinstance(obj, tuple)
                and len(obj) == len(args)
                and all(
                    _isinstance(
                        item,
                        arg,
                        nested=nested - 1,
                        treat_var_as_type=treat_var_as_type,
                    )
                    for item, arg in zip(obj, args, strict=True)
                )
            )
        if origin in (dict, Mapping, Breakpoints):
            expected_class = (
                dict
                if origin is dict and not treat_mutable_obj_as_immutable
                else Mapping
            )
            return isinstance(obj, expected_class) and all(
                _isinstance(
                    key, args[0], nested=nested - 1, treat_var_as_type=treat_var_as_type
                )
                and _isinstance(
                    value,
                    args[1],
                    nested=nested - 1,
                    treat_var_as_type=treat_var_as_type,
                )
                for key, value in obj.items()
            )
        if origin is set:
            expected_class = Sequence if treat_mutable_obj_as_immutable else set
            return isinstance(obj, expected_class) and all(
                _isinstance(
                    item,
                    args[0],
                    nested=nested - 1,
                    treat_var_as_type=treat_var_as_type,
                )
                for item in obj
            )

    if args:
        from reflex.vars import Field

        if origin is Field:
            return _isinstance(
                obj, args[0], nested=nested, treat_var_as_type=treat_var_as_type
            )

    return isinstance(obj, get_base_class(cls))
Check if an object is an instance of a class.

    Args:
        obj: The object to check.
        cls: The class to check against.
        nested: How many levels deep to check.
        treat_var_as_type: Whether to treat Var as the type it represents, i.e. _var_type.
        treat_mutable_obj_as_immutable: Whether to treat mutable objects as immutable. Useful if a component declares a mutable object as a prop, but the value is not expected to change.

    Returns:
        Whether the object is an instance of the class.
    
reflex/utils/types.py