base.py 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492
  1. """Collection of base classes."""
  2. from __future__ import annotations
  3. import contextlib
  4. import dataclasses
  5. import datetime
  6. import dis
  7. import functools
  8. import inspect
  9. import json
  10. import random
  11. import re
  12. import string
  13. import sys
  14. import warnings
  15. from types import CodeType, FunctionType
  16. from typing import (
  17. TYPE_CHECKING,
  18. Any,
  19. Callable,
  20. ClassVar,
  21. Dict,
  22. FrozenSet,
  23. Generic,
  24. Iterable,
  25. List,
  26. Literal,
  27. NoReturn,
  28. Optional,
  29. Sequence,
  30. Set,
  31. Tuple,
  32. Type,
  33. TypeVar,
  34. Union,
  35. cast,
  36. get_args,
  37. overload,
  38. )
  39. from typing_extensions import (
  40. ParamSpec,
  41. Protocol,
  42. TypeGuard,
  43. deprecated,
  44. get_type_hints,
  45. override,
  46. )
  47. from reflex import constants
  48. from reflex.base import Base
  49. from reflex.constants.compiler import Hooks
  50. from reflex.utils import console, exceptions, imports, serializers, types
  51. from reflex.utils.exceptions import (
  52. VarAttributeError,
  53. VarDependencyError,
  54. VarTypeError,
  55. VarValueError,
  56. )
  57. from reflex.utils.format import format_state_name
  58. from reflex.utils.imports import (
  59. ImmutableParsedImportDict,
  60. ImportDict,
  61. ImportVar,
  62. ParsedImportDict,
  63. parse_imports,
  64. )
  65. from reflex.utils.types import (
  66. GenericType,
  67. Self,
  68. _isinstance,
  69. get_origin,
  70. has_args,
  71. infallible_issubclass,
  72. typehint_issubclass,
  73. unionize,
  74. )
  75. if TYPE_CHECKING:
  76. from reflex.components.component import BaseComponent
  77. from reflex.state import BaseState
  78. from .function import ArgsFunctionOperation
  79. from .number import BooleanVar, NumberVar
  80. from .object import ObjectVar
  81. from .sequence import ArrayVar, StringVar
  82. VAR_TYPE = TypeVar("VAR_TYPE", covariant=True)
  83. VALUE = TypeVar("VALUE")
  84. INT_OR_FLOAT = TypeVar("INT_OR_FLOAT", int, float)
  85. FAKE_VAR_TYPE = TypeVar("FAKE_VAR_TYPE")
  86. OTHER_VAR_TYPE = TypeVar("OTHER_VAR_TYPE")
  87. warnings.filterwarnings("ignore", message="fields may not start with an underscore")
  88. P = ParamSpec("P")
  89. R = TypeVar("R")
  90. class ReflexCallable(Protocol[P, R]):
  91. """Protocol for a callable."""
  92. __call__: Callable[P, R]
  93. if sys.version_info >= (3, 10):
  94. from types import EllipsisType
  95. ReflexCallableParams = Union[EllipsisType, Tuple[GenericType, ...]]
  96. else:
  97. ReflexCallableParams = Union[Any, Tuple[GenericType, ...]]
  98. def unwrap_reflex_callalbe(
  99. callable_type: GenericType,
  100. ) -> Tuple[ReflexCallableParams, GenericType]:
  101. """Unwrap the ReflexCallable type.
  102. Args:
  103. callable_type: The ReflexCallable type to unwrap.
  104. Returns:
  105. The unwrapped ReflexCallable type.
  106. """
  107. if callable_type is ReflexCallable:
  108. return Ellipsis, Any
  109. origin = get_origin(callable_type)
  110. if origin is not ReflexCallable:
  111. if origin in types.UnionTypes:
  112. args = get_args(callable_type)
  113. params: List[ReflexCallableParams] = []
  114. return_types: List[GenericType] = []
  115. for arg in args:
  116. param, return_type = unwrap_reflex_callalbe(arg)
  117. if param not in params:
  118. params.append(param)
  119. return_types.append(return_type)
  120. return (
  121. Ellipsis if len(params) > 1 else params[0],
  122. unionize(*return_types),
  123. )
  124. return Ellipsis, Any
  125. args = get_args(callable_type)
  126. if not args or len(args) != 2:
  127. return Ellipsis, Any
  128. return args
  129. _VAR_CACHING = True
  130. def get_var_caching() -> bool:
  131. """Get the var caching status.
  132. Returns:
  133. The var caching status.
  134. """
  135. return _VAR_CACHING
  136. def set_var_caching(value: bool):
  137. """Set the var caching status.
  138. Args:
  139. value: The value to set the var caching status to.
  140. """
  141. global _VAR_CACHING
  142. _VAR_CACHING = value
  143. @dataclasses.dataclass(
  144. eq=False,
  145. frozen=True,
  146. )
  147. class VarSubclassEntry:
  148. """Entry for a Var subclass."""
  149. var_subclass: Type[Var]
  150. to_var_subclass: Type[ToOperation]
  151. python_types: Tuple[GenericType, ...]
  152. is_subclass: Callable[[GenericType], bool] | None
  153. _var_subclasses: List[VarSubclassEntry] = []
  154. _var_literal_subclasses: List[Tuple[Type[LiteralVar], VarSubclassEntry]] = []
  155. @dataclasses.dataclass(
  156. eq=True,
  157. frozen=True,
  158. )
  159. class VarData:
  160. """Metadata associated with a x."""
  161. # The name of the enclosing state.
  162. state: str = dataclasses.field(default="")
  163. # The name of the field in the state.
  164. field_name: str = dataclasses.field(default="")
  165. # Imports needed to render this var
  166. imports: ImmutableParsedImportDict = dataclasses.field(default_factory=tuple)
  167. # Hooks that need to be present in the component to render this var
  168. hooks: Tuple[str, ...] = dataclasses.field(default_factory=tuple)
  169. # Components that need to be present in the component to render this var
  170. components: Tuple[BaseComponent, ...] = dataclasses.field(default_factory=tuple)
  171. # Dependencies of the var
  172. deps: Tuple[Var, ...] = dataclasses.field(default_factory=tuple)
  173. # Position of the hook in the component
  174. position: Hooks.HookPosition | None = None
  175. def __init__(
  176. self,
  177. state: str = "",
  178. field_name: str = "",
  179. imports: ImportDict | ParsedImportDict | None = None,
  180. hooks: dict[str, VarData | None] | None = None,
  181. components: Iterable[BaseComponent] | None = None,
  182. deps: list[Var] | None = None,
  183. position: Hooks.HookPosition | None = None,
  184. ):
  185. """Initialize the var data.
  186. Args:
  187. state: The name of the enclosing state.
  188. field_name: The name of the field in the state.
  189. imports: Imports needed to render this var.
  190. hooks: Hooks that need to be present in the component to render this var.
  191. components: Components that need to be present in the component to render this var.
  192. deps: Dependencies of the var for useCallback.
  193. position: Position of the hook in the component.
  194. """
  195. immutable_imports: ImmutableParsedImportDict = tuple(
  196. (k, tuple(v)) for k, v in parse_imports(imports or {}).items()
  197. )
  198. object.__setattr__(self, "state", state)
  199. object.__setattr__(self, "field_name", field_name)
  200. object.__setattr__(self, "imports", immutable_imports)
  201. object.__setattr__(self, "hooks", tuple(hooks or {}))
  202. object.__setattr__(
  203. self, "components", tuple(components) if components is not None else ()
  204. )
  205. object.__setattr__(self, "deps", tuple(deps or []))
  206. object.__setattr__(self, "position", position or None)
  207. def old_school_imports(self) -> ImportDict:
  208. """Return the imports as a mutable dict.
  209. Returns:
  210. The imports as a mutable dict.
  211. """
  212. return {k: list(v) for k, v in self.imports}
  213. def merge(*all: VarData | None) -> VarData | None:
  214. """Merge multiple var data objects.
  215. Returns:
  216. The merged var data object.
  217. Raises:
  218. ReflexError: If the positions of the var data objects are different.
  219. """
  220. all_var_datas = list(filter(None, all))
  221. if not all_var_datas:
  222. return None
  223. if len(all_var_datas) == 1:
  224. return all_var_datas[0]
  225. # Get the first non-empty field name or default to empty string.
  226. field_name = next(
  227. (var_data.field_name for var_data in all_var_datas if var_data.field_name),
  228. "",
  229. )
  230. # Get the first non-empty state or default to empty string.
  231. state = next(
  232. (var_data.state for var_data in all_var_datas if var_data.state), ""
  233. )
  234. hooks: dict[str, VarData | None] = {
  235. hook: None for var_data in all_var_datas for hook in var_data.hooks
  236. }
  237. _imports = imports.merge_imports(
  238. *(var_data.imports for var_data in all_var_datas)
  239. )
  240. deps = [dep for var_data in all_var_datas for dep in var_data.deps]
  241. positions = list(
  242. {
  243. var_data.position
  244. for var_data in all_var_datas
  245. if var_data.position is not None
  246. }
  247. )
  248. components = tuple(
  249. component for var_data in all_var_datas for component in var_data.components
  250. )
  251. if positions:
  252. if len(positions) > 1:
  253. raise exceptions.ReflexError(
  254. f"Cannot merge var data with different positions: {positions}"
  255. )
  256. position = positions[0]
  257. else:
  258. position = None
  259. return VarData(
  260. state=state,
  261. field_name=field_name,
  262. imports=_imports,
  263. hooks=hooks,
  264. deps=deps,
  265. position=position,
  266. components=components,
  267. )
  268. def __bool__(self) -> bool:
  269. """Check if the var data is non-empty.
  270. Returns:
  271. True if any field is set to a non-default value.
  272. """
  273. return any(getattr(self, field.name) for field in dataclasses.fields(self))
  274. @classmethod
  275. def from_state(cls, state: Type[BaseState] | str, field_name: str = "") -> VarData:
  276. """Set the state of the var.
  277. Args:
  278. state: The state to set or the full name of the state.
  279. field_name: The name of the field in the state. Optional.
  280. Returns:
  281. The var with the set state.
  282. """
  283. from reflex.utils import format
  284. state_name = state if isinstance(state, str) else state.get_full_name()
  285. return VarData(
  286. state=state_name,
  287. field_name=field_name,
  288. hooks={
  289. "const {0} = useContext(StateContexts.{0})".format(
  290. format.format_state_name(state_name)
  291. ): None
  292. },
  293. imports={
  294. f"$/{constants.Dirs.CONTEXTS_PATH}": [ImportVar(tag="StateContexts")],
  295. "react": [ImportVar(tag="useContext")],
  296. },
  297. )
  298. def _decode_var_immutable(value: str) -> tuple[VarData | None, str]:
  299. """Decode the state name from a formatted var.
  300. Args:
  301. value: The value to extract the state name from.
  302. Returns:
  303. The extracted state name and the value without the state name.
  304. """
  305. var_datas = []
  306. if isinstance(value, str):
  307. # fast path if there is no encoded VarData
  308. if constants.REFLEX_VAR_OPENING_TAG not in value:
  309. return None, value
  310. offset = 0
  311. # Find all tags.
  312. while m := _decode_var_pattern.search(value):
  313. start, end = m.span()
  314. value = value[:start] + value[end:]
  315. serialized_data = m.group(1)
  316. if serialized_data.isnumeric() or (
  317. serialized_data[0] == "-" and serialized_data[1:].isnumeric()
  318. ):
  319. # This is a global immutable var.
  320. var = _global_vars[int(serialized_data)]
  321. var_data = var._get_all_var_data()
  322. if var_data is not None:
  323. var_datas.append(var_data)
  324. offset += end - start
  325. return VarData.merge(*var_datas) if var_datas else None, value
  326. def can_use_in_object_var(cls: GenericType) -> bool:
  327. """Check if the class can be used in an ObjectVar.
  328. Args:
  329. cls: The class to check.
  330. Returns:
  331. Whether the class can be used in an ObjectVar.
  332. """
  333. if types.is_union(cls):
  334. return all(can_use_in_object_var(t) for t in types.get_args(cls))
  335. return (
  336. inspect.isclass(cls)
  337. and not issubclass(cls, Var)
  338. and serializers.can_serialize(cls, dict)
  339. )
  340. @dataclasses.dataclass(
  341. eq=False,
  342. frozen=True,
  343. )
  344. class Var(Generic[VAR_TYPE]):
  345. """Base class for immutable vars."""
  346. # The name of the var.
  347. _js_expr: str = dataclasses.field()
  348. # The type of the var.
  349. _var_type: types.GenericType = dataclasses.field(default=Any)
  350. # Extra metadata associated with the Var
  351. _var_data: Optional[VarData] = dataclasses.field(default=None)
  352. def __str__(self) -> str:
  353. """String representation of the var. Guaranteed to be a valid Javascript expression.
  354. Returns:
  355. The name of the var.
  356. """
  357. return self._js_expr
  358. @property
  359. def _var_is_local(self) -> bool:
  360. """Whether this is a local javascript variable.
  361. Returns:
  362. False
  363. """
  364. return False
  365. @property
  366. @deprecated("Use `_js_expr` instead.")
  367. def _var_name(self) -> str:
  368. """The name of the var.
  369. Returns:
  370. The name of the var.
  371. """
  372. return self._js_expr
  373. @property
  374. def _var_field_name(self) -> str:
  375. """The name of the field.
  376. Returns:
  377. The name of the field.
  378. """
  379. var_data = self._get_all_var_data()
  380. field_name = var_data.field_name if var_data else None
  381. return field_name or self._js_expr
  382. @property
  383. @deprecated("Use `_js_expr` instead.")
  384. def _var_name_unwrapped(self) -> str:
  385. """The name of the var without extra curly braces.
  386. Returns:
  387. The name of the var.
  388. """
  389. return self._js_expr
  390. @property
  391. def _var_is_string(self) -> bool:
  392. """Whether the var is a string literal.
  393. Returns:
  394. False
  395. """
  396. return False
  397. def __init_subclass__(
  398. cls,
  399. python_types: Tuple[GenericType, ...] | GenericType = types.Unset(),
  400. default_type: GenericType = types.Unset(),
  401. is_subclass: Callable[[GenericType], bool] | types.Unset = types.Unset(),
  402. **kwargs,
  403. ):
  404. """Initialize the subclass.
  405. Args:
  406. python_types: The python types that the var represents.
  407. default_type: The default type of the var. Defaults to the first python type.
  408. is_subclass: A function to check if a type is a subclass of the var.
  409. **kwargs: Additional keyword arguments.
  410. """
  411. super().__init_subclass__(**kwargs)
  412. if python_types or default_type or is_subclass:
  413. python_types = (
  414. (python_types if isinstance(python_types, tuple) else (python_types,))
  415. if python_types
  416. else ()
  417. )
  418. default_type = default_type or (python_types[0] if python_types else Any)
  419. @dataclasses.dataclass(
  420. eq=False,
  421. frozen=True,
  422. **{"slots": True} if sys.version_info >= (3, 10) else {},
  423. )
  424. class ToVarOperation(ToOperation, cls):
  425. """Base class of converting a var to another var type."""
  426. _original: Var = dataclasses.field(
  427. default=Var(_js_expr="null", _var_type=None),
  428. )
  429. _default_var_type: ClassVar[GenericType] = default_type
  430. ToVarOperation.__name__ = f'To{cls.__name__.removesuffix("Var")}Operation'
  431. _var_subclasses.append(
  432. VarSubclassEntry(
  433. cls,
  434. ToVarOperation,
  435. python_types,
  436. is_subclass if not isinstance(is_subclass, types.Unset) else None,
  437. )
  438. )
  439. def __post_init__(self):
  440. """Post-initialize the var."""
  441. # Decode any inline Var markup and apply it to the instance
  442. _var_data, _js_expr = _decode_var_immutable(self._js_expr)
  443. if _var_data or _js_expr != self._js_expr:
  444. self.__init__(
  445. **{
  446. **dataclasses.asdict(self),
  447. "_js_expr": _js_expr,
  448. "_var_data": VarData.merge(self._var_data, _var_data),
  449. }
  450. )
  451. def __hash__(self) -> int:
  452. """Define a hash function for the var.
  453. Returns:
  454. The hash of the var.
  455. """
  456. return hash((self._js_expr, self._var_type, self._var_data))
  457. def _get_all_var_data(self) -> VarData | None:
  458. """Get all VarData associated with the Var.
  459. Returns:
  460. The VarData of the components and all of its children.
  461. """
  462. return self._var_data
  463. def equals(self, other: Var) -> bool:
  464. """Check if two vars are equal.
  465. Args:
  466. other: The other var to compare.
  467. Returns:
  468. Whether the vars are equal.
  469. """
  470. return (
  471. self._js_expr == other._js_expr
  472. and self._var_type == other._var_type
  473. and self._get_all_var_data() == other._get_all_var_data()
  474. )
  475. @overload
  476. def _replace(
  477. self, _var_type: Type[OTHER_VAR_TYPE], merge_var_data=None, **kwargs: Any
  478. ) -> Var[OTHER_VAR_TYPE]: ...
  479. @overload
  480. def _replace(
  481. self, _var_type: GenericType | None = None, merge_var_data=None, **kwargs: Any
  482. ) -> Self: ...
  483. def _replace(
  484. self, _var_type: GenericType | None = None, merge_var_data=None, **kwargs: Any
  485. ) -> Self | Var:
  486. """Make a copy of this Var with updated fields.
  487. Args:
  488. merge_var_data: VarData to merge into the existing VarData.
  489. **kwargs: Var fields to update.
  490. Returns:
  491. A new Var with the updated fields overwriting the corresponding fields in this Var.
  492. Raises:
  493. TypeError: If _var_is_local, _var_is_string, or _var_full_name_needs_state_prefix is not None.
  494. """
  495. if kwargs.get("_var_is_local", False) is not False:
  496. raise TypeError("The _var_is_local argument is not supported for Var.")
  497. if kwargs.get("_var_is_string", False) is not False:
  498. raise TypeError("The _var_is_string argument is not supported for Var.")
  499. if kwargs.get("_var_full_name_needs_state_prefix", False) is not False:
  500. raise TypeError(
  501. "The _var_full_name_needs_state_prefix argument is not supported for Var."
  502. )
  503. value_with_replaced = dataclasses.replace(
  504. self,
  505. _var_type=_var_type or self._var_type,
  506. _var_data=VarData.merge(
  507. kwargs.get("_var_data", self._var_data), merge_var_data
  508. ),
  509. **kwargs,
  510. )
  511. if (js_expr := kwargs.get("_js_expr")) is not None:
  512. object.__setattr__(value_with_replaced, "_js_expr", js_expr)
  513. return value_with_replaced
  514. @classmethod
  515. def create(
  516. cls,
  517. value: FAKE_VAR_TYPE,
  518. _var_is_local: bool | None = None,
  519. _var_is_string: bool | None = None,
  520. _var_data: VarData | None = None,
  521. ) -> Var[FAKE_VAR_TYPE]:
  522. """Create a var from a value.
  523. Args:
  524. value: The value to create the var from.
  525. _var_is_local: Whether the var is local. Deprecated.
  526. _var_is_string: Whether the var is a string literal. Deprecated.
  527. _var_data: Additional hooks and imports associated with the Var.
  528. Returns:
  529. The var.
  530. """
  531. if _var_is_local is not None:
  532. console.deprecate(
  533. feature_name="_var_is_local",
  534. reason="The _var_is_local argument is not supported for Var. "
  535. "If you want to create a Var from a raw Javascript expression, use the constructor directly",
  536. deprecation_version="0.6.0",
  537. removal_version="0.7.0",
  538. )
  539. if _var_is_string is not None:
  540. console.deprecate(
  541. feature_name="_var_is_string",
  542. reason="The _var_is_string argument is not supported for Var. "
  543. "If you want to create a Var from a raw Javascript expression, use the constructor directly",
  544. deprecation_version="0.6.0",
  545. removal_version="0.7.0",
  546. )
  547. # If the value is already a var, do nothing.
  548. if isinstance(value, Var):
  549. return value
  550. # Try to pull the imports and hooks from contained values.
  551. if not isinstance(value, str):
  552. return LiteralVar.create(value, _var_data=_var_data)
  553. if _var_is_string is False or _var_is_local is True:
  554. return Var(
  555. _js_expr=value,
  556. _var_data=_var_data,
  557. )
  558. return LiteralVar.create(value, _var_data=_var_data)
  559. @classmethod
  560. @deprecated("Use `.create()` instead.")
  561. def create_safe(
  562. cls,
  563. *args: Any,
  564. **kwargs: Any,
  565. ) -> Var:
  566. """Create a var from a value.
  567. Args:
  568. *args: The arguments to create the var from.
  569. **kwargs: The keyword arguments to create the var from.
  570. Returns:
  571. The var.
  572. """
  573. return cls.create(*args, **kwargs)
  574. def __format__(self, format_spec: str) -> str:
  575. """Format the var into a Javascript equivalent to an f-string.
  576. Args:
  577. format_spec: The format specifier (Ignored for now).
  578. Returns:
  579. The formatted var.
  580. """
  581. hashed_var = hash(self)
  582. _global_vars[hashed_var] = self
  583. # Encode the _var_data into the formatted output for tracking purposes.
  584. return f"{constants.REFLEX_VAR_OPENING_TAG}{hashed_var}{constants.REFLEX_VAR_CLOSING_TAG}{self._js_expr}"
  585. @overload
  586. def to(self, output: Type[bool]) -> BooleanVar: ... # pyright: ignore [reportOverlappingOverload]
  587. @overload
  588. def to(self, output: Type[int]) -> NumberVar[int]: ...
  589. @overload
  590. def to(self, output: type[float]) -> NumberVar[float]: ...
  591. @overload
  592. def to(self, output: Type[str]) -> StringVar: ... # pyright: ignore [reportOverlappingOverload]
  593. @overload
  594. def to(
  595. self,
  596. output: type[Sequence[VALUE]] | type[set[VALUE]],
  597. ) -> ArrayVar[Sequence[VALUE]]: ...
  598. @overload
  599. def to(
  600. self,
  601. output: type[dict],
  602. ) -> ObjectVar[dict]: ...
  603. @overload
  604. def to(
  605. self, output: Type[ObjectVar], var_type: Type[VAR_INSIDE]
  606. ) -> ObjectVar[VAR_INSIDE]: ...
  607. @overload
  608. def to(
  609. self, output: Type[ObjectVar], var_type: None = None
  610. ) -> ObjectVar[VAR_TYPE]: ...
  611. @overload
  612. def to(self, output: VAR_SUBCLASS, var_type: None = None) -> VAR_SUBCLASS: ...
  613. @overload
  614. def to(
  615. self,
  616. output: Type[OUTPUT] | types.GenericType,
  617. var_type: types.GenericType | None = None,
  618. ) -> OUTPUT: ...
  619. def to(
  620. self,
  621. output: Type[OUTPUT] | types.GenericType,
  622. var_type: types.GenericType | None = None,
  623. ) -> Var:
  624. """Convert the var to a different type.
  625. Args:
  626. output: The output type.
  627. var_type: The type of the var.
  628. Returns:
  629. The converted var.
  630. """
  631. from .object import ObjectVar
  632. fixed_output_type = get_origin(output) or output
  633. # If the first argument is a python type, we map it to the corresponding Var type.
  634. for var_subclass in _var_subclasses[::-1]:
  635. if (
  636. var_subclass.python_types
  637. and infallible_issubclass(fixed_output_type, var_subclass.python_types)
  638. ) or (
  639. var_subclass.is_subclass and var_subclass.is_subclass(fixed_output_type)
  640. ):
  641. return self.to(var_subclass.var_subclass, output)
  642. if fixed_output_type is None:
  643. return get_to_operation(NoneVar).create(self) # type: ignore
  644. # Handle fixed_output_type being Base or a dataclass.
  645. if can_use_in_object_var(fixed_output_type):
  646. return self.to(ObjectVar, output)
  647. if inspect.isclass(output):
  648. for var_subclass in _var_subclasses[::-1]:
  649. if issubclass(output, var_subclass.var_subclass):
  650. current_var_type = self._var_type
  651. if current_var_type is Any:
  652. new_var_type = var_type
  653. else:
  654. new_var_type = var_type or current_var_type
  655. to_operation_return = var_subclass.to_var_subclass.create(
  656. value=self, _var_type=new_var_type
  657. )
  658. return to_operation_return # type: ignore
  659. # If we can't determine the first argument, we just replace the _var_type.
  660. if not issubclass(output, Var) or var_type is None:
  661. return dataclasses.replace(
  662. self,
  663. _var_type=output,
  664. )
  665. # We couldn't determine the output type to be any other Var type, so we replace the _var_type.
  666. if var_type is not None:
  667. return dataclasses.replace(
  668. self,
  669. _var_type=var_type,
  670. )
  671. return self
  672. # We use `NoReturn` here to catch `Var[Any]` and `Var[Unknown]` cases first.
  673. @overload
  674. def guess_type(self: Var[NoReturn]) -> Var: ... # pyright: ignore [reportOverlappingOverload]
  675. @overload
  676. def guess_type(self: Var[bool]) -> BooleanVar: ...
  677. @overload
  678. def guess_type(self: Var[INT_OR_FLOAT]) -> NumberVar[INT_OR_FLOAT]: ...
  679. @overload
  680. def guess_type(self: Var[str]) -> StringVar: ... # pyright: ignore [reportOverlappingOverload]
  681. @overload
  682. def guess_type(self: Var[Sequence[VALUE]]) -> ArrayVar[Sequence[VALUE]]: ...
  683. @overload
  684. def guess_type(self: Var[Set[VALUE]]) -> ArrayVar[Set[VALUE]]: ...
  685. @overload
  686. def guess_type(
  687. self: Var[Dict[VALUE, OTHER_VAR_TYPE]],
  688. ) -> ObjectVar[Dict[VALUE, OTHER_VAR_TYPE]]: ...
  689. @overload
  690. def guess_type(self) -> Self: ...
  691. def guess_type(self) -> Var:
  692. """Guesses the type of the variable based on its `_var_type` attribute.
  693. Returns:
  694. Var: The guessed type of the variable.
  695. Raises:
  696. TypeError: If the type is not supported for guessing.
  697. """
  698. from .object import ObjectVar
  699. var_type = self._var_type
  700. if var_type is None:
  701. return self.to(None)
  702. if types.is_optional(var_type):
  703. var_type = types.get_args(var_type)[0]
  704. if var_type is Any:
  705. return self
  706. fixed_type = get_origin(var_type) or var_type
  707. if fixed_type in types.UnionTypes:
  708. inner_types = get_args(var_type)
  709. for var_subclass in _var_subclasses:
  710. if all(
  711. (
  712. infallible_issubclass(t, var_subclass.python_types)
  713. or (var_subclass.is_subclass and var_subclass.is_subclass(t))
  714. )
  715. for t in inner_types
  716. ):
  717. return self.to(var_subclass.var_subclass, self._var_type)
  718. if can_use_in_object_var(var_type):
  719. return self.to(ObjectVar, self._var_type)
  720. return self
  721. if fixed_type is Literal:
  722. args = get_args(var_type)
  723. fixed_type = unionize(*(type(arg) for arg in args))
  724. if not inspect.isclass(fixed_type):
  725. raise TypeError(f"Unsupported type {var_type} for guess_type.")
  726. if fixed_type is None:
  727. return self.to(None)
  728. for var_subclass in _var_subclasses[::-1]:
  729. if infallible_issubclass(fixed_type, var_subclass.python_types) or (
  730. var_subclass.is_subclass and var_subclass.is_subclass(fixed_type)
  731. ):
  732. return self.to(var_subclass.var_subclass, self._var_type)
  733. if can_use_in_object_var(fixed_type):
  734. return self.to(ObjectVar, self._var_type)
  735. return self
  736. def _get_default_value(self) -> Any:
  737. """Get the default value of the var.
  738. Returns:
  739. The default value of the var.
  740. Raises:
  741. ImportError: If the var is a dataframe and pandas is not installed.
  742. """
  743. if types.is_optional(self._var_type):
  744. return None
  745. type_ = (
  746. get_origin(self._var_type)
  747. if types.is_generic_alias(self._var_type)
  748. else self._var_type
  749. )
  750. if type_ is Literal:
  751. args = get_args(self._var_type)
  752. return args[0] if args else None
  753. if issubclass(type_, str):
  754. return ""
  755. if issubclass(type_, types.get_args(Union[int, float])):
  756. return 0
  757. if issubclass(type_, bool):
  758. return False
  759. if issubclass(type_, list):
  760. return []
  761. if issubclass(type_, dict):
  762. return {}
  763. if issubclass(type_, tuple):
  764. return ()
  765. if types.is_dataframe(type_):
  766. try:
  767. import pandas as pd
  768. return pd.DataFrame()
  769. except ImportError as e:
  770. raise ImportError(
  771. "Please install pandas to use dataframes in your app."
  772. ) from e
  773. return set() if issubclass(type_, set) else None
  774. def _get_setter_name(self, include_state: bool = True) -> str:
  775. """Get the name of the var's generated setter function.
  776. Args:
  777. include_state: Whether to include the state name in the setter name.
  778. Returns:
  779. The name of the setter function.
  780. """
  781. setter = constants.SETTER_PREFIX + self._var_field_name
  782. var_data = self._get_all_var_data()
  783. if var_data is None:
  784. return setter
  785. if not include_state or var_data.state == "":
  786. return setter
  787. return ".".join((var_data.state, setter))
  788. def _get_setter(self) -> Callable[[BaseState, Any], None]:
  789. """Get the var's setter function.
  790. Returns:
  791. A function that that creates a setter for the var.
  792. """
  793. actual_name = self._var_field_name
  794. def setter(state: BaseState, value: Any):
  795. """Get the setter for the var.
  796. Args:
  797. state: The state within which we add the setter function.
  798. value: The value to set.
  799. """
  800. if self._var_type in [int, float]:
  801. try:
  802. value = self._var_type(value)
  803. setattr(state, actual_name, value)
  804. except ValueError:
  805. console.debug(
  806. f"{type(state).__name__}.{self._js_expr}: Failed conversion of {value} to '{self._var_type.__name__}'. Value not set.",
  807. )
  808. else:
  809. setattr(state, actual_name, value)
  810. setter.__qualname__ = self._get_setter_name()
  811. return setter
  812. def _var_set_state(self, state: type[BaseState] | str):
  813. """Set the state of the var.
  814. Args:
  815. state: The state to set.
  816. Returns:
  817. The var with the state set.
  818. """
  819. formatted_state_name = (
  820. state
  821. if isinstance(state, str)
  822. else format_state_name(state.get_full_name())
  823. )
  824. return StateOperation.create(
  825. formatted_state_name,
  826. self,
  827. _var_data=VarData.merge(
  828. VarData.from_state(state, self._js_expr), self._var_data
  829. ),
  830. ).guess_type()
  831. def __eq__(self, other: Var | Any) -> BooleanVar:
  832. """Check if the current variable is equal to the given variable.
  833. Args:
  834. other (Var | Any): The variable to compare with.
  835. Returns:
  836. BooleanVar: A BooleanVar object representing the result of the equality check.
  837. """
  838. from .number import equal_operation
  839. return equal_operation(self, other).guess_type()
  840. def __ne__(self, other: Var | Any) -> BooleanVar:
  841. """Check if the current object is not equal to the given object.
  842. Parameters:
  843. other (Var | Any): The object to compare with.
  844. Returns:
  845. BooleanVar: A BooleanVar object representing the result of the comparison.
  846. """
  847. from .number import equal_operation
  848. return (~equal_operation(self, other)).guess_type()
  849. def bool(self) -> BooleanVar:
  850. """Convert the var to a boolean.
  851. Returns:
  852. The boolean var.
  853. """
  854. from .number import boolify
  855. return boolify(self) # pyright: ignore [reportReturnType]
  856. def __and__(self, other: Var | Any) -> Var:
  857. """Perform a logical AND operation on the current instance and another variable.
  858. Args:
  859. other: The variable to perform the logical AND operation with.
  860. Returns:
  861. A `BooleanVar` object representing the result of the logical AND operation.
  862. """
  863. return and_operation(self, other)
  864. def __rand__(self, other: Var | Any) -> Var:
  865. """Perform a logical AND operation on the current instance and another variable.
  866. Args:
  867. other: The variable to perform the logical AND operation with.
  868. Returns:
  869. A `BooleanVar` object representing the result of the logical AND operation.
  870. """
  871. return and_operation(other, self)
  872. def __or__(self, other: Var | Any) -> Var:
  873. """Perform a logical OR operation on the current instance and another variable.
  874. Args:
  875. other: The variable to perform the logical OR operation with.
  876. Returns:
  877. A `BooleanVar` object representing the result of the logical OR operation.
  878. """
  879. return or_operation(self, other)
  880. def __ror__(self, other: Var | Any) -> Var:
  881. """Perform a logical OR operation on the current instance and another variable.
  882. Args:
  883. other: The variable to perform the logical OR operation with.
  884. Returns:
  885. A `BooleanVar` object representing the result of the logical OR operation.
  886. """
  887. return or_operation(other, self)
  888. def __invert__(self) -> BooleanVar:
  889. """Perform a logical NOT operation on the current instance.
  890. Returns:
  891. A `BooleanVar` object representing the result of the logical NOT operation.
  892. """
  893. return (~self.bool()).guess_type()
  894. def to_string(self, use_json: bool = True) -> StringVar:
  895. """Convert the var to a string.
  896. Args:
  897. use_json: Whether to use JSON stringify. If False, uses Object.prototype.toString.
  898. Returns:
  899. The string var.
  900. """
  901. from .function import JSON_STRINGIFY, PROTOTYPE_TO_STRING
  902. from .sequence import StringVar
  903. return (
  904. JSON_STRINGIFY.call(self).to(StringVar)
  905. if use_json
  906. else PROTOTYPE_TO_STRING.call(self).to(StringVar)
  907. )
  908. def _as_ref(self) -> Var:
  909. """Get a reference to the var.
  910. Returns:
  911. The reference to the var.
  912. """
  913. from .object import ObjectVar
  914. refs = Var(
  915. _js_expr="refs",
  916. _var_data=VarData(
  917. imports={
  918. f"$/{constants.Dirs.STATE_PATH}": [imports.ImportVar(tag="refs")]
  919. }
  920. ),
  921. ).to(ObjectVar, Dict[str, str])
  922. return refs[LiteralVar.create(str(self))]
  923. @deprecated("Use `.js_type()` instead.")
  924. def _type(self) -> StringVar:
  925. """Returns the type of the object.
  926. This method uses the `typeof` function from the `FunctionStringVar` class
  927. to determine the type of the object.
  928. Returns:
  929. StringVar: A string variable representing the type of the object.
  930. """
  931. return self.js_type()
  932. def js_type(self) -> StringVar:
  933. """Returns the javascript type of the object.
  934. This method uses the `typeof` function from the `FunctionStringVar` class
  935. to determine the type of the object.
  936. Returns:
  937. StringVar: A string variable representing the type of the object.
  938. """
  939. from .function import FunctionStringVar
  940. from .sequence import StringVar
  941. type_of = FunctionStringVar("typeof")
  942. return type_of.call(self).to(StringVar)
  943. def _without_data(self):
  944. """Create a copy of the var without the data.
  945. Returns:
  946. The var without the data.
  947. """
  948. return dataclasses.replace(self, _var_data=None)
  949. def __get__(self, instance: Any, owner: Any):
  950. """Get the var.
  951. Args:
  952. instance: The instance to get the var from.
  953. owner: The owner of the var.
  954. Returns:
  955. The var.
  956. """
  957. return self
  958. def __getattribute__(self, name: str) -> Any:
  959. """Get an attribute of the var.
  960. Args:
  961. name: The name of the attribute.
  962. Returns:
  963. The attribute.
  964. """
  965. if not _VAR_CACHING:
  966. try:
  967. self_dict = object.__getattribute__(self, "__dict__")
  968. for key in self_dict:
  969. if key.startswith("_cached_"):
  970. del self_dict[key]
  971. except Exception:
  972. pass
  973. return super().__getattribute__(name)
  974. def __getattr__(self, name: str):
  975. """Get an attribute of the var.
  976. Args:
  977. name: The name of the attribute.
  978. Returns:
  979. The attribute.
  980. Raises:
  981. VarAttributeError: If the attribute does not exist.
  982. TypeError: If the var type is Any.
  983. """
  984. if name.startswith("_"):
  985. return self.__getattribute__(name)
  986. if name == "contains":
  987. raise TypeError(
  988. f"Var of type {self._var_type} does not support contains check."
  989. )
  990. if name == "reverse":
  991. raise TypeError("Cannot reverse non-list var.")
  992. if self._var_type is Any:
  993. raise TypeError(
  994. f"You must provide an annotation for the state var `{self!s}`. Annotation cannot be `{self._var_type}`."
  995. )
  996. if name in REPLACED_NAMES:
  997. raise VarAttributeError(
  998. f"Field {name!r} was renamed to {REPLACED_NAMES[name]!r}"
  999. )
  1000. raise VarAttributeError(
  1001. f"The State var has no attribute '{name}' or may have been annotated wrongly.",
  1002. )
  1003. def _decode(self) -> Any:
  1004. """Decode Var as a python value.
  1005. Note that Var with state set cannot be decoded python-side and will be
  1006. returned as full_name.
  1007. Returns:
  1008. The decoded value or the Var name.
  1009. """
  1010. if isinstance(self, LiteralVar):
  1011. return self._var_value # type: ignore
  1012. try:
  1013. return json.loads(str(self))
  1014. except ValueError:
  1015. return str(self)
  1016. @property
  1017. def _var_state(self) -> str:
  1018. """Compat method for getting the state.
  1019. Returns:
  1020. The state name associated with the var.
  1021. """
  1022. var_data = self._get_all_var_data()
  1023. return var_data.state if var_data else ""
  1024. @overload
  1025. @classmethod
  1026. def range(cls, stop: int | NumberVar, /) -> ArrayVar[Sequence[int]]: ...
  1027. @overload
  1028. @classmethod
  1029. def range(
  1030. cls,
  1031. start: int | NumberVar,
  1032. end: int | NumberVar,
  1033. step: int | NumberVar = 1,
  1034. /,
  1035. ) -> ArrayVar[Sequence[int]]: ...
  1036. @classmethod
  1037. def range(
  1038. cls,
  1039. first_endpoint: int | Var[int],
  1040. second_endpoint: int | Var[int] | None = None,
  1041. step: int | Var[int] | None = None,
  1042. /,
  1043. ) -> ArrayVar[Sequence[int]]:
  1044. """Create a range of numbers.
  1045. Args:
  1046. first_endpoint: The end of the range if second_endpoint is not provided, otherwise the start of the range.
  1047. second_endpoint: The end of the range.
  1048. step: The step of the range.
  1049. Returns:
  1050. The range of numbers.
  1051. """
  1052. from .sequence import ArrayVar
  1053. if second_endpoint is None:
  1054. return ArrayVar.range.call(first_endpoint).guess_type()
  1055. if step is None:
  1056. return ArrayVar.range.call(first_endpoint, second_endpoint).guess_type()
  1057. return ArrayVar.range.call(first_endpoint, second_endpoint, step).guess_type()
  1058. def __bool__(self) -> bool:
  1059. """Raise exception if using Var in a boolean context.
  1060. Raises:
  1061. VarTypeError: when attempting to bool-ify the Var.
  1062. """
  1063. raise VarTypeError(
  1064. f"Cannot convert Var {str(self)!r} to bool for use with `if`, `and`, `or`, and `not`. "
  1065. "Instead use `rx.cond` and bitwise operators `&` (and), `|` (or), `~` (invert)."
  1066. )
  1067. def __iter__(self) -> Any:
  1068. """Raise exception if using Var in an iterable context.
  1069. Raises:
  1070. VarTypeError: when attempting to iterate over the Var.
  1071. """
  1072. raise VarTypeError(
  1073. f"Cannot iterate over Var {str(self)!r}. Instead use `rx.foreach`."
  1074. )
  1075. def __contains__(self, _: Any) -> Var:
  1076. """Override the 'in' operator to alert the user that it is not supported.
  1077. Raises:
  1078. VarTypeError: the operation is not supported
  1079. """
  1080. raise VarTypeError(
  1081. "'in' operator not supported for Var types, use Var.contains() instead."
  1082. )
  1083. OUTPUT = TypeVar("OUTPUT", bound=Var)
  1084. VAR_SUBCLASS = TypeVar("VAR_SUBCLASS", bound=Var)
  1085. VAR_INSIDE = TypeVar("VAR_INSIDE")
  1086. class VarWithDefault(Var[VAR_TYPE]):
  1087. """Annotate an optional argument."""
  1088. def __init__(self, default_value: VAR_TYPE):
  1089. """Initialize the default value.
  1090. Args:
  1091. default_value: The default value.
  1092. """
  1093. super().__init__("")
  1094. self._default = default_value
  1095. @property
  1096. def default(self) -> Var[VAR_TYPE]:
  1097. """Get the default value.
  1098. Returns:
  1099. The default value.
  1100. """
  1101. return Var.create(self._default)
  1102. class ToOperation:
  1103. """A var operation that converts a var to another type."""
  1104. def __getattr__(self, name: str) -> Any:
  1105. """Get an attribute of the var.
  1106. Args:
  1107. name: The name of the attribute.
  1108. Returns:
  1109. The attribute of the var.
  1110. """
  1111. from .object import ObjectVar
  1112. if isinstance(self, ObjectVar) and name != "_js_expr":
  1113. return ObjectVar.__getattr__(self, name)
  1114. return getattr(self._original, name)
  1115. def __post_init__(self):
  1116. """Post initialization."""
  1117. object.__delattr__(self, "_js_expr")
  1118. def __hash__(self) -> int:
  1119. """Calculate the hash value of the object.
  1120. Returns:
  1121. int: The hash value of the object.
  1122. """
  1123. return hash(self._original)
  1124. def _get_all_var_data(self) -> VarData | None:
  1125. """Get all the var data.
  1126. Returns:
  1127. The var data.
  1128. """
  1129. return VarData.merge(
  1130. self._original._get_all_var_data(),
  1131. self._var_data, # type: ignore
  1132. )
  1133. @classmethod
  1134. def create(
  1135. cls,
  1136. value: Var,
  1137. _var_type: GenericType | None = None,
  1138. _var_data: VarData | None = None,
  1139. ):
  1140. """Create a ToOperation.
  1141. Args:
  1142. value: The value of the var.
  1143. _var_type: The type of the Var.
  1144. _var_data: Additional hooks and imports associated with the Var.
  1145. Returns:
  1146. The ToOperation.
  1147. """
  1148. return cls(
  1149. _js_expr="", # type: ignore
  1150. _var_data=_var_data, # type: ignore
  1151. _var_type=_var_type or cls._default_var_type, # type: ignore
  1152. _original=value, # type: ignore
  1153. )
  1154. class LiteralVar(Var):
  1155. """Base class for immutable literal vars."""
  1156. def __init_subclass__(cls, **kwargs):
  1157. """Initialize the subclass.
  1158. Args:
  1159. **kwargs: Additional keyword arguments.
  1160. Raises:
  1161. TypeError: If the LiteralVar subclass does not have a corresponding Var subclass.
  1162. """
  1163. super().__init_subclass__(**kwargs)
  1164. bases = cls.__bases__
  1165. bases_normalized = [
  1166. base if inspect.isclass(base) else get_origin(base) for base in bases
  1167. ]
  1168. possible_bases = [
  1169. base
  1170. for base in bases_normalized
  1171. if issubclass(base, Var) and base != LiteralVar
  1172. ]
  1173. if not possible_bases:
  1174. raise TypeError(
  1175. f"LiteralVar subclass {cls} must have a base class that is a subclass of Var and not LiteralVar."
  1176. )
  1177. var_subclasses = [
  1178. var_subclass
  1179. for var_subclass in _var_subclasses
  1180. if var_subclass.var_subclass in possible_bases
  1181. ]
  1182. if not var_subclasses:
  1183. raise TypeError(
  1184. f"LiteralVar {cls} must have a base class annotated with `python_types`."
  1185. )
  1186. if len(var_subclasses) != 1:
  1187. raise TypeError(
  1188. f"LiteralVar {cls} must have exactly one base class annotated with `python_types`."
  1189. )
  1190. var_subclass = var_subclasses[0]
  1191. # Remove the old subclass, happens because __init_subclass__ is called twice
  1192. # for each subclass. This is because of __slots__ in dataclasses.
  1193. for var_literal_subclass in list(_var_literal_subclasses):
  1194. if var_literal_subclass[1] is var_subclass:
  1195. _var_literal_subclasses.remove(var_literal_subclass)
  1196. _var_literal_subclasses.append((cls, var_subclass))
  1197. @classmethod
  1198. def create(
  1199. cls,
  1200. value: Any,
  1201. _var_data: VarData | None = None,
  1202. ) -> Var:
  1203. """Create a var from a value.
  1204. Args:
  1205. value: The value to create the var from.
  1206. _var_data: Additional hooks and imports associated with the Var.
  1207. Returns:
  1208. The var.
  1209. Raises:
  1210. TypeError: If the value is not a supported type for LiteralVar.
  1211. """
  1212. if isinstance(value, Var):
  1213. if _var_data is None:
  1214. return value
  1215. return value._replace(merge_var_data=_var_data)
  1216. for literal_subclass, var_subclass in _var_literal_subclasses[::-1]:
  1217. if isinstance(value, var_subclass.python_types):
  1218. return literal_subclass.create(value, _var_data=_var_data)
  1219. from reflex.event import EventHandler
  1220. from reflex.utils.format import get_event_handler_parts
  1221. from .object import LiteralObjectVar
  1222. from .sequence import LiteralStringVar
  1223. if isinstance(value, EventHandler):
  1224. return Var(_js_expr=".".join(filter(None, get_event_handler_parts(value))))
  1225. serialized_value = serializers.serialize(value)
  1226. if serialized_value is not None:
  1227. if isinstance(serialized_value, dict):
  1228. return LiteralObjectVar.create(
  1229. serialized_value,
  1230. _var_type=type(value),
  1231. _var_data=_var_data,
  1232. )
  1233. if isinstance(serialized_value, str):
  1234. return LiteralStringVar.create(
  1235. serialized_value, _var_type=type(value), _var_data=_var_data
  1236. )
  1237. return LiteralVar.create(serialized_value, _var_data=_var_data)
  1238. if isinstance(value, Base):
  1239. # get the fields of the pydantic class
  1240. fields = value.__fields__.keys()
  1241. one_level_dict = {field: getattr(value, field) for field in fields}
  1242. return LiteralObjectVar.create(
  1243. {
  1244. field: value
  1245. for field, value in one_level_dict.items()
  1246. if not callable(value)
  1247. },
  1248. _var_type=type(value),
  1249. _var_data=_var_data,
  1250. )
  1251. if dataclasses.is_dataclass(value) and not isinstance(value, type):
  1252. return LiteralObjectVar.create(
  1253. {
  1254. k: (None if callable(v) else v)
  1255. for k, v in dataclasses.asdict(value).items()
  1256. },
  1257. _var_type=type(value),
  1258. _var_data=_var_data,
  1259. )
  1260. raise TypeError(
  1261. f"Unsupported type {type(value)} for LiteralVar. Tried to create a LiteralVar from {value}."
  1262. )
  1263. def __post_init__(self):
  1264. """Post-initialize the var."""
  1265. def json(self) -> str:
  1266. """Serialize the var to a JSON string.
  1267. Raises:
  1268. NotImplementedError: If the method is not implemented.
  1269. """
  1270. raise NotImplementedError(
  1271. "LiteralVar subclasses must implement the json method."
  1272. )
  1273. @serializers.serializer
  1274. def serialize_literal(value: LiteralVar):
  1275. """Serialize a Literal type.
  1276. Args:
  1277. value: The Literal to serialize.
  1278. Returns:
  1279. The serialized Literal.
  1280. """
  1281. return value._var_value
  1282. def get_python_literal(value: Union[LiteralVar, Any]) -> Any | None:
  1283. """Get the Python literal value.
  1284. Args:
  1285. value: The value to get the Python literal value of.
  1286. Returns:
  1287. The Python literal value.
  1288. """
  1289. if isinstance(value, LiteralVar):
  1290. return value._var_value
  1291. if isinstance(value, Var):
  1292. return None
  1293. return value
  1294. def validate_arg(type_hint: GenericType) -> Callable[[Any], str | None]:
  1295. """Create a validator for an argument.
  1296. Args:
  1297. type_hint: The type hint of the argument.
  1298. Returns:
  1299. The validator.
  1300. """
  1301. def validate(value: Any):
  1302. if isinstance(value, LiteralVar):
  1303. if not _isinstance(value._var_value, type_hint):
  1304. return f"Expected {type_hint} but got {value._var_value} of type {type(value._var_value)}."
  1305. elif isinstance(value, Var):
  1306. if not typehint_issubclass(value._var_type, type_hint):
  1307. return f"Expected {type_hint} but got {value._var_type}."
  1308. else:
  1309. if not _isinstance(value, type_hint):
  1310. return f"Expected {type_hint} but got {value} of type {type(value)}."
  1311. return validate
  1312. P = ParamSpec("P")
  1313. T = TypeVar("T")
  1314. V1 = TypeVar("V1")
  1315. V2 = TypeVar("V2")
  1316. V3 = TypeVar("V3")
  1317. V4 = TypeVar("V4")
  1318. V5 = TypeVar("V5")
  1319. class TypeComputer(Protocol):
  1320. """A protocol for type computers."""
  1321. def __call__(self, *args: Var) -> Tuple[GenericType, Union[TypeComputer, None]]:
  1322. """Compute the type of the operation.
  1323. Args:
  1324. *args: The arguments to compute the type of.
  1325. """
  1326. ...
  1327. @overload
  1328. def var_operation(
  1329. func: Callable[[Var[V1], Var[V2], Var[V3]], CustomVarOperationReturn[T]],
  1330. ) -> ArgsFunctionOperation[ReflexCallable[[V1, V2, V3], T]]: ...
  1331. @overload
  1332. def var_operation(
  1333. func: Callable[[Var[V1], Var[V2], VarWithDefault[V3]], CustomVarOperationReturn[T]],
  1334. ) -> ArgsFunctionOperation[ReflexCallable[[V1, V2, VarWithDefault[V3]], T]]: ...
  1335. @overload
  1336. def var_operation(
  1337. func: Callable[
  1338. [
  1339. Var[V1],
  1340. VarWithDefault[V2],
  1341. VarWithDefault[V3],
  1342. ],
  1343. CustomVarOperationReturn[T],
  1344. ],
  1345. ) -> ArgsFunctionOperation[
  1346. ReflexCallable[
  1347. [
  1348. V1,
  1349. VarWithDefault[V2],
  1350. VarWithDefault[V3],
  1351. ],
  1352. T,
  1353. ]
  1354. ]: ...
  1355. @overload
  1356. def var_operation(
  1357. func: Callable[
  1358. [
  1359. VarWithDefault[V1],
  1360. VarWithDefault[V2],
  1361. VarWithDefault[V3],
  1362. ],
  1363. CustomVarOperationReturn[T],
  1364. ],
  1365. ) -> ArgsFunctionOperation[
  1366. ReflexCallable[
  1367. [
  1368. VarWithDefault[V1],
  1369. VarWithDefault[V1],
  1370. VarWithDefault[V1],
  1371. ],
  1372. T,
  1373. ]
  1374. ]: ...
  1375. @overload
  1376. def var_operation(
  1377. func: Callable[[Var[V1], Var[V2]], CustomVarOperationReturn[T]],
  1378. ) -> ArgsFunctionOperation[ReflexCallable[[V1, V2], T]]: ...
  1379. @overload
  1380. def var_operation(
  1381. func: Callable[
  1382. [
  1383. Var[V1],
  1384. VarWithDefault[V2],
  1385. ],
  1386. CustomVarOperationReturn[T],
  1387. ],
  1388. ) -> ArgsFunctionOperation[
  1389. ReflexCallable[
  1390. [
  1391. V1,
  1392. VarWithDefault[V2],
  1393. ],
  1394. T,
  1395. ]
  1396. ]: ...
  1397. @overload
  1398. def var_operation(
  1399. func: Callable[
  1400. [
  1401. VarWithDefault[V1],
  1402. VarWithDefault[V2],
  1403. ],
  1404. CustomVarOperationReturn[T],
  1405. ],
  1406. ) -> ArgsFunctionOperation[
  1407. ReflexCallable[
  1408. [
  1409. VarWithDefault[V1],
  1410. VarWithDefault[V2],
  1411. ],
  1412. T,
  1413. ]
  1414. ]: ...
  1415. @overload
  1416. def var_operation(
  1417. func: Callable[[Var[V1]], CustomVarOperationReturn[T]],
  1418. ) -> ArgsFunctionOperation[ReflexCallable[[V1], T]]: ...
  1419. @overload
  1420. def var_operation(
  1421. func: Callable[
  1422. [VarWithDefault[V1]],
  1423. CustomVarOperationReturn[T],
  1424. ],
  1425. ) -> ArgsFunctionOperation[
  1426. ReflexCallable[
  1427. [VarWithDefault[V1]],
  1428. T,
  1429. ]
  1430. ]: ...
  1431. @overload
  1432. def var_operation(
  1433. func: Callable[[], CustomVarOperationReturn[T]],
  1434. ) -> ArgsFunctionOperation[ReflexCallable[[], T]]: ...
  1435. def var_operation(
  1436. func: Callable[..., CustomVarOperationReturn[T]],
  1437. ) -> ArgsFunctionOperation[ReflexCallable[..., T]]:
  1438. """Decorator for creating a var operation.
  1439. Example:
  1440. ```python
  1441. @var_operation
  1442. def add(a: Var[int], b: Var[int]):
  1443. return custom_var_operation(f"{a} + {b}")
  1444. ```
  1445. Args:
  1446. func: The function to decorate.
  1447. Returns:
  1448. The decorated function.
  1449. Raises:
  1450. TypeError: If the function has keyword-only arguments or arguments without Var type hints.
  1451. """
  1452. from .function import ArgsFunctionOperation, ReflexCallable
  1453. func_name = func.__name__
  1454. func_arg_spec = inspect.getfullargspec(func)
  1455. func_signature = inspect.signature(func)
  1456. if func_arg_spec.kwonlyargs:
  1457. raise TypeError(f"Function {func_name} cannot have keyword-only arguments.")
  1458. if func_arg_spec.varargs:
  1459. raise TypeError(f"Function {func_name} cannot have variable arguments.")
  1460. arg_names = func_arg_spec.args
  1461. arg_default_values: Sequence[inspect.Parameter.empty | VarWithDefault] = tuple(
  1462. (
  1463. default_value
  1464. if isinstance(
  1465. (default_value := func_signature.parameters[arg_name].default),
  1466. VarWithDefault,
  1467. )
  1468. else inspect.Parameter.empty()
  1469. )
  1470. for arg_name in arg_names
  1471. )
  1472. type_hints = get_type_hints(func)
  1473. if not all(
  1474. (get_origin((type_hint := type_hints.get(arg_name, Any))) or type_hint)
  1475. in (Var, VarWithDefault)
  1476. and len(get_args(type_hint)) <= 1
  1477. for arg_name in arg_names
  1478. ):
  1479. raise TypeError(
  1480. f"Function {func_name} must have type hints of the form `Var[Type]`."
  1481. )
  1482. args_with_type_hints = tuple(
  1483. (arg_name, (args[0] if (args := get_args(type_hints[arg_name])) else Any))
  1484. for arg_name in arg_names
  1485. )
  1486. arg_vars = tuple(
  1487. (
  1488. Var("_" + arg_name, _var_type=arg_python_type)
  1489. if not isinstance(arg_python_type, TypeVar)
  1490. else Var("_" + arg_name)
  1491. )
  1492. for arg_name, arg_python_type in args_with_type_hints
  1493. )
  1494. custom_operation_return = func(*arg_vars)
  1495. args_operation = ArgsFunctionOperation.create(
  1496. tuple(map(str, arg_vars)),
  1497. custom_operation_return,
  1498. default_values=arg_default_values,
  1499. validators=tuple(
  1500. validate_arg(arg_type)
  1501. if not isinstance(arg_type, TypeVar)
  1502. else validate_arg(arg_type.__bound__ or Any)
  1503. for _, arg_type in args_with_type_hints
  1504. ),
  1505. function_name=func_name,
  1506. type_computer=custom_operation_return._type_computer,
  1507. _raw_js_function=custom_operation_return._raw_js_function,
  1508. _var_type=ReflexCallable[
  1509. tuple(
  1510. arg_python_type
  1511. if isinstance(arg_default_values[i], inspect.Parameter)
  1512. else VarWithDefault[arg_python_type]
  1513. for i, (_, arg_python_type) in enumerate(args_with_type_hints)
  1514. ), # type: ignore
  1515. custom_operation_return._var_type,
  1516. ],
  1517. )
  1518. return args_operation
  1519. def figure_out_type(value: Any) -> types.GenericType:
  1520. """Figure out the type of the value.
  1521. Args:
  1522. value: The value to figure out the type of.
  1523. Returns:
  1524. The type of the value.
  1525. """
  1526. if isinstance(value, Var):
  1527. return value._var_type
  1528. type_ = type(value)
  1529. if has_args(type_):
  1530. return type_
  1531. if isinstance(value, list):
  1532. return List[unionize(*(figure_out_type(v) for v in value))]
  1533. if isinstance(value, set):
  1534. return Set[unionize(*(figure_out_type(v) for v in value))]
  1535. if isinstance(value, tuple):
  1536. return Tuple[unionize(*(figure_out_type(v) for v in value)), ...]
  1537. if isinstance(value, dict):
  1538. return Dict[
  1539. unionize(*(figure_out_type(k) for k in value)),
  1540. unionize(*(figure_out_type(v) for v in value.values())),
  1541. ]
  1542. return type(value)
  1543. class cached_property_no_lock(functools.cached_property):
  1544. """A special version of functools.cached_property that does not use a lock."""
  1545. def __init__(self, func):
  1546. """Initialize the cached_property_no_lock.
  1547. Args:
  1548. func: The function to cache.
  1549. """
  1550. super().__init__(func)
  1551. self.lock = contextlib.nullcontext()
  1552. class CachedVarOperation:
  1553. """Base class for cached var operations to lower boilerplate code."""
  1554. def __post_init__(self):
  1555. """Post-initialize the CachedVarOperation."""
  1556. object.__delattr__(self, "_js_expr")
  1557. def __getattr__(self, name: str) -> Any:
  1558. """Get an attribute of the var.
  1559. Args:
  1560. name: The name of the attribute.
  1561. Returns:
  1562. The attribute.
  1563. """
  1564. if name == "_js_expr":
  1565. return self._cached_var_name
  1566. parent_classes = inspect.getmro(type(self))
  1567. next_class = parent_classes[parent_classes.index(CachedVarOperation) + 1]
  1568. return next_class.__getattr__(self, name) # type: ignore
  1569. def _get_all_var_data(self) -> VarData | None:
  1570. """Get all VarData associated with the Var.
  1571. Returns:
  1572. The VarData of the components and all of its children.
  1573. """
  1574. return self._cached_get_all_var_data
  1575. @cached_property_no_lock
  1576. def _cached_get_all_var_data(self) -> VarData | None:
  1577. """Get the cached VarData.
  1578. Returns:
  1579. The cached VarData.
  1580. """
  1581. return VarData.merge(
  1582. *(
  1583. value._get_all_var_data() if isinstance(value, Var) else None
  1584. for value in (
  1585. getattr(self, field.name)
  1586. for field in dataclasses.fields(self) # type: ignore
  1587. )
  1588. ),
  1589. self._var_data,
  1590. )
  1591. def __hash__(self) -> int:
  1592. """Calculate the hash of the object.
  1593. Returns:
  1594. The hash of the object.
  1595. """
  1596. return hash(
  1597. (
  1598. type(self).__name__,
  1599. *[
  1600. getattr(self, field.name)
  1601. for field in dataclasses.fields(self) # type: ignore
  1602. if field.name not in ["_js_expr", "_var_data", "_var_type"]
  1603. ],
  1604. )
  1605. )
  1606. @dataclasses.dataclass(
  1607. eq=False,
  1608. frozen=True,
  1609. **{"slots": True} if sys.version_info >= (3, 10) else {},
  1610. )
  1611. class CallableVar(Var):
  1612. """Decorate a Var-returning function to act as both a Var and a function.
  1613. This is used as a compatibility shim for replacing Var objects in the
  1614. API with functions that return a family of Var.
  1615. """
  1616. fn: Callable[..., Var] = dataclasses.field(
  1617. default_factory=lambda: lambda: Var(_js_expr="undefined")
  1618. )
  1619. original_var: Var = dataclasses.field(
  1620. default_factory=lambda: Var(_js_expr="undefined")
  1621. )
  1622. def __init__(self, fn: Callable[..., Var]):
  1623. """Initialize a CallableVar.
  1624. Args:
  1625. fn: The function to decorate (must return Var)
  1626. """
  1627. original_var = fn()
  1628. super(CallableVar, self).__init__(
  1629. _js_expr=original_var._js_expr,
  1630. _var_type=original_var._var_type,
  1631. _var_data=VarData.merge(original_var._get_all_var_data()),
  1632. )
  1633. object.__setattr__(self, "fn", fn)
  1634. object.__setattr__(self, "original_var", original_var)
  1635. def __call__(self, *args, **kwargs) -> Var:
  1636. """Call the decorated function.
  1637. Args:
  1638. *args: The args to pass to the function.
  1639. **kwargs: The kwargs to pass to the function.
  1640. Returns:
  1641. The Var returned from calling the function.
  1642. """
  1643. return self.fn(*args, **kwargs)
  1644. def __hash__(self) -> int:
  1645. """Calculate the hash of the object.
  1646. Returns:
  1647. The hash of the object.
  1648. """
  1649. return hash((type(self).__name__, self.original_var))
  1650. RETURN_TYPE = TypeVar("RETURN_TYPE")
  1651. DICT_KEY = TypeVar("DICT_KEY")
  1652. DICT_VAL = TypeVar("DICT_VAL")
  1653. LIST_INSIDE = TypeVar("LIST_INSIDE")
  1654. class FakeComputedVarBaseClass(property):
  1655. """A fake base class for ComputedVar to avoid inheriting from property."""
  1656. __pydantic_run_validation__ = False
  1657. def is_computed_var(obj: Any) -> TypeGuard[ComputedVar]:
  1658. """Check if the object is a ComputedVar.
  1659. Args:
  1660. obj: The object to check.
  1661. Returns:
  1662. Whether the object is a ComputedVar.
  1663. """
  1664. return isinstance(obj, FakeComputedVarBaseClass)
  1665. @dataclasses.dataclass(
  1666. eq=False,
  1667. frozen=True,
  1668. **{"slots": True} if sys.version_info >= (3, 10) else {},
  1669. )
  1670. class ComputedVar(Var[RETURN_TYPE]):
  1671. """A field with computed getters."""
  1672. # Whether to track dependencies and cache computed values
  1673. _cache: bool = dataclasses.field(default=False)
  1674. # Whether the computed var is a backend var
  1675. _backend: bool = dataclasses.field(default=False)
  1676. # The initial value of the computed var
  1677. _initial_value: RETURN_TYPE | types.Unset = dataclasses.field(default=types.Unset())
  1678. # Explicit var dependencies to track
  1679. _static_deps: set[str] = dataclasses.field(default_factory=set)
  1680. # Whether var dependencies should be auto-determined
  1681. _auto_deps: bool = dataclasses.field(default=True)
  1682. # Interval at which the computed var should be updated
  1683. _update_interval: Optional[datetime.timedelta] = dataclasses.field(default=None)
  1684. _fget: Callable[[BaseState], RETURN_TYPE] = dataclasses.field(
  1685. default_factory=lambda: lambda _: None
  1686. ) # type: ignore
  1687. def __init__(
  1688. self,
  1689. fget: Callable[[BASE_STATE], RETURN_TYPE],
  1690. initial_value: RETURN_TYPE | types.Unset = types.Unset(),
  1691. cache: bool = True,
  1692. deps: Optional[List[Union[str, Var]]] = None,
  1693. auto_deps: bool = True,
  1694. interval: Optional[Union[int, datetime.timedelta]] = None,
  1695. backend: bool | None = None,
  1696. **kwargs,
  1697. ):
  1698. """Initialize a ComputedVar.
  1699. Args:
  1700. fget: The getter function.
  1701. initial_value: The initial value of the computed var.
  1702. cache: Whether to cache the computed value.
  1703. deps: Explicit var dependencies to track.
  1704. auto_deps: Whether var dependencies should be auto-determined.
  1705. interval: Interval at which the computed var should be updated.
  1706. backend: Whether the computed var is a backend var.
  1707. **kwargs: additional attributes to set on the instance
  1708. Raises:
  1709. TypeError: If the computed var dependencies are not Var instances or var names.
  1710. """
  1711. hint = kwargs.pop("return_type", None) or get_type_hints(fget).get(
  1712. "return", Any
  1713. )
  1714. if hint is Any:
  1715. console.deprecate(
  1716. "untyped-computed-var",
  1717. "ComputedVar should have a return type annotation.",
  1718. "0.6.5",
  1719. "0.7.0",
  1720. )
  1721. kwargs.setdefault("_js_expr", fget.__name__)
  1722. kwargs.setdefault("_var_type", hint)
  1723. Var.__init__(
  1724. self,
  1725. _js_expr=kwargs.pop("_js_expr"),
  1726. _var_type=kwargs.pop("_var_type"),
  1727. _var_data=kwargs.pop("_var_data", None),
  1728. )
  1729. if kwargs:
  1730. raise TypeError(f"Unexpected keyword arguments: {tuple(kwargs)}")
  1731. if backend is None:
  1732. backend = fget.__name__.startswith("_")
  1733. object.__setattr__(self, "_backend", backend)
  1734. object.__setattr__(self, "_initial_value", initial_value)
  1735. object.__setattr__(self, "_cache", cache)
  1736. if isinstance(interval, int):
  1737. interval = datetime.timedelta(seconds=interval)
  1738. object.__setattr__(self, "_update_interval", interval)
  1739. if deps is None:
  1740. deps = []
  1741. else:
  1742. for dep in deps:
  1743. if isinstance(dep, Var):
  1744. continue
  1745. if isinstance(dep, str) and dep != "":
  1746. continue
  1747. raise TypeError(
  1748. "ComputedVar dependencies must be Var instances or var names (non-empty strings)."
  1749. )
  1750. object.__setattr__(
  1751. self,
  1752. "_static_deps",
  1753. {dep._js_expr if isinstance(dep, Var) else dep for dep in deps},
  1754. )
  1755. object.__setattr__(self, "_auto_deps", auto_deps)
  1756. object.__setattr__(self, "_fget", fget)
  1757. @override
  1758. def _replace(self, merge_var_data=None, **kwargs: Any) -> Self:
  1759. """Replace the attributes of the ComputedVar.
  1760. Args:
  1761. merge_var_data: VarData to merge into the existing VarData.
  1762. **kwargs: Var fields to update.
  1763. Returns:
  1764. The new ComputedVar instance.
  1765. Raises:
  1766. TypeError: If kwargs contains keys that are not allowed.
  1767. """
  1768. field_values = {
  1769. "fget": kwargs.pop("fget", self._fget),
  1770. "initial_value": kwargs.pop("initial_value", self._initial_value),
  1771. "cache": kwargs.pop("cache", self._cache),
  1772. "deps": kwargs.pop("deps", self._static_deps),
  1773. "auto_deps": kwargs.pop("auto_deps", self._auto_deps),
  1774. "interval": kwargs.pop("interval", self._update_interval),
  1775. "backend": kwargs.pop("backend", self._backend),
  1776. "_js_expr": kwargs.pop("_js_expr", self._js_expr),
  1777. "_var_type": kwargs.pop("_var_type", self._var_type),
  1778. "_var_data": kwargs.pop(
  1779. "_var_data", VarData.merge(self._var_data, merge_var_data)
  1780. ),
  1781. }
  1782. if kwargs:
  1783. unexpected_kwargs = ", ".join(kwargs.keys())
  1784. raise TypeError(f"Unexpected keyword arguments: {unexpected_kwargs}")
  1785. return type(self)(**field_values)
  1786. @property
  1787. def _cache_attr(self) -> str:
  1788. """Get the attribute used to cache the value on the instance.
  1789. Returns:
  1790. An attribute name.
  1791. """
  1792. return f"__cached_{self._js_expr}"
  1793. @property
  1794. def _last_updated_attr(self) -> str:
  1795. """Get the attribute used to store the last updated timestamp.
  1796. Returns:
  1797. An attribute name.
  1798. """
  1799. return f"__last_updated_{self._js_expr}"
  1800. def needs_update(self, instance: BaseState) -> bool:
  1801. """Check if the computed var needs to be updated.
  1802. Args:
  1803. instance: The state instance that the computed var is attached to.
  1804. Returns:
  1805. True if the computed var needs to be updated, False otherwise.
  1806. """
  1807. if self._update_interval is None:
  1808. return False
  1809. last_updated = getattr(instance, self._last_updated_attr, None)
  1810. if last_updated is None:
  1811. return True
  1812. return datetime.datetime.now() - last_updated > self._update_interval
  1813. @overload
  1814. def __get__(
  1815. self: ComputedVar[int] | ComputedVar[float],
  1816. instance: None,
  1817. owner: Type,
  1818. ) -> NumberVar: ...
  1819. @overload
  1820. def __get__(
  1821. self: ComputedVar[str],
  1822. instance: None,
  1823. owner: Type,
  1824. ) -> StringVar: ...
  1825. @overload
  1826. def __get__(
  1827. self: ComputedVar[dict[DICT_KEY, DICT_VAL]],
  1828. instance: None,
  1829. owner: Type,
  1830. ) -> ObjectVar[dict[DICT_KEY, DICT_VAL]]: ...
  1831. @overload
  1832. def __get__(
  1833. self: ComputedVar[Sequence[LIST_INSIDE]],
  1834. instance: None,
  1835. owner: Type,
  1836. ) -> ArrayVar[Sequence[LIST_INSIDE]]: ...
  1837. @overload
  1838. def __get__(
  1839. self: ComputedVar[set[LIST_INSIDE]],
  1840. instance: None,
  1841. owner: Type,
  1842. ) -> ArrayVar[set[LIST_INSIDE]]: ...
  1843. @overload
  1844. def __get__(self, instance: None, owner: Type) -> ComputedVar[RETURN_TYPE]: ...
  1845. @overload
  1846. def __get__(self, instance: BaseState, owner: Type) -> RETURN_TYPE: ...
  1847. def __get__(self, instance: BaseState | None, owner):
  1848. """Get the ComputedVar value.
  1849. If the value is already cached on the instance, return the cached value.
  1850. Args:
  1851. instance: the instance of the class accessing this computed var.
  1852. owner: the class that this descriptor is attached to.
  1853. Returns:
  1854. The value of the var for the given instance.
  1855. """
  1856. if instance is None:
  1857. state_where_defined = owner
  1858. while self._js_expr in state_where_defined.inherited_vars:
  1859. state_where_defined = state_where_defined.get_parent_state()
  1860. field_name = (
  1861. format_state_name(state_where_defined.get_full_name())
  1862. + "."
  1863. + self._js_expr
  1864. )
  1865. return dispatch(
  1866. field_name,
  1867. var_data=VarData.from_state(state_where_defined, self._js_expr),
  1868. result_var_type=self._var_type,
  1869. existing_var=self,
  1870. )
  1871. if not self._cache:
  1872. value = self.fget(instance)
  1873. else:
  1874. # handle caching
  1875. if not hasattr(instance, self._cache_attr) or self.needs_update(instance):
  1876. # Set cache attr on state instance.
  1877. setattr(instance, self._cache_attr, self.fget(instance))
  1878. # Ensure the computed var gets serialized to redis.
  1879. instance._was_touched = True
  1880. # Set the last updated timestamp on the state instance.
  1881. setattr(instance, self._last_updated_attr, datetime.datetime.now())
  1882. value = getattr(instance, self._cache_attr)
  1883. if not _isinstance(value, self._var_type):
  1884. console.deprecate(
  1885. "mismatched-computed-var-return",
  1886. f"Computed var {type(instance).__name__}.{self._js_expr} returned value of type {type(value)}, "
  1887. f"expected {self._var_type}. This might cause unexpected behavior.",
  1888. "0.6.5",
  1889. "0.7.0",
  1890. )
  1891. return value
  1892. def _deps(
  1893. self,
  1894. objclass: Type,
  1895. obj: FunctionType | CodeType | None = None,
  1896. self_name: Optional[str] = None,
  1897. ) -> set[str]:
  1898. """Determine var dependencies of this ComputedVar.
  1899. Save references to attributes accessed on "self". Recursively called
  1900. when the function makes a method call on "self" or define comprehensions
  1901. or nested functions that may reference "self".
  1902. Args:
  1903. objclass: the class obj this ComputedVar is attached to.
  1904. obj: the object to disassemble (defaults to the fget function).
  1905. self_name: if specified, look for this name in LOAD_FAST and LOAD_DEREF instructions.
  1906. Returns:
  1907. A set of variable names accessed by the given obj.
  1908. Raises:
  1909. VarValueError: if the function references the get_state, parent_state, or substates attributes
  1910. (cannot track deps in a related state, only implicitly via parent state).
  1911. """
  1912. if not self._auto_deps:
  1913. return self._static_deps
  1914. d = self._static_deps.copy()
  1915. if obj is None:
  1916. fget = self._fget
  1917. if fget is not None:
  1918. obj = cast(FunctionType, fget)
  1919. else:
  1920. return set()
  1921. with contextlib.suppress(AttributeError):
  1922. # unbox functools.partial
  1923. obj = cast(FunctionType, obj.func) # type: ignore
  1924. with contextlib.suppress(AttributeError):
  1925. # unbox EventHandler
  1926. obj = cast(FunctionType, obj.fn) # type: ignore
  1927. if self_name is None and isinstance(obj, FunctionType):
  1928. try:
  1929. # the first argument to the function is the name of "self" arg
  1930. self_name = obj.__code__.co_varnames[0]
  1931. except (AttributeError, IndexError):
  1932. self_name = None
  1933. if self_name is None:
  1934. # cannot reference attributes on self if method takes no args
  1935. return set()
  1936. invalid_names = ["get_state", "parent_state", "substates", "get_substate"]
  1937. self_is_top_of_stack = False
  1938. for instruction in dis.get_instructions(obj):
  1939. if (
  1940. instruction.opname in ("LOAD_FAST", "LOAD_DEREF")
  1941. and instruction.argval == self_name
  1942. ):
  1943. # bytecode loaded the class instance to the top of stack, next load instruction
  1944. # is referencing an attribute on self
  1945. self_is_top_of_stack = True
  1946. continue
  1947. if self_is_top_of_stack and instruction.opname in (
  1948. "LOAD_ATTR",
  1949. "LOAD_METHOD",
  1950. ):
  1951. try:
  1952. ref_obj = getattr(objclass, instruction.argval)
  1953. except Exception:
  1954. ref_obj = None
  1955. if instruction.argval in invalid_names:
  1956. raise VarValueError(
  1957. f"Cached var {self!s} cannot access arbitrary state via `{instruction.argval}`."
  1958. )
  1959. if callable(ref_obj):
  1960. # recurse into callable attributes
  1961. d.update(
  1962. self._deps(
  1963. objclass=objclass,
  1964. obj=ref_obj, # pyright: ignore [reportArgumentType]
  1965. )
  1966. )
  1967. # recurse into property fget functions
  1968. elif isinstance(ref_obj, property) and not isinstance(
  1969. ref_obj, ComputedVar
  1970. ):
  1971. d.update(
  1972. self._deps(
  1973. objclass=objclass,
  1974. obj=ref_obj.fget, # type: ignore
  1975. )
  1976. )
  1977. elif (
  1978. instruction.argval in objclass.backend_vars
  1979. or instruction.argval in objclass.vars
  1980. ):
  1981. # var access
  1982. d.add(instruction.argval)
  1983. elif instruction.opname == "LOAD_CONST" and isinstance(
  1984. instruction.argval, CodeType
  1985. ):
  1986. # recurse into nested functions / comprehensions, which can reference
  1987. # instance attributes from the outer scope
  1988. d.update(
  1989. self._deps(
  1990. objclass=objclass,
  1991. obj=instruction.argval,
  1992. self_name=self_name,
  1993. )
  1994. )
  1995. self_is_top_of_stack = False
  1996. return d
  1997. def mark_dirty(self, instance) -> None:
  1998. """Mark this ComputedVar as dirty.
  1999. Args:
  2000. instance: the state instance that needs to recompute the value.
  2001. """
  2002. with contextlib.suppress(AttributeError):
  2003. delattr(instance, self._cache_attr)
  2004. def _determine_var_type(self) -> GenericType:
  2005. """Get the type of the var.
  2006. Returns:
  2007. The type of the var.
  2008. """
  2009. hints = get_type_hints(self._fget)
  2010. if "return" in hints:
  2011. return hints["return"]
  2012. return Any
  2013. @property
  2014. def __class__(self) -> Type:
  2015. """Get the class of the var.
  2016. Returns:
  2017. The class of the var.
  2018. """
  2019. return FakeComputedVarBaseClass
  2020. @property
  2021. def fget(self) -> Callable[[BaseState], RETURN_TYPE]:
  2022. """Get the getter function.
  2023. Returns:
  2024. The getter function.
  2025. """
  2026. return self._fget
  2027. class DynamicRouteVar(ComputedVar[Union[str, List[str]]]):
  2028. """A ComputedVar that represents a dynamic route."""
  2029. pass
  2030. if TYPE_CHECKING:
  2031. BASE_STATE = TypeVar("BASE_STATE", bound=BaseState)
  2032. @overload
  2033. def computed_var(
  2034. fget: None = None,
  2035. initial_value: Any | types.Unset = types.Unset(),
  2036. cache: bool = True,
  2037. deps: Optional[List[Union[str, Var]]] = None,
  2038. auto_deps: bool = True,
  2039. interval: Optional[Union[datetime.timedelta, int]] = None,
  2040. backend: bool | None = None,
  2041. **kwargs,
  2042. ) -> Callable[[Callable[[BASE_STATE], RETURN_TYPE]], ComputedVar[RETURN_TYPE]]: ...
  2043. @overload
  2044. def computed_var(
  2045. fget: Callable[[BASE_STATE], RETURN_TYPE],
  2046. initial_value: RETURN_TYPE | types.Unset = types.Unset(),
  2047. cache: bool = True,
  2048. deps: Optional[List[Union[str, Var]]] = None,
  2049. auto_deps: bool = True,
  2050. interval: Optional[Union[datetime.timedelta, int]] = None,
  2051. backend: bool | None = None,
  2052. **kwargs,
  2053. ) -> ComputedVar[RETURN_TYPE]: ...
  2054. def computed_var(
  2055. fget: Callable[[BASE_STATE], Any] | None = None,
  2056. initial_value: Any | types.Unset = types.Unset(),
  2057. cache: bool = True,
  2058. deps: Optional[List[Union[str, Var]]] = None,
  2059. auto_deps: bool = True,
  2060. interval: Optional[Union[datetime.timedelta, int]] = None,
  2061. backend: bool | None = None,
  2062. **kwargs,
  2063. ) -> ComputedVar | Callable[[Callable[[BASE_STATE], Any]], ComputedVar]:
  2064. """A ComputedVar decorator with or without kwargs.
  2065. Args:
  2066. fget: The getter function.
  2067. initial_value: The initial value of the computed var.
  2068. cache: Whether to cache the computed value.
  2069. deps: Explicit var dependencies to track.
  2070. auto_deps: Whether var dependencies should be auto-determined.
  2071. interval: Interval at which the computed var should be updated.
  2072. backend: Whether the computed var is a backend var.
  2073. **kwargs: additional attributes to set on the instance
  2074. Returns:
  2075. A ComputedVar instance.
  2076. Raises:
  2077. ValueError: If caching is disabled and an update interval is set.
  2078. VarDependencyError: If user supplies dependencies without caching.
  2079. """
  2080. if cache is False and interval is not None:
  2081. raise ValueError("Cannot set update interval without caching.")
  2082. if cache is False and (deps is not None or auto_deps is False):
  2083. raise VarDependencyError("Cannot track dependencies without caching.")
  2084. if fget is not None:
  2085. return ComputedVar(fget, cache=cache)
  2086. def wrapper(fget: Callable[[BASE_STATE], Any]) -> ComputedVar:
  2087. return ComputedVar(
  2088. fget,
  2089. initial_value=initial_value,
  2090. cache=cache,
  2091. deps=deps,
  2092. auto_deps=auto_deps,
  2093. interval=interval,
  2094. backend=backend,
  2095. **kwargs,
  2096. )
  2097. return wrapper
  2098. RETURN = TypeVar("RETURN")
  2099. @dataclasses.dataclass(
  2100. eq=False,
  2101. frozen=True,
  2102. **{"slots": True} if sys.version_info >= (3, 10) else {},
  2103. )
  2104. class CustomVarOperationReturn(Var[RETURN]):
  2105. """Base class for custom var operations."""
  2106. _type_computer: TypeComputer | None = dataclasses.field(default=None)
  2107. _raw_js_function: str | None = dataclasses.field(default=None)
  2108. @classmethod
  2109. def create(
  2110. cls,
  2111. js_expression: str,
  2112. _var_type: Type[RETURN] | None = None,
  2113. _type_computer: TypeComputer | None = None,
  2114. _var_data: VarData | None = None,
  2115. _raw_js_function: str | None = None,
  2116. ) -> CustomVarOperationReturn[RETURN]:
  2117. """Create a CustomVarOperation.
  2118. Args:
  2119. js_expression: The JavaScript expression to evaluate.
  2120. _var_type: The type of the var.
  2121. _type_computer: A function to compute the type of the var given the arguments.
  2122. _var_data: Additional hooks and imports associated with the Var.
  2123. _raw_js_function: If provided, it will be used when the operation is being called with all of its arguments at once.
  2124. Returns:
  2125. The CustomVarOperation.
  2126. """
  2127. return CustomVarOperationReturn(
  2128. _js_expr=js_expression,
  2129. _var_type=_var_type or Any,
  2130. _type_computer=_type_computer,
  2131. _var_data=_var_data,
  2132. _raw_js_function=_raw_js_function,
  2133. )
  2134. def var_operation_return(
  2135. js_expression: str,
  2136. var_type: Type[RETURN] | None = None,
  2137. type_computer: Optional[TypeComputer] = None,
  2138. var_data: VarData | None = None,
  2139. _raw_js_function: str | None = None,
  2140. ) -> CustomVarOperationReturn[RETURN]:
  2141. """Shortcut for creating a CustomVarOperationReturn.
  2142. Args:
  2143. js_expression: The JavaScript expression to evaluate.
  2144. var_type: The type of the var.
  2145. type_computer: A function to compute the type of the var given the arguments.
  2146. var_data: Additional hooks and imports associated with the Var.
  2147. _raw_js_function: If provided, it will be used when the operation is being called with all of its arguments at once.
  2148. Returns:
  2149. The CustomVarOperationReturn.
  2150. """
  2151. return CustomVarOperationReturn.create(
  2152. js_expression=js_expression,
  2153. _var_type=var_type,
  2154. _type_computer=type_computer,
  2155. _var_data=var_data,
  2156. _raw_js_function=_raw_js_function,
  2157. )
  2158. @dataclasses.dataclass(
  2159. eq=False,
  2160. frozen=True,
  2161. **{"slots": True} if sys.version_info >= (3, 10) else {},
  2162. )
  2163. class CustomVarOperation(CachedVarOperation, Var[T]):
  2164. """Base class for custom var operations."""
  2165. _name: str = dataclasses.field(default="")
  2166. _args: Tuple[Tuple[str, Var], ...] = dataclasses.field(default_factory=tuple)
  2167. _return: CustomVarOperationReturn[T] = dataclasses.field(
  2168. default_factory=lambda: CustomVarOperationReturn.create("")
  2169. )
  2170. @cached_property_no_lock
  2171. def _cached_var_name(self) -> str:
  2172. """Get the cached var name.
  2173. Returns:
  2174. The cached var name.
  2175. """
  2176. return str(self._return)
  2177. @cached_property_no_lock
  2178. def _cached_get_all_var_data(self) -> VarData | None:
  2179. """Get the cached VarData.
  2180. Returns:
  2181. The cached VarData.
  2182. """
  2183. return VarData.merge(
  2184. *(arg[1]._get_all_var_data() for arg in self._args),
  2185. self._return._get_all_var_data(),
  2186. self._var_data,
  2187. )
  2188. @classmethod
  2189. def create(
  2190. cls,
  2191. name: str,
  2192. args: Tuple[Tuple[str, Var], ...],
  2193. return_var: CustomVarOperationReturn[T],
  2194. _var_data: VarData | None = None,
  2195. ) -> CustomVarOperation[T]:
  2196. """Create a CustomVarOperation.
  2197. Args:
  2198. name: The name of the operation.
  2199. args: The arguments to the operation.
  2200. return_var: The return var.
  2201. _var_data: Additional hooks and imports associated with the Var.
  2202. Returns:
  2203. The CustomVarOperation.
  2204. """
  2205. return CustomVarOperation(
  2206. _js_expr="",
  2207. _var_type=return_var._var_type,
  2208. _var_data=_var_data,
  2209. _name=name,
  2210. _args=args,
  2211. _return=return_var,
  2212. )
  2213. class NoneVar(Var[None], python_types=type(None)):
  2214. """A var representing None."""
  2215. @dataclasses.dataclass(
  2216. eq=False,
  2217. frozen=True,
  2218. **{"slots": True} if sys.version_info >= (3, 10) else {},
  2219. )
  2220. class LiteralNoneVar(LiteralVar, NoneVar):
  2221. """A var representing None."""
  2222. _var_value: None = None
  2223. def json(self) -> str:
  2224. """Serialize the var to a JSON string.
  2225. Returns:
  2226. The JSON string.
  2227. """
  2228. return "null"
  2229. @classmethod
  2230. def create(
  2231. cls,
  2232. value: None = None,
  2233. _var_data: VarData | None = None,
  2234. ) -> LiteralNoneVar:
  2235. """Create a var from a value.
  2236. Args:
  2237. value: The value of the var. Must be None. Existed for compatibility with LiteralVar.
  2238. _var_data: Additional hooks and imports associated with the Var.
  2239. Returns:
  2240. The var.
  2241. """
  2242. return LiteralNoneVar(
  2243. _js_expr="null",
  2244. _var_type=None,
  2245. _var_data=_var_data,
  2246. )
  2247. def get_to_operation(var_subclass: Type[Var]) -> Type[ToOperation]:
  2248. """Get the ToOperation class for a given Var subclass.
  2249. Args:
  2250. var_subclass: The Var subclass.
  2251. Returns:
  2252. The ToOperation class.
  2253. Raises:
  2254. ValueError: If the ToOperation class cannot be found.
  2255. """
  2256. possible_classes = [
  2257. saved_var_subclass.to_var_subclass
  2258. for saved_var_subclass in _var_subclasses
  2259. if saved_var_subclass.var_subclass is var_subclass
  2260. ]
  2261. if not possible_classes:
  2262. raise ValueError(f"Could not find ToOperation for {var_subclass}.")
  2263. return possible_classes[0]
  2264. @dataclasses.dataclass(
  2265. eq=False,
  2266. frozen=True,
  2267. **{"slots": True} if sys.version_info >= (3, 10) else {},
  2268. )
  2269. class StateOperation(CachedVarOperation, Var):
  2270. """A var operation that accesses a field on an object."""
  2271. _state_name: str = dataclasses.field(default="")
  2272. _field: Var = dataclasses.field(default_factory=lambda: LiteralNoneVar.create())
  2273. @cached_property_no_lock
  2274. def _cached_var_name(self) -> str:
  2275. """Get the cached var name.
  2276. Returns:
  2277. The cached var name.
  2278. """
  2279. return f"{self._state_name!s}.{self._field!s}"
  2280. def __getattr__(self, name: str) -> Any:
  2281. """Get an attribute of the var.
  2282. Args:
  2283. name: The name of the attribute.
  2284. Returns:
  2285. The attribute.
  2286. """
  2287. if name == "_js_expr":
  2288. return self._cached_var_name
  2289. return getattr(self._field, name)
  2290. @classmethod
  2291. def create(
  2292. cls,
  2293. state_name: str,
  2294. field: Var,
  2295. _var_data: VarData | None = None,
  2296. ) -> StateOperation:
  2297. """Create a DotOperation.
  2298. Args:
  2299. state_name: The name of the state.
  2300. field: The field of the state.
  2301. _var_data: Additional hooks and imports associated with the Var.
  2302. Returns:
  2303. The DotOperation.
  2304. """
  2305. return StateOperation(
  2306. _js_expr="",
  2307. _var_type=field._var_type,
  2308. _var_data=_var_data,
  2309. _state_name=state_name,
  2310. _field=field,
  2311. )
  2312. def get_uuid_string_var() -> Var:
  2313. """Return a Var that generates a single memoized UUID via .web/utils/state.js.
  2314. useMemo with an empty dependency array ensures that the generated UUID is
  2315. consistent across re-renders of the component.
  2316. Returns:
  2317. A Var that generates a UUID at runtime.
  2318. """
  2319. from reflex.utils.imports import ImportVar
  2320. from reflex.vars import Var
  2321. unique_uuid_var = get_unique_variable_name()
  2322. unique_uuid_var_data = VarData(
  2323. imports={
  2324. f"$/{constants.Dirs.STATE_PATH}": {ImportVar(tag="generateUUID")}, # type: ignore
  2325. "react": "useMemo",
  2326. },
  2327. hooks={f"const {unique_uuid_var} = useMemo(generateUUID, [])": None},
  2328. )
  2329. return Var(
  2330. _js_expr=unique_uuid_var,
  2331. _var_type=str,
  2332. _var_data=unique_uuid_var_data,
  2333. )
  2334. # Set of unique variable names.
  2335. USED_VARIABLES = set()
  2336. def get_unique_variable_name() -> str:
  2337. """Get a unique variable name.
  2338. Returns:
  2339. The unique variable name.
  2340. """
  2341. name = "".join([random.choice(string.ascii_lowercase) for _ in range(8)])
  2342. if name not in USED_VARIABLES:
  2343. USED_VARIABLES.add(name)
  2344. return name
  2345. return get_unique_variable_name()
  2346. # Compile regex for finding reflex var tags.
  2347. _decode_var_pattern_re = (
  2348. rf"{constants.REFLEX_VAR_OPENING_TAG}(.*?){constants.REFLEX_VAR_CLOSING_TAG}"
  2349. )
  2350. _decode_var_pattern = re.compile(_decode_var_pattern_re, flags=re.DOTALL)
  2351. # Defined global immutable vars.
  2352. _global_vars: Dict[int, Var] = {}
  2353. def _extract_var_data(value: Iterable) -> list[VarData | None]:
  2354. """Extract the var imports and hooks from an iterable containing a Var.
  2355. Args:
  2356. value: The iterable to extract the VarData from
  2357. Returns:
  2358. The extracted VarDatas.
  2359. """
  2360. from reflex.style import Style
  2361. from reflex.vars import Var
  2362. var_datas = []
  2363. with contextlib.suppress(TypeError):
  2364. for sub in value:
  2365. if isinstance(sub, Var):
  2366. var_datas.append(sub._var_data)
  2367. elif not isinstance(sub, str):
  2368. # Recurse into dict values.
  2369. if (
  2370. (values_fn := getattr(sub, "values", None)) is not None
  2371. and callable(values_fn)
  2372. and isinstance((values := values_fn()), Iterable)
  2373. ):
  2374. var_datas.extend(_extract_var_data(values))
  2375. # Recurse into iterable values (or dict keys).
  2376. var_datas.extend(_extract_var_data(sub))
  2377. # Style objects should already have _var_data.
  2378. if isinstance(value, Style):
  2379. var_datas.append(value._var_data)
  2380. else:
  2381. # Recurse when value is a dict itself.
  2382. values_fn = getattr(value, "values", None)
  2383. if callable(values_fn) and isinstance((values := values_fn()), Iterable):
  2384. var_datas.extend(_extract_var_data(values))
  2385. return var_datas
  2386. # These names were changed in reflex 0.3.0
  2387. REPLACED_NAMES = {
  2388. "full_name": "_var_full_name",
  2389. "name": "_js_expr",
  2390. "state": "_var_data.state",
  2391. "type_": "_var_type",
  2392. "is_local": "_var_is_local",
  2393. "is_string": "_var_is_string",
  2394. "set_state": "_var_set_state",
  2395. "deps": "_deps",
  2396. }
  2397. dispatchers: Dict[GenericType, Callable[[Var], Var]] = {}
  2398. def transform(fn: Callable[[Var], Var]) -> Callable[[Var], Var]:
  2399. """Register a function to transform a Var.
  2400. Args:
  2401. fn: The function to register.
  2402. Returns:
  2403. The decorator.
  2404. Raises:
  2405. TypeError: If the return type of the function is not a Var.
  2406. TypeError: If the Var return type does not have a generic type.
  2407. ValueError: If a function for the generic type is already registered.
  2408. """
  2409. return_type = fn.__annotations__["return"]
  2410. origin = get_origin(return_type)
  2411. if origin is not Var:
  2412. raise TypeError(
  2413. f"Expected return type of {fn.__name__} to be a Var, got {origin}."
  2414. )
  2415. generic_args = get_args(return_type)
  2416. if not generic_args:
  2417. raise TypeError(
  2418. f"Expected Var return type of {fn.__name__} to have a generic type."
  2419. )
  2420. generic_type = get_origin(generic_args[0]) or generic_args[0]
  2421. if generic_type in dispatchers:
  2422. raise ValueError(f"Function for {generic_type} already registered.")
  2423. dispatchers[generic_type] = fn
  2424. return fn
  2425. def generic_type_to_actual_type_map(
  2426. generic_type: GenericType, actual_type: GenericType
  2427. ) -> Dict[TypeVar, GenericType]:
  2428. """Map the generic type to the actual type.
  2429. Args:
  2430. generic_type: The generic type.
  2431. actual_type: The actual type.
  2432. Returns:
  2433. The mapping of type variables to actual types.
  2434. Raises:
  2435. TypeError: If the generic type and actual type do not match.
  2436. TypeError: If the number of generic arguments and actual arguments do not match.
  2437. """
  2438. generic_origin = get_origin(generic_type) or generic_type
  2439. actual_origin = get_origin(actual_type) or actual_type
  2440. if generic_origin is not actual_origin:
  2441. if isinstance(generic_origin, TypeVar):
  2442. return {generic_origin: actual_origin}
  2443. raise TypeError(
  2444. f"Type mismatch: expected {generic_origin}, got {actual_origin}."
  2445. )
  2446. generic_args = get_args(generic_type)
  2447. actual_args = get_args(actual_type)
  2448. if len(generic_args) != len(actual_args):
  2449. raise TypeError(
  2450. f"Number of generic arguments mismatch: expected {len(generic_args)}, got {len(actual_args)}."
  2451. )
  2452. # call recursively for nested generic types and merge the results
  2453. return {
  2454. k: v
  2455. for generic_arg, actual_arg in zip(generic_args, actual_args)
  2456. for k, v in generic_type_to_actual_type_map(generic_arg, actual_arg).items()
  2457. }
  2458. def resolve_generic_type_with_mapping(
  2459. generic_type: GenericType, type_mapping: Dict[TypeVar, GenericType]
  2460. ):
  2461. """Resolve a generic type with a type mapping.
  2462. Args:
  2463. generic_type: The generic type.
  2464. type_mapping: The type mapping.
  2465. Returns:
  2466. The resolved generic type.
  2467. """
  2468. if isinstance(generic_type, TypeVar):
  2469. return type_mapping.get(generic_type, generic_type)
  2470. generic_origin = get_origin(generic_type) or generic_type
  2471. generic_args = get_args(generic_type)
  2472. if not generic_args:
  2473. return generic_type
  2474. mapping_for_older_python = {
  2475. list: List,
  2476. set: Set,
  2477. dict: Dict,
  2478. tuple: Tuple,
  2479. frozenset: FrozenSet,
  2480. }
  2481. return mapping_for_older_python.get(generic_origin, generic_origin)[
  2482. tuple(
  2483. resolve_generic_type_with_mapping(arg, type_mapping) for arg in generic_args
  2484. )
  2485. ]
  2486. def resolve_arg_type_from_return_type(
  2487. arg_type: GenericType, return_type: GenericType, actual_return_type: GenericType
  2488. ) -> GenericType:
  2489. """Resolve the argument type from the return type.
  2490. Args:
  2491. arg_type: The argument type.
  2492. return_type: The return type.
  2493. actual_return_type: The requested return type.
  2494. Returns:
  2495. The argument type without the generics that are resolved.
  2496. """
  2497. return resolve_generic_type_with_mapping(
  2498. arg_type, generic_type_to_actual_type_map(return_type, actual_return_type)
  2499. )
  2500. def dispatch(
  2501. field_name: str,
  2502. var_data: VarData,
  2503. result_var_type: GenericType,
  2504. existing_var: Var | None = None,
  2505. ) -> Var:
  2506. """Dispatch a Var to the appropriate transformation function.
  2507. Args:
  2508. field_name: The name of the field.
  2509. var_data: The VarData associated with the Var.
  2510. result_var_type: The type of the Var.
  2511. existing_var: The existing Var to transform. Optional.
  2512. Returns:
  2513. The transformed Var.
  2514. Raises:
  2515. TypeError: If the return type of the function is not a Var.
  2516. TypeError: If the Var return type does not have a generic type.
  2517. TypeError: If the first argument of the function is not a Var.
  2518. TypeError: If the first argument of the function does not have a generic type
  2519. """
  2520. result_origin_var_type = get_origin(result_var_type) or result_var_type
  2521. if result_origin_var_type in dispatchers:
  2522. fn = dispatchers[result_origin_var_type]
  2523. fn_first_arg_type = next(
  2524. iter(inspect.signature(fn).parameters.values())
  2525. ).annotation
  2526. fn_return = inspect.signature(fn).return_annotation
  2527. fn_return_origin = get_origin(fn_return) or fn_return
  2528. if fn_return_origin is not Var:
  2529. raise TypeError(
  2530. f"Expected return type of {fn.__name__} to be a Var, got {fn_return}."
  2531. )
  2532. fn_return_generic_args = get_args(fn_return)
  2533. if not fn_return_generic_args:
  2534. raise TypeError(f"Expected generic type of {fn_return} to be a type.")
  2535. arg_origin = get_origin(fn_first_arg_type) or fn_first_arg_type
  2536. if arg_origin is not Var:
  2537. raise TypeError(
  2538. f"Expected first argument of {fn.__name__} to be a Var, got {fn_first_arg_type}."
  2539. )
  2540. arg_generic_args = get_args(fn_first_arg_type)
  2541. if not arg_generic_args:
  2542. raise TypeError(
  2543. f"Expected generic type of {fn_first_arg_type} to be a type."
  2544. )
  2545. arg_type = arg_generic_args[0]
  2546. fn_return_type = fn_return_generic_args[0]
  2547. var = (
  2548. Var(
  2549. field_name,
  2550. _var_data=var_data,
  2551. _var_type=resolve_arg_type_from_return_type(
  2552. arg_type, fn_return_type, result_var_type
  2553. ),
  2554. ).guess_type()
  2555. if existing_var is None
  2556. else existing_var._replace(
  2557. _var_type=resolve_arg_type_from_return_type(
  2558. arg_type, fn_return_type, result_var_type
  2559. ),
  2560. _var_data=var_data,
  2561. _js_expr=field_name,
  2562. ).guess_type()
  2563. )
  2564. return fn(var)
  2565. if existing_var is not None:
  2566. return existing_var._replace(
  2567. _js_expr=field_name,
  2568. _var_data=var_data,
  2569. _var_type=result_var_type,
  2570. ).guess_type()
  2571. return Var(
  2572. field_name,
  2573. _var_data=var_data,
  2574. _var_type=result_var_type,
  2575. ).guess_type()
  2576. V = TypeVar("V")
  2577. BASE_TYPE = TypeVar("BASE_TYPE", bound=Base)
  2578. class Field(Generic[T]):
  2579. """Shadow class for Var to allow for type hinting in the IDE."""
  2580. def __set__(self, instance, value: T):
  2581. """Set the Var.
  2582. Args:
  2583. instance: The instance of the class setting the Var.
  2584. value: The value to set the Var to.
  2585. """
  2586. @overload
  2587. def __get__(self: Field[bool], instance: None, owner) -> BooleanVar: ...
  2588. @overload
  2589. def __get__(self: Field[int], instance: None, owner) -> NumberVar[int]: ...
  2590. @overload
  2591. def __get__(self: Field[float], instance: None, owner) -> NumberVar[float]: ...
  2592. @overload
  2593. def __get__(self: Field[str], instance: None, owner) -> StringVar[str]: ...
  2594. @overload
  2595. def __get__(self: Field[None], instance: None, owner) -> NoneVar: ...
  2596. @overload
  2597. def __get__(
  2598. self: Field[Sequence[V]] | Field[Set[V]] | Field[List[V]],
  2599. instance: None,
  2600. owner,
  2601. ) -> ArrayVar[Sequence[V]]: ...
  2602. @overload
  2603. def __get__(
  2604. self: Field[Dict[str, V]], instance: None, owner
  2605. ) -> ObjectVar[Dict[str, V]]: ...
  2606. @overload
  2607. def __get__(
  2608. self: Field[BASE_TYPE], instance: None, owner
  2609. ) -> ObjectVar[BASE_TYPE]: ...
  2610. @overload
  2611. def __get__(self, instance: None, owner) -> Var[T]: ...
  2612. @overload
  2613. def __get__(self, instance, owner) -> T: ...
  2614. def __get__(self, instance, owner): # type: ignore
  2615. """Get the Var.
  2616. Args:
  2617. instance: The instance of the class accessing the Var.
  2618. owner: The class that the Var is attached to.
  2619. """
  2620. def field(value: T) -> Field[T]:
  2621. """Create a Field with a value.
  2622. Args:
  2623. value: The value of the Field.
  2624. Returns:
  2625. The Field.
  2626. """
  2627. return value # type: ignore
  2628. def and_operation(a: Var | Any, b: Var | Any) -> Var:
  2629. """Perform a logical AND operation on two variables.
  2630. Args:
  2631. a: The first variable.
  2632. b: The second variable.
  2633. Returns:
  2634. The result of the logical AND operation.
  2635. """
  2636. from .function import ArgsFunctionOperation
  2637. a = Var.create(a)
  2638. b = Var.create(b)
  2639. return _and_func_operation(
  2640. ArgsFunctionOperation.create((), a, _var_type=ReflexCallable[[], a._var_type]),
  2641. ArgsFunctionOperation.create((), b, _var_type=ReflexCallable[[], b._var_type]),
  2642. )
  2643. def or_operation(a: Var | Any, b: Var | Any) -> Var:
  2644. """Perform a logical OR operation on two variables.
  2645. Args:
  2646. a: The first variable.
  2647. b: The second variable.
  2648. Returns:
  2649. The result of the logical OR operation.
  2650. """
  2651. from .function import ArgsFunctionOperation
  2652. a = Var.create(a)
  2653. b = Var.create(b)
  2654. return _or_func_operation(
  2655. ArgsFunctionOperation.create((), a, _var_type=ReflexCallable[[], a._var_type]),
  2656. ArgsFunctionOperation.create((), b, _var_type=ReflexCallable[[], b._var_type]),
  2657. )
  2658. T_LOGICAL = TypeVar("T_LOGICAL")
  2659. U_LOGICAL = TypeVar("U_LOGICAL")
  2660. @var_operation
  2661. def _and_func_operation(
  2662. a: Var[ReflexCallable[[], T_LOGICAL]], b: Var[ReflexCallable[[], U_LOGICAL]]
  2663. ) -> CustomVarOperationReturn[ReflexCallable[[], Union[T_LOGICAL, U_LOGICAL]]]:
  2664. """Perform a logical AND operation on two variables.
  2665. Args:
  2666. a: The first variable.
  2667. b: The second variable.
  2668. Returns:
  2669. The result of the logical AND operation.
  2670. """
  2671. def type_computer(*args: Var):
  2672. if not args:
  2673. return (
  2674. ReflexCallable[[ReflexCallable[[], Any], ReflexCallable[[], Any]], Any],
  2675. type_computer,
  2676. )
  2677. if len(args) == 1:
  2678. return (
  2679. ReflexCallable[[ReflexCallable[[], Any]], Any],
  2680. functools.partial(type_computer, args[0]),
  2681. )
  2682. a_return_type = unwrap_reflex_callalbe(args[0]._var_type)[1]
  2683. b_return_type = unwrap_reflex_callalbe(args[1]._var_type)[1]
  2684. return (
  2685. ReflexCallable[[], unionize(a_return_type, b_return_type)],
  2686. None,
  2687. )
  2688. return var_operation_return(
  2689. js_expression=f"({a}() && {b}())",
  2690. type_computer=type_computer,
  2691. )
  2692. @var_operation
  2693. def _or_func_operation(
  2694. a: Var[ReflexCallable[[], T_LOGICAL]], b: Var[ReflexCallable[[], U_LOGICAL]]
  2695. ) -> CustomVarOperationReturn[ReflexCallable[[], Union[T_LOGICAL, U_LOGICAL]]]:
  2696. """Perform a logical OR operation on two variables.
  2697. Args:
  2698. a: The first variable.
  2699. b: The second variable.
  2700. Returns:
  2701. The result of the logical OR operation.
  2702. """
  2703. def type_computer(*args: Var):
  2704. if not args:
  2705. return (
  2706. ReflexCallable[[ReflexCallable[[], Any], ReflexCallable[[], Any]], Any],
  2707. type_computer,
  2708. )
  2709. if len(args) == 1:
  2710. return (
  2711. ReflexCallable[[ReflexCallable[[], Any]], Any],
  2712. functools.partial(type_computer, args[0]),
  2713. )
  2714. a_return_type = unwrap_reflex_callalbe(args[0]._var_type)[1]
  2715. b_return_type = unwrap_reflex_callalbe(args[1]._var_type)[1]
  2716. return (
  2717. ReflexCallable[[], unionize(a_return_type, b_return_type)],
  2718. None,
  2719. )
  2720. return var_operation_return(
  2721. js_expression=f"({a}() || {b}())",
  2722. type_computer=type_computer,
  2723. )
  2724. def passthrough_unary_type_computer(no_args: GenericType) -> TypeComputer:
  2725. """Create a type computer for unary operations.
  2726. Args:
  2727. no_args: The type to return when no arguments are provided.
  2728. Returns:
  2729. The type computer.
  2730. """
  2731. def type_computer(*args: Var):
  2732. if not args:
  2733. return (no_args, type_computer)
  2734. return (ReflexCallable[[], args[0]._var_type], None)
  2735. return type_computer
  2736. def unary_type_computer(
  2737. no_args: GenericType, computer: Callable[[Var], GenericType]
  2738. ) -> TypeComputer:
  2739. """Create a type computer for unary operations.
  2740. Args:
  2741. no_args: The type to return when no arguments are provided.
  2742. computer: The function to compute the type.
  2743. Returns:
  2744. The type computer.
  2745. """
  2746. def type_computer(*args: Var):
  2747. if not args:
  2748. return (no_args, type_computer)
  2749. return (ReflexCallable[[], computer(args[0])], None)
  2750. return type_computer
  2751. def nary_type_computer(
  2752. *types: GenericType, computer: Callable[..., GenericType]
  2753. ) -> TypeComputer:
  2754. """Create a type computer for n-ary operations.
  2755. Args:
  2756. types: The types to return when no arguments are provided.
  2757. computer: The function to compute the type.
  2758. Returns:
  2759. The type computer.
  2760. """
  2761. def type_computer(*args: Var):
  2762. if len(args) != len(types):
  2763. return (
  2764. types[len(args)],
  2765. functools.partial(type_computer, *args),
  2766. )
  2767. return (
  2768. ReflexCallable[[], computer(args)],
  2769. None,
  2770. )
  2771. return type_computer