function.py 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  1. """Immutable function vars."""
  2. from __future__ import annotations
  3. import dataclasses
  4. import inspect
  5. import sys
  6. from typing import (
  7. TYPE_CHECKING,
  8. Any,
  9. Callable,
  10. Mapping,
  11. NoReturn,
  12. Optional,
  13. Sequence,
  14. Tuple,
  15. Type,
  16. Union,
  17. overload,
  18. )
  19. from typing_extensions import Concatenate, Generic, ParamSpec, TypeVar
  20. from reflex.utils import format
  21. from reflex.utils.exceptions import VarTypeError
  22. from reflex.utils.types import GenericType, Unset, get_origin
  23. from .base import (
  24. CachedVarOperation,
  25. LiteralVar,
  26. ReflexCallable,
  27. TypeComputer,
  28. Var,
  29. VarData,
  30. VarWithDefault,
  31. cached_property_no_lock,
  32. unwrap_reflex_callalbe,
  33. )
  34. if TYPE_CHECKING:
  35. from .number import BooleanVar, NumberVar
  36. from .object import ObjectVar
  37. from .sequence import ArrayVar, StringVar
  38. P = ParamSpec("P")
  39. R = TypeVar("R")
  40. R2 = TypeVar("R2")
  41. V1 = TypeVar("V1")
  42. V2 = TypeVar("V2")
  43. V3 = TypeVar("V3")
  44. V4 = TypeVar("V4")
  45. V5 = TypeVar("V5")
  46. V6 = TypeVar("V6")
  47. CALLABLE_TYPE = TypeVar("CALLABLE_TYPE", bound=ReflexCallable, infer_variance=True)
  48. OTHER_CALLABLE_TYPE = TypeVar(
  49. "OTHER_CALLABLE_TYPE", bound=ReflexCallable, infer_variance=True
  50. )
  51. MAPPING_TYPE = TypeVar("MAPPING_TYPE", bound=Mapping, covariant=True)
  52. SEQUENCE_TYPE = TypeVar("SEQUENCE_TYPE", bound=Sequence, covariant=True)
  53. def type_is_reflex_callable(type_: Any) -> bool:
  54. """Check if a type is a ReflexCallable.
  55. Args:
  56. type_: The type to check.
  57. Returns:
  58. True if the type is a ReflexCallable.
  59. """
  60. return type_ is ReflexCallable or get_origin(type_) is ReflexCallable
  61. K = TypeVar("K", covariant=True)
  62. V = TypeVar("V", covariant=True)
  63. T = TypeVar("T", covariant=True)
  64. class FunctionVar(
  65. Var[CALLABLE_TYPE],
  66. default_type=ReflexCallable[Any, Any],
  67. is_subclass=type_is_reflex_callable,
  68. ):
  69. """Base class for immutable function vars."""
  70. @overload
  71. def partial(self) -> FunctionVar[CALLABLE_TYPE]: ...
  72. @overload
  73. def partial(
  74. self: FunctionVar[ReflexCallable[Concatenate[VarWithDefault[V1], P], R]]
  75. | FunctionVar[ReflexCallable[Concatenate[V1, P], R]],
  76. arg1: Union[V1, Var[V1]],
  77. ) -> FunctionVar[ReflexCallable[P, R]]: ...
  78. @overload
  79. def partial(
  80. self: FunctionVar[
  81. ReflexCallable[Concatenate[VarWithDefault[V1], VarWithDefault[V2], P], R]
  82. ]
  83. | FunctionVar[ReflexCallable[Concatenate[V1, VarWithDefault[V2], P], R]]
  84. | FunctionVar[ReflexCallable[Concatenate[V1, V2, P], R]],
  85. arg1: Union[V1, Var[V1]],
  86. arg2: Union[V2, Var[V2]],
  87. ) -> FunctionVar[ReflexCallable[P, R]]: ...
  88. @overload
  89. def partial(
  90. self: FunctionVar[
  91. ReflexCallable[
  92. Concatenate[
  93. VarWithDefault[V1], VarWithDefault[V2], VarWithDefault[V3], P
  94. ],
  95. R,
  96. ]
  97. ]
  98. | FunctionVar[
  99. ReflexCallable[
  100. Concatenate[V1, VarWithDefault[V2], VarWithDefault[V3], P], R
  101. ]
  102. ]
  103. | FunctionVar[ReflexCallable[Concatenate[V1, V2, VarWithDefault[V3], P], R]]
  104. | FunctionVar[ReflexCallable[Concatenate[V1, V2, V3, P], R]],
  105. arg1: Union[V1, Var[V1]],
  106. arg2: Union[V2, Var[V2]],
  107. arg3: Union[V3, Var[V3]],
  108. ) -> FunctionVar[ReflexCallable[P, R]]: ...
  109. @overload
  110. def partial(
  111. self: FunctionVar[ReflexCallable[Concatenate[V1, V2, V3, V4, P], R]],
  112. arg1: Union[V1, Var[V1]],
  113. arg2: Union[V2, Var[V2]],
  114. arg3: Union[V3, Var[V3]],
  115. arg4: Union[V4, Var[V4]],
  116. ) -> FunctionVar[ReflexCallable[P, R]]: ...
  117. @overload
  118. def partial(
  119. self: FunctionVar[ReflexCallable[Concatenate[V1, V2, V3, V4, V5, P], R]],
  120. arg1: Union[V1, Var[V1]],
  121. arg2: Union[V2, Var[V2]],
  122. arg3: Union[V3, Var[V3]],
  123. arg4: Union[V4, Var[V4]],
  124. arg5: Union[V5, Var[V5]],
  125. ) -> FunctionVar[ReflexCallable[P, R]]: ...
  126. @overload
  127. def partial(
  128. self: FunctionVar[ReflexCallable[Concatenate[V1, V2, V3, V4, V5, V6, P], R]],
  129. arg1: Union[V1, Var[V1]],
  130. arg2: Union[V2, Var[V2]],
  131. arg3: Union[V3, Var[V3]],
  132. arg4: Union[V4, Var[V4]],
  133. arg5: Union[V5, Var[V5]],
  134. arg6: Union[V6, Var[V6]],
  135. ) -> FunctionVar[ReflexCallable[P, R]]: ...
  136. @overload
  137. def partial(
  138. self: FunctionVar[ReflexCallable[P, R]], *args: Var | Any
  139. ) -> FunctionVar[ReflexCallable[P, R]]: ...
  140. @overload
  141. def partial(self, *args: Var | Any) -> FunctionVar: ...
  142. def partial(self, *args: Var | Any) -> FunctionVar: # pyright: ignore [reportInconsistentOverload]
  143. """Partially apply the function with the given arguments.
  144. Args:
  145. *args: The arguments to partially apply the function with.
  146. Returns:
  147. The partially applied function.
  148. """
  149. if not args:
  150. return self
  151. args = tuple(map(LiteralVar.create, args))
  152. remaining_validators = self._pre_check(*args)
  153. partial_types, type_computer = self._partial_type(*args)
  154. if self.__call__ is self.partial:
  155. # if the default behavior is partial, we should return a new partial function
  156. return ArgsFunctionOperationBuilder.create(
  157. (),
  158. VarOperationCall.create(
  159. self,
  160. *args,
  161. Var(_js_expr="...args"),
  162. _var_type=self._return_type(*args),
  163. ),
  164. rest="args",
  165. validators=remaining_validators,
  166. type_computer=type_computer,
  167. _var_type=partial_types,
  168. )
  169. return ArgsFunctionOperation.create(
  170. (),
  171. VarOperationCall.create(
  172. self, *args, Var(_js_expr="...args"), _var_type=self._return_type(*args)
  173. ),
  174. rest="args",
  175. validators=remaining_validators,
  176. type_computer=type_computer,
  177. _var_type=partial_types,
  178. )
  179. # THIS CODE IS GENERATED BY `_generate_overloads_for_function_var_call` function below.
  180. @overload
  181. def call(
  182. self: FunctionVar[ReflexCallable[[], bool]],
  183. ) -> BooleanVar: ...
  184. @overload
  185. def call(
  186. self: FunctionVar[ReflexCallable[[], int]],
  187. ) -> NumberVar[int]: ...
  188. @overload
  189. def call(
  190. self: FunctionVar[ReflexCallable[[], float]],
  191. ) -> NumberVar[float]: ...
  192. @overload
  193. def call( # pyright: ignore[reportOverlappingOverload]
  194. self: FunctionVar[ReflexCallable[[], str]],
  195. ) -> StringVar[str]: ...
  196. @overload
  197. def call(
  198. self: FunctionVar[ReflexCallable[[], SEQUENCE_TYPE]],
  199. ) -> ArrayVar[SEQUENCE_TYPE]: ...
  200. @overload
  201. def call(
  202. self: FunctionVar[ReflexCallable[[], MAPPING_TYPE]],
  203. ) -> ObjectVar[MAPPING_TYPE]: ...
  204. @overload
  205. def call(
  206. self: FunctionVar[ReflexCallable[[], R]],
  207. ) -> Var[R]: ...
  208. @overload
  209. def call(
  210. self: FunctionVar[ReflexCallable[[VarWithDefault[V1]], bool]],
  211. arg1: Union[V1, Var[V1], Unset] = Unset(),
  212. ) -> BooleanVar: ...
  213. @overload
  214. def call(
  215. self: FunctionVar[ReflexCallable[[VarWithDefault[V1]], int]],
  216. arg1: Union[V1, Var[V1], Unset] = Unset(),
  217. ) -> NumberVar[int]: ...
  218. @overload
  219. def call(
  220. self: FunctionVar[ReflexCallable[[VarWithDefault[V1]], float]],
  221. arg1: Union[V1, Var[V1], Unset] = Unset(),
  222. ) -> NumberVar[float]: ...
  223. @overload
  224. def call(
  225. self: FunctionVar[ReflexCallable[[VarWithDefault[V1]], str]],
  226. arg1: Union[V1, Var[V1], Unset] = Unset(),
  227. ) -> StringVar[str]: ...
  228. @overload
  229. def call(
  230. self: FunctionVar[ReflexCallable[[VarWithDefault[V1]], SEQUENCE_TYPE]],
  231. arg1: Union[V1, Var[V1], Unset] = Unset(),
  232. ) -> ArrayVar[SEQUENCE_TYPE]: ...
  233. @overload
  234. def call(
  235. self: FunctionVar[ReflexCallable[[VarWithDefault[V1]], MAPPING_TYPE]],
  236. arg1: Union[V1, Var[V1], Unset] = Unset(),
  237. ) -> ObjectVar[MAPPING_TYPE]: ...
  238. @overload
  239. def call(
  240. self: FunctionVar[ReflexCallable[[VarWithDefault[V1]], R]],
  241. arg1: Union[V1, Var[V1], Unset] = Unset(),
  242. ) -> Var[R]: ...
  243. @overload
  244. def call(
  245. self: FunctionVar[
  246. ReflexCallable[[VarWithDefault[V1], VarWithDefault[V2]], bool]
  247. ],
  248. arg1: Union[V1, Var[V1], Unset] = Unset(),
  249. arg2: Union[V2, Var[V2], Unset] = Unset(),
  250. ) -> BooleanVar: ...
  251. @overload
  252. def call(
  253. self: FunctionVar[
  254. ReflexCallable[[VarWithDefault[V1], VarWithDefault[V2]], int]
  255. ],
  256. arg1: Union[V1, Var[V1], Unset] = Unset(),
  257. arg2: Union[V2, Var[V2], Unset] = Unset(),
  258. ) -> NumberVar[int]: ...
  259. @overload
  260. def call(
  261. self: FunctionVar[
  262. ReflexCallable[[VarWithDefault[V1], VarWithDefault[V2]], float]
  263. ],
  264. arg1: Union[V1, Var[V1], Unset] = Unset(),
  265. arg2: Union[V2, Var[V2], Unset] = Unset(),
  266. ) -> NumberVar[float]: ...
  267. @overload
  268. def call(
  269. self: FunctionVar[
  270. ReflexCallable[[VarWithDefault[V1], VarWithDefault[V2]], str]
  271. ],
  272. arg1: Union[V1, Var[V1], Unset] = Unset(),
  273. arg2: Union[V2, Var[V2], Unset] = Unset(),
  274. ) -> StringVar[str]: ...
  275. @overload
  276. def call(
  277. self: FunctionVar[
  278. ReflexCallable[[VarWithDefault[V1], VarWithDefault[V2]], SEQUENCE_TYPE]
  279. ],
  280. arg1: Union[V1, Var[V1], Unset] = Unset(),
  281. arg2: Union[V2, Var[V2], Unset] = Unset(),
  282. ) -> ArrayVar[SEQUENCE_TYPE]: ...
  283. @overload
  284. def call(
  285. self: FunctionVar[
  286. ReflexCallable[[VarWithDefault[V1], VarWithDefault[V2]], MAPPING_TYPE]
  287. ],
  288. arg1: Union[V1, Var[V1], Unset] = Unset(),
  289. arg2: Union[V2, Var[V2], Unset] = Unset(),
  290. ) -> ObjectVar[MAPPING_TYPE]: ...
  291. @overload
  292. def call(
  293. self: FunctionVar[ReflexCallable[[VarWithDefault[V1], VarWithDefault[V2]], R]],
  294. arg1: Union[V1, Var[V1], Unset] = Unset(),
  295. arg2: Union[V2, Var[V2], Unset] = Unset(),
  296. ) -> Var[R]: ...
  297. @overload
  298. def call(
  299. self: FunctionVar[
  300. ReflexCallable[
  301. [VarWithDefault[V1], VarWithDefault[V2], VarWithDefault[V3]], bool
  302. ]
  303. ],
  304. arg1: Union[V1, Var[V1], Unset] = Unset(),
  305. arg2: Union[V2, Var[V2], Unset] = Unset(),
  306. arg3: Union[V3, Var[V3], Unset] = Unset(),
  307. ) -> BooleanVar: ...
  308. @overload
  309. def call(
  310. self: FunctionVar[
  311. ReflexCallable[
  312. [VarWithDefault[V1], VarWithDefault[V2], VarWithDefault[V3]], int
  313. ]
  314. ],
  315. arg1: Union[V1, Var[V1], Unset] = Unset(),
  316. arg2: Union[V2, Var[V2], Unset] = Unset(),
  317. arg3: Union[V3, Var[V3], Unset] = Unset(),
  318. ) -> NumberVar[int]: ...
  319. @overload
  320. def call(
  321. self: FunctionVar[
  322. ReflexCallable[
  323. [VarWithDefault[V1], VarWithDefault[V2], VarWithDefault[V3]], float
  324. ]
  325. ],
  326. arg1: Union[V1, Var[V1], Unset] = Unset(),
  327. arg2: Union[V2, Var[V2], Unset] = Unset(),
  328. arg3: Union[V3, Var[V3], Unset] = Unset(),
  329. ) -> NumberVar[float]: ...
  330. @overload
  331. def call(
  332. self: FunctionVar[
  333. ReflexCallable[
  334. [VarWithDefault[V1], VarWithDefault[V2], VarWithDefault[V3]], str
  335. ]
  336. ],
  337. arg1: Union[V1, Var[V1], Unset] = Unset(),
  338. arg2: Union[V2, Var[V2], Unset] = Unset(),
  339. arg3: Union[V3, Var[V3], Unset] = Unset(),
  340. ) -> StringVar[str]: ...
  341. @overload
  342. def call(
  343. self: FunctionVar[
  344. ReflexCallable[
  345. [VarWithDefault[V1], VarWithDefault[V2], VarWithDefault[V3]],
  346. SEQUENCE_TYPE,
  347. ]
  348. ],
  349. arg1: Union[V1, Var[V1], Unset] = Unset(),
  350. arg2: Union[V2, Var[V2], Unset] = Unset(),
  351. arg3: Union[V3, Var[V3], Unset] = Unset(),
  352. ) -> ArrayVar[SEQUENCE_TYPE]: ...
  353. @overload
  354. def call(
  355. self: FunctionVar[
  356. ReflexCallable[
  357. [VarWithDefault[V1], VarWithDefault[V2], VarWithDefault[V3]],
  358. MAPPING_TYPE,
  359. ]
  360. ],
  361. arg1: Union[V1, Var[V1], Unset] = Unset(),
  362. arg2: Union[V2, Var[V2], Unset] = Unset(),
  363. arg3: Union[V3, Var[V3], Unset] = Unset(),
  364. ) -> ObjectVar[MAPPING_TYPE]: ...
  365. @overload
  366. def call(
  367. self: FunctionVar[
  368. ReflexCallable[
  369. [VarWithDefault[V1], VarWithDefault[V2], VarWithDefault[V3]], R
  370. ]
  371. ],
  372. arg1: Union[V1, Var[V1], Unset] = Unset(),
  373. arg2: Union[V2, Var[V2], Unset] = Unset(),
  374. arg3: Union[V3, Var[V3], Unset] = Unset(),
  375. ) -> Var[R]: ...
  376. @overload
  377. def call(
  378. self: FunctionVar[
  379. ReflexCallable[
  380. [
  381. VarWithDefault[V1],
  382. VarWithDefault[V2],
  383. VarWithDefault[V3],
  384. VarWithDefault[V4],
  385. ],
  386. bool,
  387. ]
  388. ],
  389. arg1: Union[V1, Var[V1], Unset] = Unset(),
  390. arg2: Union[V2, Var[V2], Unset] = Unset(),
  391. arg3: Union[V3, Var[V3], Unset] = Unset(),
  392. arg4: Union[V4, Var[V4], Unset] = Unset(),
  393. ) -> BooleanVar: ...
  394. @overload
  395. def call(
  396. self: FunctionVar[
  397. ReflexCallable[
  398. [
  399. VarWithDefault[V1],
  400. VarWithDefault[V2],
  401. VarWithDefault[V3],
  402. VarWithDefault[V4],
  403. ],
  404. int,
  405. ]
  406. ],
  407. arg1: Union[V1, Var[V1], Unset] = Unset(),
  408. arg2: Union[V2, Var[V2], Unset] = Unset(),
  409. arg3: Union[V3, Var[V3], Unset] = Unset(),
  410. arg4: Union[V4, Var[V4], Unset] = Unset(),
  411. ) -> NumberVar[int]: ...
  412. @overload
  413. def call(
  414. self: FunctionVar[
  415. ReflexCallable[
  416. [
  417. VarWithDefault[V1],
  418. VarWithDefault[V2],
  419. VarWithDefault[V3],
  420. VarWithDefault[V4],
  421. ],
  422. float,
  423. ]
  424. ],
  425. arg1: Union[V1, Var[V1], Unset] = Unset(),
  426. arg2: Union[V2, Var[V2], Unset] = Unset(),
  427. arg3: Union[V3, Var[V3], Unset] = Unset(),
  428. arg4: Union[V4, Var[V4], Unset] = Unset(),
  429. ) -> NumberVar[float]: ...
  430. @overload
  431. def call(
  432. self: FunctionVar[
  433. ReflexCallable[
  434. [
  435. VarWithDefault[V1],
  436. VarWithDefault[V2],
  437. VarWithDefault[V3],
  438. VarWithDefault[V4],
  439. ],
  440. str,
  441. ]
  442. ],
  443. arg1: Union[V1, Var[V1], Unset] = Unset(),
  444. arg2: Union[V2, Var[V2], Unset] = Unset(),
  445. arg3: Union[V3, Var[V3], Unset] = Unset(),
  446. arg4: Union[V4, Var[V4], Unset] = Unset(),
  447. ) -> StringVar[str]: ...
  448. @overload
  449. def call(
  450. self: FunctionVar[
  451. ReflexCallable[
  452. [
  453. VarWithDefault[V1],
  454. VarWithDefault[V2],
  455. VarWithDefault[V3],
  456. VarWithDefault[V4],
  457. ],
  458. SEQUENCE_TYPE,
  459. ]
  460. ],
  461. arg1: Union[V1, Var[V1], Unset] = Unset(),
  462. arg2: Union[V2, Var[V2], Unset] = Unset(),
  463. arg3: Union[V3, Var[V3], Unset] = Unset(),
  464. arg4: Union[V4, Var[V4], Unset] = Unset(),
  465. ) -> ArrayVar[SEQUENCE_TYPE]: ...
  466. @overload
  467. def call(
  468. self: FunctionVar[
  469. ReflexCallable[
  470. [
  471. VarWithDefault[V1],
  472. VarWithDefault[V2],
  473. VarWithDefault[V3],
  474. VarWithDefault[V4],
  475. ],
  476. MAPPING_TYPE,
  477. ]
  478. ],
  479. arg1: Union[V1, Var[V1], Unset] = Unset(),
  480. arg2: Union[V2, Var[V2], Unset] = Unset(),
  481. arg3: Union[V3, Var[V3], Unset] = Unset(),
  482. arg4: Union[V4, Var[V4], Unset] = Unset(),
  483. ) -> ObjectVar[MAPPING_TYPE]: ...
  484. @overload
  485. def call(
  486. self: FunctionVar[
  487. ReflexCallable[
  488. [
  489. VarWithDefault[V1],
  490. VarWithDefault[V2],
  491. VarWithDefault[V3],
  492. VarWithDefault[V4],
  493. ],
  494. R,
  495. ]
  496. ],
  497. arg1: Union[V1, Var[V1], Unset] = Unset(),
  498. arg2: Union[V2, Var[V2], Unset] = Unset(),
  499. arg3: Union[V3, Var[V3], Unset] = Unset(),
  500. arg4: Union[V4, Var[V4], Unset] = Unset(),
  501. ) -> Var[R]: ...
  502. @overload
  503. def call(
  504. self: FunctionVar[ReflexCallable[[V1], bool]], arg1: Union[V1, Var[V1]]
  505. ) -> BooleanVar: ...
  506. @overload
  507. def call(
  508. self: FunctionVar[ReflexCallable[[V1], int]], arg1: Union[V1, Var[V1]]
  509. ) -> NumberVar[int]: ...
  510. @overload
  511. def call(
  512. self: FunctionVar[ReflexCallable[[V1], float]], arg1: Union[V1, Var[V1]]
  513. ) -> NumberVar[float]: ...
  514. @overload
  515. def call(
  516. self: FunctionVar[ReflexCallable[[V1], str]], arg1: Union[V1, Var[V1]]
  517. ) -> StringVar[str]: ...
  518. @overload
  519. def call(
  520. self: FunctionVar[ReflexCallable[[V1], SEQUENCE_TYPE]], arg1: Union[V1, Var[V1]]
  521. ) -> ArrayVar[SEQUENCE_TYPE]: ...
  522. @overload
  523. def call(
  524. self: FunctionVar[ReflexCallable[[V1], MAPPING_TYPE]], arg1: Union[V1, Var[V1]]
  525. ) -> ObjectVar[MAPPING_TYPE]: ...
  526. @overload
  527. def call(
  528. self: FunctionVar[ReflexCallable[[V1], R]], arg1: Union[V1, Var[V1]]
  529. ) -> Var[R]: ...
  530. @overload
  531. def call(
  532. self: FunctionVar[ReflexCallable[[V1, VarWithDefault[V2]], bool]],
  533. arg1: Union[V1, Var[V1]],
  534. arg2: Union[V2, Var[V2], Unset] = Unset(),
  535. ) -> BooleanVar: ...
  536. @overload
  537. def call(
  538. self: FunctionVar[ReflexCallable[[V1, VarWithDefault[V2]], int]],
  539. arg1: Union[V1, Var[V1]],
  540. arg2: Union[V2, Var[V2], Unset] = Unset(),
  541. ) -> NumberVar[int]: ...
  542. @overload
  543. def call(
  544. self: FunctionVar[ReflexCallable[[V1, VarWithDefault[V2]], float]],
  545. arg1: Union[V1, Var[V1]],
  546. arg2: Union[V2, Var[V2], Unset] = Unset(),
  547. ) -> NumberVar[float]: ...
  548. @overload
  549. def call(
  550. self: FunctionVar[ReflexCallable[[V1, VarWithDefault[V2]], str]],
  551. arg1: Union[V1, Var[V1]],
  552. arg2: Union[V2, Var[V2], Unset] = Unset(),
  553. ) -> StringVar[str]: ...
  554. @overload
  555. def call(
  556. self: FunctionVar[ReflexCallable[[V1, VarWithDefault[V2]], SEQUENCE_TYPE]],
  557. arg1: Union[V1, Var[V1]],
  558. arg2: Union[V2, Var[V2], Unset] = Unset(),
  559. ) -> ArrayVar[SEQUENCE_TYPE]: ...
  560. @overload
  561. def call(
  562. self: FunctionVar[ReflexCallable[[V1, VarWithDefault[V2]], MAPPING_TYPE]],
  563. arg1: Union[V1, Var[V1]],
  564. arg2: Union[V2, Var[V2], Unset] = Unset(),
  565. ) -> ObjectVar[MAPPING_TYPE]: ...
  566. @overload
  567. def call(
  568. self: FunctionVar[ReflexCallable[[V1, VarWithDefault[V2]], R]],
  569. arg1: Union[V1, Var[V1]],
  570. arg2: Union[V2, Var[V2], Unset] = Unset(),
  571. ) -> Var[R]: ...
  572. @overload
  573. def call(
  574. self: FunctionVar[
  575. ReflexCallable[[V1, VarWithDefault[V2], VarWithDefault[V3]], bool]
  576. ],
  577. arg1: Union[V1, Var[V1]],
  578. arg2: Union[V2, Var[V2], Unset] = Unset(),
  579. arg3: Union[V3, Var[V3], Unset] = Unset(),
  580. ) -> BooleanVar: ...
  581. @overload
  582. def call(
  583. self: FunctionVar[
  584. ReflexCallable[[V1, VarWithDefault[V2], VarWithDefault[V3]], int]
  585. ],
  586. arg1: Union[V1, Var[V1]],
  587. arg2: Union[V2, Var[V2], Unset] = Unset(),
  588. arg3: Union[V3, Var[V3], Unset] = Unset(),
  589. ) -> NumberVar[int]: ...
  590. @overload
  591. def call(
  592. self: FunctionVar[
  593. ReflexCallable[[V1, VarWithDefault[V2], VarWithDefault[V3]], float]
  594. ],
  595. arg1: Union[V1, Var[V1]],
  596. arg2: Union[V2, Var[V2], Unset] = Unset(),
  597. arg3: Union[V3, Var[V3], Unset] = Unset(),
  598. ) -> NumberVar[float]: ...
  599. @overload
  600. def call(
  601. self: FunctionVar[
  602. ReflexCallable[[V1, VarWithDefault[V2], VarWithDefault[V3]], str]
  603. ],
  604. arg1: Union[V1, Var[V1]],
  605. arg2: Union[V2, Var[V2], Unset] = Unset(),
  606. arg3: Union[V3, Var[V3], Unset] = Unset(),
  607. ) -> StringVar[str]: ...
  608. @overload
  609. def call(
  610. self: FunctionVar[
  611. ReflexCallable[[V1, VarWithDefault[V2], VarWithDefault[V3]], SEQUENCE_TYPE]
  612. ],
  613. arg1: Union[V1, Var[V1]],
  614. arg2: Union[V2, Var[V2], Unset] = Unset(),
  615. arg3: Union[V3, Var[V3], Unset] = Unset(),
  616. ) -> ArrayVar[SEQUENCE_TYPE]: ...
  617. @overload
  618. def call(
  619. self: FunctionVar[
  620. ReflexCallable[[V1, VarWithDefault[V2], VarWithDefault[V3]], MAPPING_TYPE]
  621. ],
  622. arg1: Union[V1, Var[V1]],
  623. arg2: Union[V2, Var[V2], Unset] = Unset(),
  624. arg3: Union[V3, Var[V3], Unset] = Unset(),
  625. ) -> ObjectVar[MAPPING_TYPE]: ...
  626. @overload
  627. def call(
  628. self: FunctionVar[
  629. ReflexCallable[[V1, VarWithDefault[V2], VarWithDefault[V3]], R]
  630. ],
  631. arg1: Union[V1, Var[V1]],
  632. arg2: Union[V2, Var[V2], Unset] = Unset(),
  633. arg3: Union[V3, Var[V3], Unset] = Unset(),
  634. ) -> Var[R]: ...
  635. @overload
  636. def call(
  637. self: FunctionVar[
  638. ReflexCallable[
  639. [V1, VarWithDefault[V2], VarWithDefault[V3], VarWithDefault[V4]], bool
  640. ]
  641. ],
  642. arg1: Union[V1, Var[V1]],
  643. arg2: Union[V2, Var[V2], Unset] = Unset(),
  644. arg3: Union[V3, Var[V3], Unset] = Unset(),
  645. arg4: Union[V4, Var[V4], Unset] = Unset(),
  646. ) -> BooleanVar: ...
  647. @overload
  648. def call(
  649. self: FunctionVar[
  650. ReflexCallable[
  651. [V1, VarWithDefault[V2], VarWithDefault[V3], VarWithDefault[V4]], int
  652. ]
  653. ],
  654. arg1: Union[V1, Var[V1]],
  655. arg2: Union[V2, Var[V2], Unset] = Unset(),
  656. arg3: Union[V3, Var[V3], Unset] = Unset(),
  657. arg4: Union[V4, Var[V4], Unset] = Unset(),
  658. ) -> NumberVar[int]: ...
  659. @overload
  660. def call(
  661. self: FunctionVar[
  662. ReflexCallable[
  663. [V1, VarWithDefault[V2], VarWithDefault[V3], VarWithDefault[V4]], float
  664. ]
  665. ],
  666. arg1: Union[V1, Var[V1]],
  667. arg2: Union[V2, Var[V2], Unset] = Unset(),
  668. arg3: Union[V3, Var[V3], Unset] = Unset(),
  669. arg4: Union[V4, Var[V4], Unset] = Unset(),
  670. ) -> NumberVar[float]: ...
  671. @overload
  672. def call(
  673. self: FunctionVar[
  674. ReflexCallable[
  675. [V1, VarWithDefault[V2], VarWithDefault[V3], VarWithDefault[V4]], str
  676. ]
  677. ],
  678. arg1: Union[V1, Var[V1]],
  679. arg2: Union[V2, Var[V2], Unset] = Unset(),
  680. arg3: Union[V3, Var[V3], Unset] = Unset(),
  681. arg4: Union[V4, Var[V4], Unset] = Unset(),
  682. ) -> StringVar[str]: ...
  683. @overload
  684. def call(
  685. self: FunctionVar[
  686. ReflexCallable[
  687. [V1, VarWithDefault[V2], VarWithDefault[V3], VarWithDefault[V4]],
  688. SEQUENCE_TYPE,
  689. ]
  690. ],
  691. arg1: Union[V1, Var[V1]],
  692. arg2: Union[V2, Var[V2], Unset] = Unset(),
  693. arg3: Union[V3, Var[V3], Unset] = Unset(),
  694. arg4: Union[V4, Var[V4], Unset] = Unset(),
  695. ) -> ArrayVar[SEQUENCE_TYPE]: ...
  696. @overload
  697. def call(
  698. self: FunctionVar[
  699. ReflexCallable[
  700. [V1, VarWithDefault[V2], VarWithDefault[V3], VarWithDefault[V4]],
  701. MAPPING_TYPE,
  702. ]
  703. ],
  704. arg1: Union[V1, Var[V1]],
  705. arg2: Union[V2, Var[V2], Unset] = Unset(),
  706. arg3: Union[V3, Var[V3], Unset] = Unset(),
  707. arg4: Union[V4, Var[V4], Unset] = Unset(),
  708. ) -> ObjectVar[MAPPING_TYPE]: ...
  709. @overload
  710. def call(
  711. self: FunctionVar[
  712. ReflexCallable[
  713. [V1, VarWithDefault[V2], VarWithDefault[V3], VarWithDefault[V4]], R
  714. ]
  715. ],
  716. arg1: Union[V1, Var[V1]],
  717. arg2: Union[V2, Var[V2], Unset] = Unset(),
  718. arg3: Union[V3, Var[V3], Unset] = Unset(),
  719. arg4: Union[V4, Var[V4], Unset] = Unset(),
  720. ) -> Var[R]: ...
  721. @overload
  722. def call(
  723. self: FunctionVar[ReflexCallable[[V1, V2], bool]],
  724. arg1: Union[V1, Var[V1]],
  725. arg2: Union[V2, Var[V2]],
  726. ) -> BooleanVar: ...
  727. @overload
  728. def call(
  729. self: FunctionVar[ReflexCallable[[V1, V2], int]],
  730. arg1: Union[V1, Var[V1]],
  731. arg2: Union[V2, Var[V2]],
  732. ) -> NumberVar[int]: ...
  733. @overload
  734. def call(
  735. self: FunctionVar[ReflexCallable[[V1, V2], float]],
  736. arg1: Union[V1, Var[V1]],
  737. arg2: Union[V2, Var[V2]],
  738. ) -> NumberVar[float]: ...
  739. @overload
  740. def call(
  741. self: FunctionVar[ReflexCallable[[V1, V2], str]],
  742. arg1: Union[V1, Var[V1]],
  743. arg2: Union[V2, Var[V2]],
  744. ) -> StringVar[str]: ...
  745. @overload
  746. def call(
  747. self: FunctionVar[ReflexCallable[[V1, V2], SEQUENCE_TYPE]],
  748. arg1: Union[V1, Var[V1]],
  749. arg2: Union[V2, Var[V2]],
  750. ) -> ArrayVar[SEQUENCE_TYPE]: ...
  751. @overload
  752. def call(
  753. self: FunctionVar[ReflexCallable[[V1, V2], MAPPING_TYPE]],
  754. arg1: Union[V1, Var[V1]],
  755. arg2: Union[V2, Var[V2]],
  756. ) -> ObjectVar[MAPPING_TYPE]: ...
  757. @overload
  758. def call(
  759. self: FunctionVar[ReflexCallable[[V1, V2], R]],
  760. arg1: Union[V1, Var[V1]],
  761. arg2: Union[V2, Var[V2]],
  762. ) -> Var[R]: ...
  763. @overload
  764. def call(
  765. self: FunctionVar[ReflexCallable[[V1, V2, VarWithDefault[V3]], bool]],
  766. arg1: Union[V1, Var[V1]],
  767. arg2: Union[V2, Var[V2]],
  768. arg3: Union[V3, Var[V3], Unset] = Unset(),
  769. ) -> BooleanVar: ...
  770. @overload
  771. def call(
  772. self: FunctionVar[ReflexCallable[[V1, V2, VarWithDefault[V3]], int]],
  773. arg1: Union[V1, Var[V1]],
  774. arg2: Union[V2, Var[V2]],
  775. arg3: Union[V3, Var[V3], Unset] = Unset(),
  776. ) -> NumberVar[int]: ...
  777. @overload
  778. def call(
  779. self: FunctionVar[ReflexCallable[[V1, V2, VarWithDefault[V3]], float]],
  780. arg1: Union[V1, Var[V1]],
  781. arg2: Union[V2, Var[V2]],
  782. arg3: Union[V3, Var[V3], Unset] = Unset(),
  783. ) -> NumberVar[float]: ...
  784. @overload
  785. def call(
  786. self: FunctionVar[ReflexCallable[[V1, V2, VarWithDefault[V3]], str]],
  787. arg1: Union[V1, Var[V1]],
  788. arg2: Union[V2, Var[V2]],
  789. arg3: Union[V3, Var[V3], Unset] = Unset(),
  790. ) -> StringVar[str]: ...
  791. @overload
  792. def call(
  793. self: FunctionVar[ReflexCallable[[V1, V2, VarWithDefault[V3]], SEQUENCE_TYPE]],
  794. arg1: Union[V1, Var[V1]],
  795. arg2: Union[V2, Var[V2]],
  796. arg3: Union[V3, Var[V3], Unset] = Unset(),
  797. ) -> ArrayVar[SEQUENCE_TYPE]: ...
  798. @overload
  799. def call(
  800. self: FunctionVar[ReflexCallable[[V1, V2, VarWithDefault[V3]], MAPPING_TYPE]],
  801. arg1: Union[V1, Var[V1]],
  802. arg2: Union[V2, Var[V2]],
  803. arg3: Union[V3, Var[V3], Unset] = Unset(),
  804. ) -> ObjectVar[MAPPING_TYPE]: ...
  805. @overload
  806. def call(
  807. self: FunctionVar[ReflexCallable[[V1, V2, VarWithDefault[V3]], R]],
  808. arg1: Union[V1, Var[V1]],
  809. arg2: Union[V2, Var[V2]],
  810. arg3: Union[V3, Var[V3], Unset] = Unset(),
  811. ) -> Var[R]: ...
  812. @overload
  813. def call(
  814. self: FunctionVar[
  815. ReflexCallable[[V1, V2, VarWithDefault[V3], VarWithDefault[V4]], bool]
  816. ],
  817. arg1: Union[V1, Var[V1]],
  818. arg2: Union[V2, Var[V2]],
  819. arg3: Union[V3, Var[V3], Unset] = Unset(),
  820. arg4: Union[V4, Var[V4], Unset] = Unset(),
  821. ) -> BooleanVar: ...
  822. @overload
  823. def call(
  824. self: FunctionVar[
  825. ReflexCallable[[V1, V2, VarWithDefault[V3], VarWithDefault[V4]], int]
  826. ],
  827. arg1: Union[V1, Var[V1]],
  828. arg2: Union[V2, Var[V2]],
  829. arg3: Union[V3, Var[V3], Unset] = Unset(),
  830. arg4: Union[V4, Var[V4], Unset] = Unset(),
  831. ) -> NumberVar[int]: ...
  832. @overload
  833. def call(
  834. self: FunctionVar[
  835. ReflexCallable[[V1, V2, VarWithDefault[V3], VarWithDefault[V4]], float]
  836. ],
  837. arg1: Union[V1, Var[V1]],
  838. arg2: Union[V2, Var[V2]],
  839. arg3: Union[V3, Var[V3], Unset] = Unset(),
  840. arg4: Union[V4, Var[V4], Unset] = Unset(),
  841. ) -> NumberVar[float]: ...
  842. @overload
  843. def call(
  844. self: FunctionVar[
  845. ReflexCallable[[V1, V2, VarWithDefault[V3], VarWithDefault[V4]], str]
  846. ],
  847. arg1: Union[V1, Var[V1]],
  848. arg2: Union[V2, Var[V2]],
  849. arg3: Union[V3, Var[V3], Unset] = Unset(),
  850. arg4: Union[V4, Var[V4], Unset] = Unset(),
  851. ) -> StringVar[str]: ...
  852. @overload
  853. def call(
  854. self: FunctionVar[
  855. ReflexCallable[
  856. [V1, V2, VarWithDefault[V3], VarWithDefault[V4]], SEQUENCE_TYPE
  857. ]
  858. ],
  859. arg1: Union[V1, Var[V1]],
  860. arg2: Union[V2, Var[V2]],
  861. arg3: Union[V3, Var[V3], Unset] = Unset(),
  862. arg4: Union[V4, Var[V4], Unset] = Unset(),
  863. ) -> ArrayVar[SEQUENCE_TYPE]: ...
  864. @overload
  865. def call(
  866. self: FunctionVar[
  867. ReflexCallable[
  868. [V1, V2, VarWithDefault[V3], VarWithDefault[V4]], MAPPING_TYPE
  869. ]
  870. ],
  871. arg1: Union[V1, Var[V1]],
  872. arg2: Union[V2, Var[V2]],
  873. arg3: Union[V3, Var[V3], Unset] = Unset(),
  874. arg4: Union[V4, Var[V4], Unset] = Unset(),
  875. ) -> ObjectVar[MAPPING_TYPE]: ...
  876. @overload
  877. def call(
  878. self: FunctionVar[
  879. ReflexCallable[[V1, V2, VarWithDefault[V3], VarWithDefault[V4]], R]
  880. ],
  881. arg1: Union[V1, Var[V1]],
  882. arg2: Union[V2, Var[V2]],
  883. arg3: Union[V3, Var[V3], Unset] = Unset(),
  884. arg4: Union[V4, Var[V4], Unset] = Unset(),
  885. ) -> Var[R]: ...
  886. @overload
  887. def call(
  888. self: FunctionVar[ReflexCallable[[V1, V2, V3], bool]],
  889. arg1: Union[V1, Var[V1]],
  890. arg2: Union[V2, Var[V2]],
  891. arg3: Union[V3, Var[V3]],
  892. ) -> BooleanVar: ...
  893. @overload
  894. def call(
  895. self: FunctionVar[ReflexCallable[[V1, V2, V3], int]],
  896. arg1: Union[V1, Var[V1]],
  897. arg2: Union[V2, Var[V2]],
  898. arg3: Union[V3, Var[V3]],
  899. ) -> NumberVar[int]: ...
  900. @overload
  901. def call(
  902. self: FunctionVar[ReflexCallable[[V1, V2, V3], float]],
  903. arg1: Union[V1, Var[V1]],
  904. arg2: Union[V2, Var[V2]],
  905. arg3: Union[V3, Var[V3]],
  906. ) -> NumberVar[float]: ...
  907. @overload
  908. def call(
  909. self: FunctionVar[ReflexCallable[[V1, V2, V3], str]],
  910. arg1: Union[V1, Var[V1]],
  911. arg2: Union[V2, Var[V2]],
  912. arg3: Union[V3, Var[V3]],
  913. ) -> StringVar[str]: ...
  914. @overload
  915. def call(
  916. self: FunctionVar[ReflexCallable[[V1, V2, V3], SEQUENCE_TYPE]],
  917. arg1: Union[V1, Var[V1]],
  918. arg2: Union[V2, Var[V2]],
  919. arg3: Union[V3, Var[V3]],
  920. ) -> ArrayVar[SEQUENCE_TYPE]: ...
  921. @overload
  922. def call(
  923. self: FunctionVar[ReflexCallable[[V1, V2, V3], MAPPING_TYPE]],
  924. arg1: Union[V1, Var[V1]],
  925. arg2: Union[V2, Var[V2]],
  926. arg3: Union[V3, Var[V3]],
  927. ) -> ObjectVar[MAPPING_TYPE]: ...
  928. @overload
  929. def call(
  930. self: FunctionVar[ReflexCallable[[V1, V2, V3], R]],
  931. arg1: Union[V1, Var[V1]],
  932. arg2: Union[V2, Var[V2]],
  933. arg3: Union[V3, Var[V3]],
  934. ) -> Var[R]: ...
  935. @overload
  936. def call(
  937. self: FunctionVar[ReflexCallable[[V1, V2, V3, VarWithDefault[V4]], bool]],
  938. arg1: Union[V1, Var[V1]],
  939. arg2: Union[V2, Var[V2]],
  940. arg3: Union[V3, Var[V3]],
  941. arg4: Union[V4, Var[V4], Unset] = Unset(),
  942. ) -> BooleanVar: ...
  943. @overload
  944. def call(
  945. self: FunctionVar[ReflexCallable[[V1, V2, V3, VarWithDefault[V4]], int]],
  946. arg1: Union[V1, Var[V1]],
  947. arg2: Union[V2, Var[V2]],
  948. arg3: Union[V3, Var[V3]],
  949. arg4: Union[V4, Var[V4], Unset] = Unset(),
  950. ) -> NumberVar[int]: ...
  951. @overload
  952. def call(
  953. self: FunctionVar[ReflexCallable[[V1, V2, V3, VarWithDefault[V4]], float]],
  954. arg1: Union[V1, Var[V1]],
  955. arg2: Union[V2, Var[V2]],
  956. arg3: Union[V3, Var[V3]],
  957. arg4: Union[V4, Var[V4], Unset] = Unset(),
  958. ) -> NumberVar[float]: ...
  959. @overload
  960. def call(
  961. self: FunctionVar[ReflexCallable[[V1, V2, V3, VarWithDefault[V4]], str]],
  962. arg1: Union[V1, Var[V1]],
  963. arg2: Union[V2, Var[V2]],
  964. arg3: Union[V3, Var[V3]],
  965. arg4: Union[V4, Var[V4], Unset] = Unset(),
  966. ) -> StringVar[str]: ...
  967. @overload
  968. def call(
  969. self: FunctionVar[
  970. ReflexCallable[[V1, V2, V3, VarWithDefault[V4]], SEQUENCE_TYPE]
  971. ],
  972. arg1: Union[V1, Var[V1]],
  973. arg2: Union[V2, Var[V2]],
  974. arg3: Union[V3, Var[V3]],
  975. arg4: Union[V4, Var[V4], Unset] = Unset(),
  976. ) -> ArrayVar[SEQUENCE_TYPE]: ...
  977. @overload
  978. def call(
  979. self: FunctionVar[
  980. ReflexCallable[[V1, V2, V3, VarWithDefault[V4]], MAPPING_TYPE]
  981. ],
  982. arg1: Union[V1, Var[V1]],
  983. arg2: Union[V2, Var[V2]],
  984. arg3: Union[V3, Var[V3]],
  985. arg4: Union[V4, Var[V4], Unset] = Unset(),
  986. ) -> ObjectVar[MAPPING_TYPE]: ...
  987. @overload
  988. def call(
  989. self: FunctionVar[ReflexCallable[[V1, V2, V3, VarWithDefault[V4]], R]],
  990. arg1: Union[V1, Var[V1]],
  991. arg2: Union[V2, Var[V2]],
  992. arg3: Union[V3, Var[V3]],
  993. arg4: Union[V4, Var[V4], Unset] = Unset(),
  994. ) -> Var[R]: ...
  995. @overload
  996. def call(
  997. self: FunctionVar[ReflexCallable[[V1, V2, V3, V4], bool]],
  998. arg1: Union[V1, Var[V1]],
  999. arg2: Union[V2, Var[V2]],
  1000. arg3: Union[V3, Var[V3]],
  1001. arg4: Union[V4, Var[V4]],
  1002. ) -> BooleanVar: ...
  1003. @overload
  1004. def call(
  1005. self: FunctionVar[ReflexCallable[[V1, V2, V3, V4], int]],
  1006. arg1: Union[V1, Var[V1]],
  1007. arg2: Union[V2, Var[V2]],
  1008. arg3: Union[V3, Var[V3]],
  1009. arg4: Union[V4, Var[V4]],
  1010. ) -> NumberVar[int]: ...
  1011. @overload
  1012. def call(
  1013. self: FunctionVar[ReflexCallable[[V1, V2, V3, V4], float]],
  1014. arg1: Union[V1, Var[V1]],
  1015. arg2: Union[V2, Var[V2]],
  1016. arg3: Union[V3, Var[V3]],
  1017. arg4: Union[V4, Var[V4]],
  1018. ) -> NumberVar[float]: ...
  1019. @overload
  1020. def call(
  1021. self: FunctionVar[ReflexCallable[[V1, V2, V3, V4], str]],
  1022. arg1: Union[V1, Var[V1]],
  1023. arg2: Union[V2, Var[V2]],
  1024. arg3: Union[V3, Var[V3]],
  1025. arg4: Union[V4, Var[V4]],
  1026. ) -> StringVar[str]: ...
  1027. @overload
  1028. def call(
  1029. self: FunctionVar[ReflexCallable[[V1, V2, V3, V4], SEQUENCE_TYPE]],
  1030. arg1: Union[V1, Var[V1]],
  1031. arg2: Union[V2, Var[V2]],
  1032. arg3: Union[V3, Var[V3]],
  1033. arg4: Union[V4, Var[V4]],
  1034. ) -> ArrayVar[SEQUENCE_TYPE]: ...
  1035. @overload
  1036. def call(
  1037. self: FunctionVar[ReflexCallable[[V1, V2, V3, V4], MAPPING_TYPE]],
  1038. arg1: Union[V1, Var[V1]],
  1039. arg2: Union[V2, Var[V2]],
  1040. arg3: Union[V3, Var[V3]],
  1041. arg4: Union[V4, Var[V4]],
  1042. ) -> ObjectVar[MAPPING_TYPE]: ...
  1043. @overload
  1044. def call(
  1045. self: FunctionVar[ReflexCallable[[V1, V2, V3, V4], R]],
  1046. arg1: Union[V1, Var[V1]],
  1047. arg2: Union[V2, Var[V2]],
  1048. arg3: Union[V3, Var[V3]],
  1049. arg4: Union[V4, Var[V4]],
  1050. ) -> Var[R]: ...
  1051. # Capture Any to allow for arbitrary number of arguments
  1052. @overload
  1053. def call(self: FunctionVar[NoReturn], *args: Var | Any) -> Var: ...
  1054. def call(self, *args: Var | Any) -> Var: # pyright: ignore [reportInconsistentOverload]
  1055. """Call the function with the given arguments.
  1056. Args:
  1057. *args: The arguments to call the function with.
  1058. Returns:
  1059. The function call operation.
  1060. Raises:
  1061. VarTypeError: If the number of arguments is invalid
  1062. """
  1063. required_arg_len = self._required_arg_len()
  1064. arg_len = self._arg_len()
  1065. if arg_len is not None:
  1066. if len(args) < required_arg_len:
  1067. raise VarTypeError(
  1068. f"Passed {len(args)} arguments, expected at least {required_arg_len} for {self!s}"
  1069. )
  1070. if len(args) > arg_len:
  1071. raise VarTypeError(
  1072. f"Passed {len(args)} arguments, expected at most {arg_len} for {self!s}"
  1073. )
  1074. args = tuple(map(LiteralVar.create, args))
  1075. self._pre_check(*args)
  1076. return_type = self._return_type(*args)
  1077. if isinstance(self, (ArgsFunctionOperation, ArgsFunctionOperationBuilder)):
  1078. default_args = self._default_args()
  1079. max_allowed_arguments = (
  1080. arg_len if arg_len is not None else len(args) + len(default_args)
  1081. )
  1082. provided_argument_count = len(args)
  1083. # we skip default args which we provided
  1084. default_args_provided = len(default_args) - (
  1085. max_allowed_arguments - provided_argument_count
  1086. )
  1087. full_args = args + tuple(default_args[default_args_provided:])
  1088. if self._raw_js_function is not None:
  1089. return VarOperationCall.create(
  1090. FunctionStringVar.create(
  1091. self._raw_js_function,
  1092. _var_type=self._var_type,
  1093. _var_data=self._get_all_var_data(),
  1094. ),
  1095. *full_args,
  1096. _var_type=return_type,
  1097. ).guess_type()
  1098. if self._original_var_operation is not None:
  1099. return ExpressionCall.create(
  1100. self._original_var_operation,
  1101. *full_args,
  1102. _var_data=self._get_all_var_data(),
  1103. _var_type=return_type,
  1104. ).guess_type()
  1105. return VarOperationCall.create(self, *args, _var_type=return_type).guess_type()
  1106. def chain(
  1107. self: FunctionVar[ReflexCallable[P, R]],
  1108. other: FunctionVar[ReflexCallable[[R], R2]]
  1109. | FunctionVar[ReflexCallable[[R, VarWithDefault[Any]], R2]]
  1110. | FunctionVar[
  1111. ReflexCallable[[R, VarWithDefault[Any], VarWithDefault[Any]], R2]
  1112. ],
  1113. ) -> FunctionVar[ReflexCallable[P, R2]]:
  1114. """Chain two functions together.
  1115. Args:
  1116. other: The other function to chain.
  1117. Returns:
  1118. The chained function.
  1119. """
  1120. self_arg_type, self_return_type = unwrap_reflex_callalbe(self._var_type)
  1121. _, other_return_type = unwrap_reflex_callalbe(other._var_type)
  1122. return ArgsFunctionOperationBuilder.create(
  1123. (),
  1124. VarOperationCall.create(
  1125. other,
  1126. VarOperationCall.create(
  1127. self, Var(_js_expr="...args"), _var_type=self_return_type
  1128. ),
  1129. _var_type=other_return_type,
  1130. ),
  1131. rest="arg",
  1132. _var_type=ReflexCallable[self_arg_type, other_return_type], # pyright: ignore [reportInvalidTypeArguments]
  1133. )
  1134. def _partial_type(
  1135. self, *args: Var | Any
  1136. ) -> Tuple[GenericType, Optional[TypeComputer]]:
  1137. """Override the type of the function call with the given arguments.
  1138. Args:
  1139. *args: The arguments to call the function with.
  1140. Returns:
  1141. The overridden type of the function call.
  1142. """
  1143. args_types, return_type = unwrap_reflex_callalbe(self._var_type)
  1144. if isinstance(args_types, tuple):
  1145. return ReflexCallable[[*args_types[len(args) :]], return_type], None
  1146. return ReflexCallable[..., return_type], None
  1147. def _arg_len(self) -> int | None:
  1148. """Get the number of arguments the function takes.
  1149. Returns:
  1150. The number of arguments the function takes.
  1151. """
  1152. args_types, _ = unwrap_reflex_callalbe(self._var_type)
  1153. if isinstance(args_types, tuple):
  1154. return len(args_types)
  1155. return None
  1156. def _required_arg_len(self) -> int:
  1157. """Get the number of required arguments the function takes.
  1158. Returns:
  1159. The number of required arguments the function takes.
  1160. """
  1161. args_types, _ = unwrap_reflex_callalbe(self._var_type)
  1162. if isinstance(args_types, tuple):
  1163. return sum(
  1164. 1
  1165. for arg_type in args_types
  1166. if get_origin(arg_type) is not VarWithDefault
  1167. )
  1168. return 0
  1169. def _default_args(self) -> list[Any]:
  1170. """Get the default arguments of the function.
  1171. Returns:
  1172. The default arguments of the function.
  1173. """
  1174. if isinstance(self, (ArgsFunctionOperation, ArgsFunctionOperationBuilder)):
  1175. return [
  1176. arg.default
  1177. for arg in self._default_values
  1178. if not isinstance(arg, inspect.Parameter.empty)
  1179. ]
  1180. return []
  1181. def _return_type(self, *args: Var | Any) -> GenericType:
  1182. """Override the type of the function call with the given arguments.
  1183. Args:
  1184. *args: The arguments to call the function with.
  1185. Returns:
  1186. The overridden type of the function call.
  1187. """
  1188. partial_types, _ = self._partial_type(*args)
  1189. return unwrap_reflex_callalbe(partial_types)[1]
  1190. def _pre_check(
  1191. self, *args: Var | Any
  1192. ) -> Tuple[Callable[[Any], Optional[str]], ...]:
  1193. """Check if the function can be called with the given arguments.
  1194. Args:
  1195. *args: The arguments to call the function with.
  1196. Returns:
  1197. True if the function can be called with the given arguments.
  1198. """
  1199. return ()
  1200. @overload
  1201. def __get__(self, instance: None, owner: Any) -> FunctionVar[CALLABLE_TYPE]: ...
  1202. @overload
  1203. def __get__(
  1204. self: FunctionVar[ReflexCallable[Concatenate[V1, P], R]],
  1205. instance: Var[V1],
  1206. owner: Any,
  1207. ) -> FunctionVar[ReflexCallable[P, R]]: ...
  1208. def __get__(self, instance: Any, owner: Any):
  1209. """Get the function var.
  1210. Args:
  1211. instance: The instance of the class.
  1212. owner: The owner of the class.
  1213. Returns:
  1214. The function var.
  1215. """
  1216. if instance is None:
  1217. return self
  1218. return self.partial(instance)
  1219. __call__ = call
  1220. @dataclasses.dataclass(frozen=True)
  1221. class ExpressionCall(CachedVarOperation, Var[R]):
  1222. """Class for expression calls."""
  1223. _original_var_operation: Callable = dataclasses.field(default=lambda *args: "")
  1224. _args: Tuple[Var, ...] = dataclasses.field(default_factory=tuple)
  1225. @cached_property_no_lock
  1226. def _cached_var_name(self) -> str:
  1227. """The name of the var.
  1228. Returns:
  1229. The name of the var.
  1230. """
  1231. return self._original_var_operation(*self._args)
  1232. @cached_property_no_lock
  1233. def _cached_get_all_var_data(self) -> VarData | None:
  1234. """Get all the var data associated with the var.
  1235. Returns:
  1236. All the var data associated with the var.
  1237. """
  1238. return VarData.merge(
  1239. *[arg._get_all_var_data() for arg in self._args],
  1240. self._var_data,
  1241. )
  1242. @classmethod
  1243. def create(
  1244. cls,
  1245. _original_var_operation: Callable,
  1246. *args: Var | Any,
  1247. _var_type: GenericType = Any,
  1248. _var_data: VarData | None = None,
  1249. ) -> ExpressionCall:
  1250. """Create a new expression call.
  1251. Args:
  1252. _original_var_operation: The original var operation.
  1253. *args: The arguments to call the expression with.
  1254. _var_data: Additional hooks and imports associated with the Var.
  1255. Returns:
  1256. The expression call var.
  1257. """
  1258. return ExpressionCall(
  1259. _js_expr="",
  1260. _var_type=_var_type,
  1261. _var_data=_var_data,
  1262. _original_var_operation=_original_var_operation,
  1263. _args=args,
  1264. )
  1265. class BuilderFunctionVar(
  1266. FunctionVar[CALLABLE_TYPE], default_type=ReflexCallable[Any, Any]
  1267. ):
  1268. """Base class for immutable function vars with the builder pattern."""
  1269. __call__ = FunctionVar.partial
  1270. class FunctionStringVar(FunctionVar[CALLABLE_TYPE]):
  1271. """Base class for immutable function vars from a string."""
  1272. @classmethod
  1273. def create(
  1274. cls,
  1275. func: str,
  1276. _var_type: Type[OTHER_CALLABLE_TYPE] = ReflexCallable[Any, Any],
  1277. _var_data: VarData | None = None,
  1278. ) -> FunctionStringVar[OTHER_CALLABLE_TYPE]:
  1279. """Create a new function var from a string.
  1280. Args:
  1281. func: The function to call.
  1282. _var_type: The type of the Var.
  1283. _var_data: Additional hooks and imports associated with the Var.
  1284. Returns:
  1285. The function var.
  1286. """
  1287. return FunctionStringVar(
  1288. _js_expr=func,
  1289. _var_type=_var_type,
  1290. _var_data=_var_data,
  1291. )
  1292. @dataclasses.dataclass(
  1293. eq=False,
  1294. frozen=True,
  1295. slots=True,
  1296. )
  1297. class VarOperationCall(Generic[P, R], CachedVarOperation, Var[R]):
  1298. """Base class for immutable vars that are the result of a function call."""
  1299. _func: Optional[FunctionVar[ReflexCallable[..., R]]] = dataclasses.field(
  1300. default=None
  1301. )
  1302. _args: Tuple[Union[Var, Any], ...] = dataclasses.field(default_factory=tuple)
  1303. @cached_property_no_lock
  1304. def _cached_var_name(self) -> str:
  1305. """The name of the var.
  1306. Returns:
  1307. The name of the var.
  1308. """
  1309. return f"({self._func!s}({', '.join([str(LiteralVar.create(arg)) for arg in self._args])}))"
  1310. @cached_property_no_lock
  1311. def _cached_get_all_var_data(self) -> VarData | None:
  1312. """Get all the var data associated with the var.
  1313. Returns:
  1314. All the var data associated with the var.
  1315. """
  1316. return VarData.merge(
  1317. self._func._get_all_var_data() if self._func is not None else None,
  1318. *[LiteralVar.create(arg)._get_all_var_data() for arg in self._args],
  1319. self._var_data,
  1320. )
  1321. @classmethod
  1322. def create(
  1323. cls,
  1324. func: FunctionVar[ReflexCallable[P, R]],
  1325. *args: Var | Any,
  1326. _var_type: GenericType = Any,
  1327. _var_data: VarData | None = None,
  1328. ) -> VarOperationCall:
  1329. """Create a new function call var.
  1330. Args:
  1331. func: The function to call.
  1332. *args: The arguments to call the function with.
  1333. _var_type: The type of the Var.
  1334. _var_data: Additional hooks and imports associated with the Var.
  1335. Returns:
  1336. The function call var.
  1337. """
  1338. function_return_type = (
  1339. func._var_type.__args__[1]
  1340. if getattr(func._var_type, "__args__", None)
  1341. else Any
  1342. )
  1343. var_type = _var_type if _var_type is not Any else function_return_type
  1344. return cls(
  1345. _js_expr="",
  1346. _var_type=var_type,
  1347. _var_data=_var_data,
  1348. _func=func,
  1349. _args=args,
  1350. )
  1351. @dataclasses.dataclass(frozen=True)
  1352. class DestructuredArg:
  1353. """Class for destructured arguments."""
  1354. fields: Tuple[str, ...] = ()
  1355. rest: Optional[str] = None
  1356. def to_javascript(self) -> str:
  1357. """Convert the destructured argument to JavaScript.
  1358. Returns:
  1359. The destructured argument in JavaScript.
  1360. """
  1361. return format.wrap(
  1362. ", ".join(self.fields) + (f", ...{self.rest}" if self.rest else ""),
  1363. "{",
  1364. "}",
  1365. )
  1366. @dataclasses.dataclass(
  1367. frozen=True,
  1368. )
  1369. class FunctionArgs:
  1370. """Class for function arguments."""
  1371. args: Tuple[Union[str, DestructuredArg], ...] = ()
  1372. rest: Optional[str] = None
  1373. def format_args_function_operation(
  1374. self: ArgsFunctionOperation | ArgsFunctionOperationBuilder,
  1375. ) -> str:
  1376. """Format an args function operation.
  1377. Args:
  1378. self: The function operation.
  1379. Returns:
  1380. The formatted args function operation.
  1381. """
  1382. arg_names_str = ", ".join(
  1383. [
  1384. (arg if isinstance(arg, str) else arg.to_javascript())
  1385. + (
  1386. f" = {default_value.default!s}"
  1387. if i < len(self._default_values)
  1388. and not isinstance(
  1389. (default_value := self._default_values[i]), inspect.Parameter.empty
  1390. )
  1391. else ""
  1392. )
  1393. for i, arg in enumerate(self._args.args)
  1394. ]
  1395. + ([f"...{self._args.rest}"] if self._args.rest else [])
  1396. )
  1397. return_expr_str = str(LiteralVar.create(self._return_expr))
  1398. # Wrap return expression in curly braces if explicit return syntax is used.
  1399. return_expr_str_wrapped = (
  1400. format.wrap(return_expr_str, "{", "}")
  1401. if self._explicit_return
  1402. else return_expr_str
  1403. )
  1404. return f"(({arg_names_str}) => {return_expr_str_wrapped})"
  1405. def pre_check_args(
  1406. self: ArgsFunctionOperation | ArgsFunctionOperationBuilder, *args: Var | Any
  1407. ) -> Tuple[Callable[[Any], Optional[str]], ...]:
  1408. """Check if the function can be called with the given arguments.
  1409. Args:
  1410. self: The function operation.
  1411. *args: The arguments to call the function with.
  1412. Returns:
  1413. True if the function can be called with the given arguments.
  1414. Raises:
  1415. VarTypeError: If the arguments are invalid.
  1416. """
  1417. for i, (validator, arg) in enumerate(zip(self._validators, args, strict=False)):
  1418. if (validation_message := validator(arg)) is not None:
  1419. arg_name = self._args.args[i] if i < len(self._args.args) else None
  1420. if arg_name is not None:
  1421. raise VarTypeError(
  1422. f"Invalid argument {arg!s} provided to {arg_name} in {self._function_name or 'var operation'}. {validation_message}"
  1423. )
  1424. raise VarTypeError(
  1425. f"Invalid argument {arg!s} provided to argument {i} in {self._function_name or 'var operation'}. {validation_message}"
  1426. )
  1427. return self._validators[len(args) :]
  1428. def figure_partial_type(
  1429. self: ArgsFunctionOperation | ArgsFunctionOperationBuilder,
  1430. *args: Var | Any,
  1431. ) -> Tuple[GenericType, Optional[TypeComputer]]:
  1432. """Figure out the return type of the function.
  1433. Args:
  1434. self: The function operation.
  1435. *args: The arguments to call the function with.
  1436. Returns:
  1437. The return type of the function.
  1438. """
  1439. return (
  1440. self._type_computer(*args)
  1441. if self._type_computer is not None
  1442. else FunctionVar._partial_type(self, *args)
  1443. )
  1444. @dataclasses.dataclass(
  1445. eq=False,
  1446. frozen=True,
  1447. slots=True,
  1448. )
  1449. class ArgsFunctionOperation(CachedVarOperation, FunctionVar[CALLABLE_TYPE]):
  1450. """Base class for immutable function defined via arguments and return expression."""
  1451. _args: FunctionArgs = dataclasses.field(default_factory=FunctionArgs)
  1452. _default_values: Tuple[VarWithDefault | inspect.Parameter.empty, ...] = (
  1453. dataclasses.field(default_factory=tuple)
  1454. )
  1455. _validators: Tuple[Callable[[Any], Optional[str]], ...] = dataclasses.field(
  1456. default_factory=tuple
  1457. )
  1458. _return_expr: Union[Var, Any] = dataclasses.field(default=None)
  1459. _function_name: str = dataclasses.field(default="")
  1460. _type_computer: Optional[TypeComputer] = dataclasses.field(default=None)
  1461. _explicit_return: bool = dataclasses.field(default=False)
  1462. _raw_js_function: str | None = dataclasses.field(default=None)
  1463. _original_var_operation: Callable | None = dataclasses.field(default=None)
  1464. _cached_var_name = cached_property_no_lock(format_args_function_operation)
  1465. _pre_check = pre_check_args
  1466. _partial_type = figure_partial_type
  1467. @classmethod
  1468. def create(
  1469. cls,
  1470. args_names: Sequence[Union[str, DestructuredArg]],
  1471. return_expr: Var | Any,
  1472. default_values: Sequence[VarWithDefault | inspect.Parameter.empty] = (),
  1473. rest: str | None = None,
  1474. validators: Sequence[Callable[[Any], Optional[str]]] = (),
  1475. function_name: str = "",
  1476. explicit_return: bool = False,
  1477. type_computer: Optional[TypeComputer] = None,
  1478. _raw_js_function: str | None = None,
  1479. _original_var_operation: Callable | None = None,
  1480. _var_type: GenericType = Callable,
  1481. _var_data: VarData | None = None,
  1482. ):
  1483. """Create a new function var.
  1484. Args:
  1485. args_names: The names of the arguments.
  1486. return_expr: The return expression of the function.
  1487. default_values: The default values of the arguments.
  1488. rest: The name of the rest argument.
  1489. validators: The validators for the arguments.
  1490. function_name: The name of the function.
  1491. explicit_return: Whether to use explicit return syntax.
  1492. type_computer: A function to compute the return type.
  1493. _raw_js_function: If provided, it will be used when the operation is being called with all of its arguments at once.
  1494. _original_var_operation: The original var operation, if any.
  1495. _var_type: The type of the var.
  1496. _var_data: Additional hooks and imports associated with the Var.
  1497. Returns:
  1498. The function var.
  1499. """
  1500. return_expr = Var.create(return_expr)
  1501. return cls(
  1502. _js_expr="",
  1503. _var_type=_var_type,
  1504. _var_data=_var_data,
  1505. _args=FunctionArgs(args=tuple(args_names), rest=rest),
  1506. _raw_js_function=_raw_js_function,
  1507. _original_var_operation=_original_var_operation,
  1508. _default_values=tuple(default_values),
  1509. _function_name=function_name,
  1510. _validators=tuple(validators),
  1511. _return_expr=return_expr,
  1512. _explicit_return=explicit_return,
  1513. _type_computer=type_computer,
  1514. )
  1515. @dataclasses.dataclass(
  1516. eq=False,
  1517. frozen=True,
  1518. slots=True,
  1519. )
  1520. class ArgsFunctionOperationBuilder(
  1521. CachedVarOperation, BuilderFunctionVar[CALLABLE_TYPE]
  1522. ):
  1523. """Base class for immutable function defined via arguments and return expression with the builder pattern."""
  1524. _args: FunctionArgs = dataclasses.field(default_factory=FunctionArgs)
  1525. _default_values: Tuple[VarWithDefault | inspect.Parameter.empty, ...] = (
  1526. dataclasses.field(default_factory=tuple)
  1527. )
  1528. _validators: Tuple[Callable[[Any], Optional[str]], ...] = dataclasses.field(
  1529. default_factory=tuple
  1530. )
  1531. _return_expr: Union[Var, Any] = dataclasses.field(default=None)
  1532. _function_name: str = dataclasses.field(default="")
  1533. _type_computer: Optional[TypeComputer] = dataclasses.field(default=None)
  1534. _explicit_return: bool = dataclasses.field(default=False)
  1535. _raw_js_function: str | None = dataclasses.field(default=None)
  1536. _original_var_operation: Callable | None = dataclasses.field(default=None)
  1537. _cached_var_name = cached_property_no_lock(format_args_function_operation)
  1538. _pre_check = pre_check_args
  1539. _partial_type = figure_partial_type
  1540. @classmethod
  1541. def create(
  1542. cls,
  1543. args_names: Sequence[Union[str, DestructuredArg]],
  1544. return_expr: Var | Any,
  1545. default_values: Sequence[VarWithDefault | inspect.Parameter.empty] = (),
  1546. rest: str | None = None,
  1547. validators: Sequence[Callable[[Any], Optional[str]]] = (),
  1548. function_name: str = "",
  1549. explicit_return: bool = False,
  1550. type_computer: Optional[TypeComputer] = None,
  1551. _raw_js_function: str | None = None,
  1552. _original_var_operation: Callable | None = None,
  1553. _var_type: GenericType = Callable,
  1554. _var_data: VarData | None = None,
  1555. ):
  1556. """Create a new function var.
  1557. Args:
  1558. args_names: The names of the arguments.
  1559. return_expr: The return expression of the function.
  1560. default_values: The default values of the arguments.
  1561. rest: The name of the rest argument.
  1562. validators: The validators for the arguments.
  1563. function_name: The name of the function.
  1564. explicit_return: Whether to use explicit return syntax.
  1565. type_computer: A function to compute the return type.
  1566. _raw_js_function: If provided, it will be used when the operation is being called with all of its arguments at once.
  1567. _original_var_operation: The original var operation, if any.
  1568. _var_type: The type of the var.
  1569. _var_data: Additional hooks and imports associated with the Var.
  1570. Returns:
  1571. The function var.
  1572. """
  1573. return_expr = Var.create(return_expr)
  1574. return cls(
  1575. _js_expr="",
  1576. _var_type=_var_type,
  1577. _var_data=_var_data,
  1578. _args=FunctionArgs(args=tuple(args_names), rest=rest),
  1579. _raw_js_function=_raw_js_function,
  1580. _original_var_operation=_original_var_operation,
  1581. _default_values=tuple(default_values),
  1582. _function_name=function_name,
  1583. _validators=tuple(validators),
  1584. _return_expr=return_expr,
  1585. _explicit_return=explicit_return,
  1586. _type_computer=type_computer,
  1587. )
  1588. if python_version := sys.version_info[:2] >= (3, 10):
  1589. JSON_STRINGIFY = FunctionStringVar.create(
  1590. "JSON.stringify", _var_type=ReflexCallable[[Any], str]
  1591. )
  1592. ARRAY_ISARRAY = FunctionStringVar.create(
  1593. "Array.isArray", _var_type=ReflexCallable[[Any], bool]
  1594. )
  1595. PROTOTYPE_TO_STRING = FunctionStringVar.create(
  1596. "((__to_string) => __to_string.toString())",
  1597. _var_type=ReflexCallable[[Any], str],
  1598. )
  1599. else:
  1600. JSON_STRINGIFY = FunctionStringVar.create(
  1601. "JSON.stringify", _var_type=ReflexCallable[Any, str]
  1602. )
  1603. ARRAY_ISARRAY = FunctionStringVar.create(
  1604. "Array.isArray", _var_type=ReflexCallable[Any, bool]
  1605. )
  1606. PROTOTYPE_TO_STRING = FunctionStringVar.create(
  1607. "((__to_string) => __to_string.toString())",
  1608. _var_type=ReflexCallable[Any, str],
  1609. )
  1610. def _generate_overloads_for_function_var_call(maximum_args: int = 4) -> str:
  1611. """Generate overloads for the function var call method.
  1612. Args:
  1613. maximum_args: The maximum number of arguments to generate overloads for.
  1614. Returns:
  1615. The generated overloads.
  1616. """
  1617. overloads = []
  1618. return_type_mapping = {
  1619. "bool": "BooleanVar",
  1620. "int": "NumberVar[int]",
  1621. "float": "NumberVar[float]",
  1622. "str": "StringVar[str]",
  1623. "SEQUENCE_TYPE": "ArrayVar[SEQUENCE_TYPE]",
  1624. "MAPPING_TYPE": "ObjectVar[MAPPING_TYPE]",
  1625. "R": "Var[R]",
  1626. }
  1627. for number_of_required_args in range(maximum_args + 1):
  1628. for number_of_optional_args in range(
  1629. maximum_args + 1 - number_of_required_args
  1630. ):
  1631. for return_type, return_type_var in return_type_mapping.items():
  1632. required_args = [
  1633. f"arg{j + 1}: Union[V{j + 1}, Var[V{j + 1}]]"
  1634. for j in range(number_of_required_args)
  1635. ]
  1636. optional_args = [
  1637. f"arg{j + 1}: Union[V{j + 1}, Var[V{j + 1}], Unset] = Unset()"
  1638. for j in range(
  1639. number_of_required_args,
  1640. number_of_required_args + number_of_optional_args,
  1641. )
  1642. ]
  1643. required_params = [f"V{j + 1}" for j in range(number_of_required_args)]
  1644. optional_params = [
  1645. f"VarWithDefault[V{j + 1}]"
  1646. for j in range(
  1647. number_of_required_args,
  1648. number_of_required_args + number_of_optional_args,
  1649. )
  1650. ]
  1651. function_type_hint = f"""FunctionVar[ReflexCallable[[{", ".join(required_params + optional_params)}], {return_type}]]"""
  1652. new_line = "\n"
  1653. overloads.append(
  1654. f"""
  1655. @overload
  1656. def call(
  1657. self: {function_type_hint},
  1658. {"," + new_line + " ".join(required_args + optional_args)}
  1659. ) -> {return_type_var}: ...
  1660. """
  1661. )
  1662. return "\n".join(overloads)