reflex/reflex/vars.pyi
benedikt-bartscher e42d4ed9ef
[REF-3009] type transforming serializers (#3227)
* wip type transforming serializers

* old python sucks

* typing fixups

* Expose the `to` parameter on `rx.serializer` for type conversion

Serializers can also return a tuple of `(serialized_value, type)`, if both ways
are specified, then the returned value MUST match the `to` parameter.

When initializing a new rx.Var, if `_var_is_string` is not specified and the serializer returns a `str` type, then mark `_var_is_string=True` to indicate that the Var should be treated like a string literal.

Include datetime, color, types, and paths as "serializing to str" type.

Avoid other changes at this point to reduce fallout from this change:

  Notably, the `serialize_str` function does NOT cast to `str`, which
  would cause existing code to treat all Var initialized with a str as a
  str literal even though this was NOT the default before.

Update test cases to accomodate these changes.

* Raise deprecation warning for rx.Var.create with string literal

In the future, we will treat strings as string literals in the JS code. To get
a Var that is not treated like a string, pass _var_is_string=False.

This will allow our serializers to automatically identify cast string literals
with less special cases (and the special cases need to be explicitly
identified).

* Add test case for mismatched serialized types

* fix old python

* Remove serializer returning a tuple feature

Simplify the logic; instead of making a wrapper function that returns
a tuple, just save the type conversions in a separate global.

* Reset the LRU cache when adding new serializers

---------

Co-authored-by: Masen Furer <m_github@0x26.net>
2024-06-07 09:50:10 -07:00

185 lines
6.2 KiB
Python

""" Generated with stubgen from mypy, then manually edited, do not regen."""
from __future__ import annotations
import datetime
from dataclasses import dataclass
from _typeshed import Incomplete
from reflex import constants as constants
from reflex.base import Base as Base
from reflex.state import State as State
from reflex.state import BaseState as BaseState
from reflex.utils import console as console, format as format, types as types
from reflex.utils.imports import ImportVar
from types import FunctionType
from typing import (
Any,
Callable,
Dict,
Iterable,
List,
Optional,
Set,
Tuple,
Type,
Union,
overload,
_GenericAlias, # type: ignore
)
USED_VARIABLES: Incomplete
def get_unique_variable_name() -> str: ...
def _encode_var(value: Var) -> str: ...
def _decode_var(value: str) -> tuple[VarData, str]: ...
def _extract_var_data(value: Iterable) -> list[VarData | None]: ...
class VarData(Base):
state: str = ""
imports: dict[str, List[ImportVar]] = {}
hooks: Dict[str, None] = {}
interpolations: List[Tuple[int, int]] = []
@classmethod
def merge(cls, *others: VarData | None) -> VarData | None: ...
class Var:
_var_name: str
_var_type: Type
_var_is_local: bool = False
_var_is_string: bool = False
_var_full_name_needs_state_prefix: bool = False
_var_data: VarData | None = None
@classmethod
def create(
cls, value: Any, _var_is_local: bool = True, _var_is_string: bool | None = None, _var_data: VarData | None = None,
) -> Optional[Var]: ...
@classmethod
def create_safe(
cls, value: Any, _var_is_local: bool = True, _var_is_string: bool | None = None, _var_data: VarData | None = None,
) -> Var: ...
@classmethod
def __class_getitem__(cls, type_: Type) -> _GenericAlias: ...
def _replace(self, merge_var_data=None, **kwargs: Any) -> Var: ...
def equals(self, other: Var) -> bool: ...
def to_string(self) -> Var: ...
def __hash__(self) -> int: ...
def __format__(self, format_spec: str) -> str: ...
def __getitem__(self, i: Any) -> Var: ...
def __getattribute__(self, name: str) -> Var: ...
def operation(
self,
op: str = ...,
other: Optional[Var] = ...,
type_: Optional[Type] = ...,
flip: bool = ...,
fn: Optional[str] = ...,
) -> Var: ...
def compare(self, op: str, other: Var) -> Var: ...
def __invert__(self) -> Var: ...
def __neg__(self) -> Var: ...
def __abs__(self) -> Var: ...
def length(self) -> Var: ...
def __eq__(self, other: Var) -> Var: ...
def __ne__(self, other: Var) -> Var: ...
def __gt__(self, other: Var) -> Var: ...
def __ge__(self, other: Var) -> Var: ...
def __lt__(self, other: Var) -> Var: ...
def __le__(self, other: Var) -> Var: ...
def __add__(self, other: Var) -> Var: ...
def __radd__(self, other: Var) -> Var: ...
def __sub__(self, other: Var) -> Var: ...
def __rsub__(self, other: Var) -> Var: ...
def __mul__(self, other: Var) -> Var: ...
def __rmul__(self, other: Var) -> Var: ...
def __pow__(self, other: Var) -> Var: ...
def __rpow__(self, other: Var) -> Var: ...
def __truediv__(self, other: Var) -> Var: ...
def __rtruediv__(self, other: Var) -> Var: ...
def __floordiv__(self, other: Var) -> Var: ...
def __mod__(self, other: Var) -> Var: ...
def __rmod__(self, other: Var) -> Var: ...
def __and__(self, other: Var) -> Var: ...
def __rand__(self, other: Var) -> Var: ...
def __or__(self, other: Var) -> Var: ...
def __ror__(self, other: Var) -> Var: ...
def __contains__(self, _: Any) -> Var: ...
def contains(self, other: Any) -> Var: ...
def reverse(self) -> Var: ...
def foreach(self, fn: Callable) -> Var: ...
@classmethod
def range(
cls,
v1: Var | int = 0,
v2: Var | int | None = None,
step: Var | int | None = None,
) -> Var: ...
def to(self, type_: Type) -> Var: ...
def as_ref(self) -> Var: ...
@property
def _var_full_name(self) -> str: ...
def _var_set_state(self, state: Type[BaseState] | str) -> Any: ...
@dataclass(eq=False)
class BaseVar(Var):
_var_name: str
_var_type: Any
_var_is_local: bool = False
_var_is_string: bool = False
_var_full_name_needs_state_prefix: bool = False
_var_data: VarData | None = None
def __hash__(self) -> int: ...
def get_default_value(self) -> Any: ...
def get_setter_name(self, include_state: bool = ...) -> str: ...
def get_setter(self) -> Callable[[BaseState, Any], None]: ...
@dataclass(init=False)
class ComputedVar(Var):
_var_cache: bool
fget: FunctionType
@property
def _cache_attr(self) -> str: ...
def __get__(self, instance, owner): ...
def _deps(self, objclass: Type, obj: Optional[FunctionType] = ...) -> Set[str]: ...
def _replace(self, merge_var_data=None, **kwargs: Any) -> ComputedVar: ...
def mark_dirty(self, instance) -> None: ...
def needs_update(self, instance) -> bool: ...
def _determine_var_type(self) -> Type: ...
@overload
def __init__(
self,
fget: Callable[[BaseState], Any],
**kwargs,
) -> None: ...
@overload
def __init__(self, func) -> None: ...
@overload
def computed_var(
fget: Callable[[BaseState], Any] | None = None,
initial_value: Any | None = None,
cache: bool = False,
deps: Optional[List[Union[str, Var]]] = None,
auto_deps: bool = True,
interval: Optional[Union[datetime.timedelta, int]] = None,
**kwargs,
) -> Callable[[Callable[[Any], Any]], ComputedVar]: ...
@overload
def computed_var(fget: Callable[[Any], Any]) -> ComputedVar: ...
@overload
def cached_var(
fget: Callable[[BaseState], Any] | None = None,
initial_value: Any | None = None,
deps: Optional[List[Union[str, Var]]] = None,
auto_deps: bool = True,
interval: Optional[Union[datetime.timedelta, int]] = None,
**kwargs,
) -> Callable[[Callable[[Any], Any]], ComputedVar]: ...
@overload
def cached_var(fget: Callable[[Any], Any]) -> ComputedVar: ...
class CallableVar(BaseVar):
def __init__(self, fn: Callable[..., BaseVar]): ...
def __call__(self, *args, **kwargs) -> BaseVar: ...
def get_uuid_string_var() -> Var: ...