base.py 100 KB

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