test_var.py 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  1. import decimal
  2. import json
  3. import math
  4. import typing
  5. from collections.abc import Mapping, Sequence
  6. from typing import cast
  7. import pytest
  8. from pandas import DataFrame
  9. from pytest_mock import MockerFixture
  10. import reflex as rx
  11. from reflex.base import Base
  12. from reflex.config import PerformanceMode
  13. from reflex.constants.base import REFLEX_VAR_CLOSING_TAG, REFLEX_VAR_OPENING_TAG
  14. from reflex.state import BaseState
  15. from reflex.utils.exceptions import (
  16. PrimitiveUnserializableToJSONError,
  17. UntypedComputedVarError,
  18. )
  19. from reflex.utils.imports import ImportVar
  20. from reflex.vars import VarData
  21. from reflex.vars.base import (
  22. ComputedVar,
  23. LiteralVar,
  24. Var,
  25. computed_var,
  26. var_operation,
  27. var_operation_return,
  28. )
  29. from reflex.vars.function import (
  30. ArgsFunctionOperation,
  31. DestructuredArg,
  32. FunctionStringVar,
  33. )
  34. from reflex.vars.number import LiteralBooleanVar, LiteralNumberVar, NumberVar
  35. from reflex.vars.object import LiteralObjectVar, ObjectVar
  36. from reflex.vars.sequence import (
  37. ArrayVar,
  38. ConcatVarOperation,
  39. LiteralArrayVar,
  40. LiteralStringVar,
  41. )
  42. test_vars = [
  43. Var(_js_expr="prop1", _var_type=int),
  44. Var(_js_expr="key", _var_type=str),
  45. Var(_js_expr="value", _var_type=str)._var_set_state("state"),
  46. Var(_js_expr="local", _var_type=str)._var_set_state("state"),
  47. Var(_js_expr="local2", _var_type=str),
  48. ]
  49. class ATestState(BaseState):
  50. """Test state."""
  51. value: str
  52. dict_val: dict[str, list] = {}
  53. @pytest.fixture
  54. def TestObj():
  55. class TestObj(Base):
  56. foo: int
  57. bar: str
  58. return TestObj
  59. @pytest.fixture
  60. def ParentState(TestObj):
  61. class ParentState(BaseState):
  62. foo: int
  63. bar: int
  64. @computed_var
  65. def var_without_annotation(self):
  66. return TestObj
  67. return ParentState
  68. @pytest.fixture
  69. def ChildState(ParentState, TestObj):
  70. class ChildState(ParentState):
  71. @computed_var
  72. def var_without_annotation(self):
  73. """This shadows ParentState.var_without_annotation.
  74. Returns:
  75. TestObj: Test object.
  76. """
  77. return TestObj
  78. return ChildState
  79. @pytest.fixture
  80. def GrandChildState(ChildState, TestObj):
  81. class GrandChildState(ChildState):
  82. @computed_var
  83. def var_without_annotation(self):
  84. """This shadows ChildState.var_without_annotation.
  85. Returns:
  86. TestObj: Test object.
  87. """
  88. return TestObj
  89. return GrandChildState
  90. @pytest.fixture
  91. def StateWithAnyVar(TestObj):
  92. class StateWithAnyVar(BaseState):
  93. @computed_var
  94. def var_without_annotation(self) -> typing.Any:
  95. return TestObj
  96. return StateWithAnyVar
  97. @pytest.fixture
  98. def StateWithCorrectVarAnnotation():
  99. class StateWithCorrectVarAnnotation(BaseState):
  100. @computed_var
  101. def var_with_annotation(self) -> str:
  102. return "Correct annotation"
  103. return StateWithCorrectVarAnnotation
  104. @pytest.fixture
  105. def StateWithWrongVarAnnotation(TestObj):
  106. class StateWithWrongVarAnnotation(BaseState):
  107. @computed_var
  108. def var_with_annotation(self) -> str:
  109. return TestObj
  110. return StateWithWrongVarAnnotation
  111. @pytest.fixture
  112. def StateWithInitialComputedVar():
  113. class StateWithInitialComputedVar(BaseState):
  114. @computed_var(initial_value="Initial value")
  115. def var_with_initial_value(self) -> str:
  116. return "Runtime value"
  117. return StateWithInitialComputedVar
  118. @pytest.fixture
  119. def ChildWithInitialComputedVar(StateWithInitialComputedVar):
  120. class ChildWithInitialComputedVar(StateWithInitialComputedVar):
  121. @computed_var(initial_value="Initial value")
  122. def var_with_initial_value_child(self) -> str:
  123. return "Runtime value"
  124. return ChildWithInitialComputedVar
  125. @pytest.fixture
  126. def StateWithRuntimeOnlyVar():
  127. class StateWithRuntimeOnlyVar(BaseState):
  128. @computed_var(initial_value=None)
  129. def var_raises_at_runtime(self) -> str:
  130. raise ValueError("So nicht, mein Freund")
  131. return StateWithRuntimeOnlyVar
  132. @pytest.fixture
  133. def ChildWithRuntimeOnlyVar(StateWithRuntimeOnlyVar):
  134. class ChildWithRuntimeOnlyVar(StateWithRuntimeOnlyVar):
  135. @computed_var(initial_value="Initial value")
  136. def var_raises_at_runtime_child(self) -> str:
  137. raise ValueError("So nicht, mein Freund")
  138. return ChildWithRuntimeOnlyVar
  139. @pytest.mark.parametrize(
  140. "prop,expected",
  141. zip(
  142. test_vars,
  143. [
  144. "prop1",
  145. "key",
  146. "state.value",
  147. "state.local",
  148. "local2",
  149. ],
  150. strict=True,
  151. ),
  152. )
  153. def test_full_name(prop, expected):
  154. """Test that the full name of a var is correct.
  155. Args:
  156. prop: The var to test.
  157. expected: The expected full name.
  158. """
  159. assert str(prop) == expected
  160. @pytest.mark.parametrize(
  161. "prop,expected",
  162. zip(
  163. test_vars,
  164. ["prop1", "key", "state.value", "state.local", "local2"],
  165. strict=True,
  166. ),
  167. )
  168. def test_str(prop, expected):
  169. """Test that the string representation of a var is correct.
  170. Args:
  171. prop: The var to test.
  172. expected: The expected string representation.
  173. """
  174. assert str(prop) == expected
  175. @pytest.mark.parametrize(
  176. ("prop", "expected"),
  177. [
  178. (Var(_js_expr="p", _var_type=int), 0),
  179. (Var(_js_expr="p", _var_type=float), 0.0),
  180. (Var(_js_expr="p", _var_type=str), ""),
  181. (Var(_js_expr="p", _var_type=bool), False),
  182. (Var(_js_expr="p", _var_type=list), []),
  183. (Var(_js_expr="p", _var_type=dict), {}),
  184. (Var(_js_expr="p", _var_type=tuple), ()),
  185. (Var(_js_expr="p", _var_type=set), set()),
  186. ],
  187. )
  188. def test_default_value(prop: Var, expected):
  189. """Test that the default value of a var is correct.
  190. Args:
  191. prop: The var to test.
  192. expected: The expected default value.
  193. """
  194. assert prop._get_default_value() == expected
  195. @pytest.mark.parametrize(
  196. "prop,expected",
  197. zip(
  198. test_vars,
  199. [
  200. "set_prop1",
  201. "set_key",
  202. "state.set_value",
  203. "state.set_local",
  204. "set_local2",
  205. ],
  206. strict=True,
  207. ),
  208. )
  209. def test_get_setter(prop: Var, expected):
  210. """Test that the name of the setter function of a var is correct.
  211. Args:
  212. prop: The var to test.
  213. expected: The expected name of the setter function.
  214. """
  215. assert prop._get_setter_name() == expected
  216. @pytest.mark.parametrize(
  217. "value,expected",
  218. [
  219. (None, Var(_js_expr="null", _var_type=None)),
  220. (1, Var(_js_expr="1", _var_type=int)),
  221. ("key", Var(_js_expr='"key"', _var_type=str)),
  222. (3.14, Var(_js_expr="3.14", _var_type=float)),
  223. ([1, 2, 3], Var(_js_expr="[1, 2, 3]", _var_type=Sequence[int])),
  224. (
  225. {"a": 1, "b": 2},
  226. Var(_js_expr='({ ["a"] : 1, ["b"] : 2 })', _var_type=Mapping[str, int]),
  227. ),
  228. ],
  229. )
  230. def test_create(value, expected):
  231. """Test the var create function.
  232. Args:
  233. value: The value to create a var from.
  234. expected: The expected name of the setter function.
  235. """
  236. prop = LiteralVar.create(value)
  237. assert prop.equals(expected)
  238. def test_create_type_error():
  239. """Test the var create function when inputs type error."""
  240. class ErrorType:
  241. pass
  242. value = ErrorType()
  243. with pytest.raises(TypeError):
  244. LiteralVar.create(value)
  245. def v(value) -> Var:
  246. return LiteralVar.create(value)
  247. def test_basic_operations(TestObj):
  248. """Test the var operations.
  249. Args:
  250. TestObj: The test object.
  251. """
  252. assert str(v(1) == v(2)) == "(1 === 2)"
  253. assert str(v(1) != v(2)) == "(1 !== 2)"
  254. assert str(LiteralNumberVar.create(1) < 2) == "(1 < 2)"
  255. assert str(LiteralNumberVar.create(1) <= 2) == "(1 <= 2)"
  256. assert str(LiteralNumberVar.create(1) > 2) == "(1 > 2)"
  257. assert str(LiteralNumberVar.create(1) >= 2) == "(1 >= 2)"
  258. assert str(LiteralNumberVar.create(1) + 2) == "(1 + 2)"
  259. assert str(LiteralNumberVar.create(1) - 2) == "(1 - 2)"
  260. assert str(LiteralNumberVar.create(1) * 2) == "(1 * 2)"
  261. assert str(LiteralNumberVar.create(1) / 2) == "(1 / 2)"
  262. assert str(LiteralNumberVar.create(1) // 2) == "Math.floor(1 / 2)"
  263. assert str(LiteralNumberVar.create(1) % 2) == "(1 % 2)"
  264. assert str(LiteralNumberVar.create(1) ** 2) == "(1 ** 2)"
  265. assert str(LiteralNumberVar.create(1) & v(2)) == "(1 && 2)"
  266. assert str(LiteralNumberVar.create(1) | v(2)) == "(1 || 2)"
  267. assert str(LiteralArrayVar.create([1, 2, 3])[0]) == "[1, 2, 3].at(0)"
  268. assert (
  269. str(LiteralObjectVar.create({"a": 1, "b": 2})["a"])
  270. == '({ ["a"] : 1, ["b"] : 2 })["a"]'
  271. )
  272. assert str(v("foo") == v("bar")) == '("foo" === "bar")'
  273. assert str(Var(_js_expr="foo") == Var(_js_expr="bar")) == "(foo === bar)"
  274. assert (
  275. str(LiteralVar.create("foo") == LiteralVar.create("bar")) == '("foo" === "bar")'
  276. )
  277. print(Var(_js_expr="foo").to(ObjectVar, TestObj)._var_set_state("state"))
  278. assert (
  279. str(
  280. Var(_js_expr="foo").to(ObjectVar, TestObj)._var_set_state("state").bar
  281. == LiteralVar.create("bar")
  282. )
  283. == '(state.foo["bar"] === "bar")'
  284. )
  285. assert (
  286. str(Var(_js_expr="foo").to(ObjectVar, TestObj)._var_set_state("state").bar)
  287. == 'state.foo["bar"]'
  288. )
  289. assert str(abs(LiteralNumberVar.create(1))) == "Math.abs(1)"
  290. assert str(LiteralArrayVar.create([1, 2, 3]).length()) == "[1, 2, 3].length"
  291. assert (
  292. str(LiteralArrayVar.create([1, 2]) + LiteralArrayVar.create([3, 4]))
  293. == "[...[1, 2], ...[3, 4]]"
  294. )
  295. # Tests for reverse operation
  296. assert (
  297. str(LiteralArrayVar.create([1, 2, 3]).reverse())
  298. == "[1, 2, 3].slice().reverse()"
  299. )
  300. assert (
  301. str(LiteralArrayVar.create(["1", "2", "3"]).reverse())
  302. == '["1", "2", "3"].slice().reverse()'
  303. )
  304. assert (
  305. str(Var(_js_expr="foo")._var_set_state("state").to(list).reverse())
  306. == "state.foo.slice().reverse()"
  307. )
  308. assert str(Var(_js_expr="foo").to(list).reverse()) == "foo.slice().reverse()"
  309. assert str(Var(_js_expr="foo", _var_type=str).js_type()) == "(typeof(foo))"
  310. @pytest.mark.parametrize(
  311. "var, expected",
  312. [
  313. (v([1, 2, 3]), "[1, 2, 3]"),
  314. (v({1, 2, 3}), "[1, 2, 3]"),
  315. (v(["1", "2", "3"]), '["1", "2", "3"]'),
  316. (
  317. Var(_js_expr="foo")._var_set_state("state").to(list),
  318. "state.foo",
  319. ),
  320. (Var(_js_expr="foo").to(list), "foo"),
  321. (v((1, 2, 3)), "[1, 2, 3]"),
  322. (v(("1", "2", "3")), '["1", "2", "3"]'),
  323. (
  324. Var(_js_expr="foo")._var_set_state("state").to(tuple),
  325. "state.foo",
  326. ),
  327. (Var(_js_expr="foo").to(tuple), "foo"),
  328. ],
  329. )
  330. def test_list_tuple_contains(var, expected):
  331. assert str(var.contains(1)) == f"{expected}.includes(1)"
  332. assert str(var.contains("1")) == f'{expected}.includes("1")'
  333. assert str(var.contains(v(1))) == f"{expected}.includes(1)"
  334. assert str(var.contains(v("1"))) == f'{expected}.includes("1")'
  335. other_state_var = Var(_js_expr="other", _var_type=str)._var_set_state("state")
  336. other_var = Var(_js_expr="other", _var_type=str)
  337. assert str(var.contains(other_state_var)) == f"{expected}.includes(state.other)"
  338. assert str(var.contains(other_var)) == f"{expected}.includes(other)"
  339. class Foo(rx.Base):
  340. """Foo class."""
  341. bar: int
  342. baz: str
  343. class Bar(rx.Base):
  344. """Bar class."""
  345. bar: str
  346. baz: str
  347. foo: int
  348. @pytest.mark.parametrize(
  349. ("var", "var_type"),
  350. [
  351. (Var(_js_expr="").to(Foo | Bar), Foo | Bar),
  352. (Var(_js_expr="").to(Foo | Bar).bar, int | str),
  353. (Var(_js_expr="").to(Foo | Bar), Foo | Bar),
  354. (Var(_js_expr="").to(Foo | Bar).baz, str),
  355. (
  356. Var(_js_expr="").to(Foo | Bar).foo,
  357. int | None,
  358. ),
  359. ],
  360. )
  361. def test_var_types(var, var_type):
  362. assert var._var_type == var_type
  363. @pytest.mark.parametrize(
  364. "var, expected",
  365. [
  366. (v("123"), json.dumps("123")),
  367. (Var(_js_expr="foo")._var_set_state("state").to(str), "state.foo"),
  368. (Var(_js_expr="foo").to(str), "foo"),
  369. ],
  370. )
  371. def test_str_contains(var, expected):
  372. assert str(var.contains("1")) == f'{expected}.includes("1")'
  373. assert str(var.contains(v("1"))) == f'{expected}.includes("1")'
  374. other_state_var = Var(_js_expr="other")._var_set_state("state").to(str)
  375. other_var = Var(_js_expr="other").to(str)
  376. assert str(var.contains(other_state_var)) == f"{expected}.includes(state.other)"
  377. assert str(var.contains(other_var)) == f"{expected}.includes(other)"
  378. assert (
  379. str(var.contains("1", "hello"))
  380. == f'{expected}.some(obj => obj["hello"] === "1")'
  381. )
  382. @pytest.mark.parametrize(
  383. "var, expected",
  384. [
  385. (v({"a": 1, "b": 2}), '({ ["a"] : 1, ["b"] : 2 })'),
  386. (Var(_js_expr="foo")._var_set_state("state").to(dict), "state.foo"),
  387. (Var(_js_expr="foo").to(dict), "foo"),
  388. ],
  389. )
  390. def test_dict_contains(var, expected):
  391. assert str(var.contains(1)) == f"{expected}.hasOwnProperty(1)"
  392. assert str(var.contains("1")) == f'{expected}.hasOwnProperty("1")'
  393. assert str(var.contains(v(1))) == f"{expected}.hasOwnProperty(1)"
  394. assert str(var.contains(v("1"))) == f'{expected}.hasOwnProperty("1")'
  395. other_state_var = Var(_js_expr="other")._var_set_state("state").to(str)
  396. other_var = Var(_js_expr="other").to(str)
  397. assert (
  398. str(var.contains(other_state_var)) == f"{expected}.hasOwnProperty(state.other)"
  399. )
  400. assert str(var.contains(other_var)) == f"{expected}.hasOwnProperty(other)"
  401. @pytest.mark.parametrize(
  402. "var",
  403. [
  404. Var(_js_expr="list", _var_type=list[int]).guess_type(),
  405. Var(_js_expr="tuple", _var_type=tuple[int, int]).guess_type(),
  406. Var(_js_expr="str", _var_type=str).guess_type(),
  407. ],
  408. )
  409. def test_var_indexing_lists(var):
  410. """Test that we can index into str, list or tuple vars.
  411. Args:
  412. var : The str, list or tuple base var.
  413. """
  414. # Test basic indexing.
  415. assert str(var[0]) == f"{var._js_expr}.at(0)"
  416. assert str(var[1]) == f"{var._js_expr}.at(1)"
  417. # Test negative indexing.
  418. assert str(var[-1]) == f"{var._js_expr}.at(-1)"
  419. @pytest.mark.parametrize(
  420. "var, type_",
  421. [
  422. (Var(_js_expr="list", _var_type=list[int]).guess_type(), [int, int]),
  423. (
  424. Var(_js_expr="tuple", _var_type=tuple[int, str]).guess_type(),
  425. [int, str],
  426. ),
  427. (Var.create((1, 2)), [int, int]),
  428. ],
  429. )
  430. def test_var_indexing_types(var, type_):
  431. """Test that indexing returns valid types.
  432. Args:
  433. var : The list, tuple base var.
  434. type_ : The type on indexed object.
  435. """
  436. assert var[0]._var_type == type_[0]
  437. assert var[1]._var_type == type_[1]
  438. def test_var_indexing_str():
  439. """Test that we can index into str vars."""
  440. str_var = Var(_js_expr="str").to(str)
  441. # Test that indexing gives a type of Var[str].
  442. assert isinstance(str_var[0], Var)
  443. assert str_var[0]._var_type is str
  444. # Test basic indexing.
  445. assert str(str_var[0]) == "str.at(0)"
  446. assert str(str_var[1]) == "str.at(1)"
  447. # Test negative indexing.
  448. assert str(str_var[-1]) == "str.at(-1)"
  449. @pytest.mark.parametrize(
  450. "var",
  451. [
  452. (Var(_js_expr="foo", _var_type=int).guess_type()),
  453. (Var(_js_expr="bar", _var_type=float).guess_type()),
  454. ],
  455. )
  456. def test_var_replace_with_invalid_kwargs(var):
  457. with pytest.raises(TypeError) as excinfo:
  458. var._replace(_this_should_fail=True)
  459. assert "unexpected keyword argument" in str(excinfo.value)
  460. def test_computed_var_replace_with_invalid_kwargs():
  461. @computed_var(initial_value=1)
  462. def test_var(state) -> int:
  463. return 1
  464. with pytest.raises(TypeError) as excinfo:
  465. test_var._replace(_random_kwarg=True)
  466. assert "Unexpected keyword argument" in str(excinfo.value)
  467. @pytest.mark.parametrize(
  468. "var, index",
  469. [
  470. (Var(_js_expr="lst", _var_type=list[int]).guess_type(), [1, 2]),
  471. (
  472. Var(_js_expr="lst", _var_type=list[int]).guess_type(),
  473. {"name": "dict"},
  474. ),
  475. (Var(_js_expr="lst", _var_type=list[int]).guess_type(), {"set"}),
  476. (
  477. Var(_js_expr="lst", _var_type=list[int]).guess_type(),
  478. (
  479. 1,
  480. 2,
  481. ),
  482. ),
  483. (Var(_js_expr="lst", _var_type=list[int]).guess_type(), 1.5),
  484. (Var(_js_expr="lst", _var_type=list[int]).guess_type(), "str"),
  485. (
  486. Var(_js_expr="lst", _var_type=list[int]).guess_type(),
  487. Var(_js_expr="string_var", _var_type=str).guess_type(),
  488. ),
  489. (
  490. Var(_js_expr="lst", _var_type=list[int]).guess_type(),
  491. Var(_js_expr="float_var", _var_type=float).guess_type(),
  492. ),
  493. (
  494. Var(_js_expr="lst", _var_type=list[int]).guess_type(),
  495. Var(_js_expr="list_var", _var_type=list[int]).guess_type(),
  496. ),
  497. (
  498. Var(_js_expr="lst", _var_type=list[int]).guess_type(),
  499. Var(_js_expr="set_var", _var_type=set[str]).guess_type(),
  500. ),
  501. (
  502. Var(_js_expr="lst", _var_type=list[int]).guess_type(),
  503. Var(_js_expr="dict_var", _var_type=dict[str, str]).guess_type(),
  504. ),
  505. (Var(_js_expr="str", _var_type=str).guess_type(), [1, 2]),
  506. (Var(_js_expr="lst", _var_type=str).guess_type(), {"name": "dict"}),
  507. (Var(_js_expr="lst", _var_type=str).guess_type(), {"set"}),
  508. (
  509. Var(_js_expr="lst", _var_type=str).guess_type(),
  510. Var(_js_expr="string_var", _var_type=str).guess_type(),
  511. ),
  512. (
  513. Var(_js_expr="lst", _var_type=str).guess_type(),
  514. Var(_js_expr="float_var", _var_type=float).guess_type(),
  515. ),
  516. (Var(_js_expr="str", _var_type=tuple[str]).guess_type(), [1, 2]),
  517. (
  518. Var(_js_expr="lst", _var_type=tuple[str]).guess_type(),
  519. {"name": "dict"},
  520. ),
  521. (Var(_js_expr="lst", _var_type=tuple[str]).guess_type(), {"set"}),
  522. (
  523. Var(_js_expr="lst", _var_type=tuple[str]).guess_type(),
  524. Var(_js_expr="string_var", _var_type=str).guess_type(),
  525. ),
  526. (
  527. Var(_js_expr="lst", _var_type=tuple[str]).guess_type(),
  528. Var(_js_expr="float_var", _var_type=float).guess_type(),
  529. ),
  530. ],
  531. )
  532. def test_var_unsupported_indexing_lists(var, index):
  533. """Test unsupported indexing throws a type error.
  534. Args:
  535. var: The base var.
  536. index: The base var index.
  537. """
  538. with pytest.raises(TypeError):
  539. var[index]
  540. @pytest.mark.parametrize(
  541. "var",
  542. [
  543. Var(_js_expr="lst", _var_type=list[int]).guess_type(),
  544. Var(_js_expr="tuple", _var_type=tuple[int, int]).guess_type(),
  545. ],
  546. )
  547. def test_var_list_slicing(var):
  548. """Test that we can slice into str, list or tuple vars.
  549. Args:
  550. var : The str, list or tuple base var.
  551. """
  552. assert str(var[:1]) == f"{var._js_expr}.slice(undefined, 1)"
  553. assert str(var[1:]) == f"{var._js_expr}.slice(1, undefined)"
  554. assert str(var[:]) == f"{var._js_expr}.slice(undefined, undefined)"
  555. def test_str_var_slicing():
  556. """Test that we can slice into str vars."""
  557. str_var = Var(_js_expr="str").to(str)
  558. # Test that slicing gives a type of Var[str].
  559. assert isinstance(str_var[:1], Var)
  560. assert str_var[:1]._var_type is str
  561. # Test basic slicing.
  562. assert str(str_var[:1]) == 'str.split("").slice(undefined, 1).join("")'
  563. assert str(str_var[1:]) == 'str.split("").slice(1, undefined).join("")'
  564. assert str(str_var[:]) == 'str.split("").slice(undefined, undefined).join("")'
  565. assert str(str_var[1:2]) == 'str.split("").slice(1, 2).join("")'
  566. # Test negative slicing.
  567. assert str(str_var[:-1]) == 'str.split("").slice(undefined, -1).join("")'
  568. assert str(str_var[-1:]) == 'str.split("").slice(-1, undefined).join("")'
  569. assert str(str_var[:-2]) == 'str.split("").slice(undefined, -2).join("")'
  570. assert str(str_var[-2:]) == 'str.split("").slice(-2, undefined).join("")'
  571. def test_dict_indexing():
  572. """Test that we can index into dict vars."""
  573. dct = Var(_js_expr="dct").to(ObjectVar, dict[str, str])
  574. # Check correct indexing.
  575. assert str(dct["a"]) == 'dct["a"]'
  576. assert str(dct["asdf"]) == 'dct["asdf"]'
  577. @pytest.mark.parametrize(
  578. "var, index",
  579. [
  580. (
  581. Var(_js_expr="dict", _var_type=dict[str, str]).guess_type(),
  582. [1, 2],
  583. ),
  584. (
  585. Var(_js_expr="dict", _var_type=dict[str, str]).guess_type(),
  586. {"name": "dict"},
  587. ),
  588. (
  589. Var(_js_expr="dict", _var_type=dict[str, str]).guess_type(),
  590. {"set"},
  591. ),
  592. (
  593. Var(_js_expr="dict", _var_type=dict[str, str]).guess_type(),
  594. (
  595. 1,
  596. 2,
  597. ),
  598. ),
  599. (
  600. Var(_js_expr="lst", _var_type=dict[str, str]).guess_type(),
  601. Var(_js_expr="list_var", _var_type=list[int]).guess_type(),
  602. ),
  603. (
  604. Var(_js_expr="lst", _var_type=dict[str, str]).guess_type(),
  605. Var(_js_expr="set_var", _var_type=set[str]).guess_type(),
  606. ),
  607. (
  608. Var(_js_expr="lst", _var_type=dict[str, str]).guess_type(),
  609. Var(_js_expr="dict_var", _var_type=dict[str, str]).guess_type(),
  610. ),
  611. (
  612. Var(_js_expr="df", _var_type=DataFrame).guess_type(),
  613. [1, 2],
  614. ),
  615. (
  616. Var(_js_expr="df", _var_type=DataFrame).guess_type(),
  617. {"name": "dict"},
  618. ),
  619. (
  620. Var(_js_expr="df", _var_type=DataFrame).guess_type(),
  621. {"set"},
  622. ),
  623. (
  624. Var(_js_expr="df", _var_type=DataFrame).guess_type(),
  625. (
  626. 1,
  627. 2,
  628. ),
  629. ),
  630. (
  631. Var(_js_expr="df", _var_type=DataFrame).guess_type(),
  632. Var(_js_expr="list_var", _var_type=list[int]).guess_type(),
  633. ),
  634. (
  635. Var(_js_expr="df", _var_type=DataFrame).guess_type(),
  636. Var(_js_expr="set_var", _var_type=set[str]).guess_type(),
  637. ),
  638. (
  639. Var(_js_expr="df", _var_type=DataFrame).guess_type(),
  640. Var(_js_expr="dict_var", _var_type=dict[str, str]).guess_type(),
  641. ),
  642. ],
  643. )
  644. def test_var_unsupported_indexing_dicts(var, index):
  645. """Test unsupported indexing throws a type error.
  646. Args:
  647. var: The base var.
  648. index: The base var index.
  649. """
  650. with pytest.raises(TypeError):
  651. var[index]
  652. @pytest.mark.parametrize(
  653. "fixture",
  654. [
  655. "ParentState",
  656. "StateWithAnyVar",
  657. ],
  658. )
  659. def test_computed_var_without_annotation_error(request, fixture):
  660. """Test that a type error is thrown when an attribute of a computed var is
  661. accessed without annotating the computed var.
  662. Args:
  663. request: Fixture Request.
  664. fixture: The state fixture.
  665. """
  666. with pytest.raises(TypeError) as err:
  667. state = request.getfixturevalue(fixture)
  668. state.var_without_annotation.foo
  669. full_name = state.var_without_annotation._var_full_name
  670. assert (
  671. err.value.args[0]
  672. == f"You must provide an annotation for the state var `{full_name}`. Annotation cannot be `typing.Any`"
  673. )
  674. @pytest.mark.parametrize(
  675. "fixture",
  676. [
  677. "ChildState",
  678. "GrandChildState",
  679. ],
  680. )
  681. def test_shadow_computed_var_error(request: pytest.FixtureRequest, fixture: str):
  682. """Test that a name error is thrown when an attribute of a computed var is
  683. shadowed by another attribute.
  684. Args:
  685. request: Fixture Request.
  686. fixture: The state fixture.
  687. """
  688. with pytest.raises(UntypedComputedVarError):
  689. state = request.getfixturevalue(fixture)
  690. state.var_without_annotation.foo
  691. @pytest.mark.parametrize(
  692. "fixture",
  693. [
  694. "StateWithCorrectVarAnnotation",
  695. "StateWithWrongVarAnnotation",
  696. ],
  697. )
  698. def test_computed_var_with_annotation_error(request, fixture):
  699. """Test that an Attribute error is thrown when a non-existent attribute of an annotated computed var is
  700. accessed or when the wrong annotation is provided to a computed var.
  701. Args:
  702. request: Fixture Request.
  703. fixture: The state fixture.
  704. """
  705. with pytest.raises(AttributeError) as err:
  706. state = request.getfixturevalue(fixture)
  707. state.var_with_annotation.foo
  708. full_name = state.var_with_annotation._var_full_name
  709. assert (
  710. err.value.args[0]
  711. == f"The State var `{full_name}` has no attribute 'foo' or may have been annotated wrongly."
  712. )
  713. @pytest.mark.parametrize(
  714. "fixture,var_name,expected_initial,expected_runtime,raises_at_runtime",
  715. [
  716. (
  717. "StateWithInitialComputedVar",
  718. "var_with_initial_value",
  719. "Initial value",
  720. "Runtime value",
  721. False,
  722. ),
  723. (
  724. "ChildWithInitialComputedVar",
  725. "var_with_initial_value_child",
  726. "Initial value",
  727. "Runtime value",
  728. False,
  729. ),
  730. (
  731. "StateWithRuntimeOnlyVar",
  732. "var_raises_at_runtime",
  733. None,
  734. None,
  735. True,
  736. ),
  737. (
  738. "ChildWithRuntimeOnlyVar",
  739. "var_raises_at_runtime_child",
  740. "Initial value",
  741. None,
  742. True,
  743. ),
  744. ],
  745. )
  746. def test_state_with_initial_computed_var(
  747. request, fixture, var_name, expected_initial, expected_runtime, raises_at_runtime
  748. ):
  749. """Test that the initial and runtime values of a computed var are correct.
  750. Args:
  751. request: Fixture Request.
  752. fixture: The state fixture.
  753. var_name: The name of the computed var.
  754. expected_initial: The expected initial value of the computed var.
  755. expected_runtime: The expected runtime value of the computed var.
  756. raises_at_runtime: Whether the computed var is runtime only.
  757. """
  758. state = request.getfixturevalue(fixture)()
  759. state_name = state.get_full_name()
  760. initial_dict = state.dict(initial=True)[state_name]
  761. assert initial_dict[var_name] == expected_initial
  762. if raises_at_runtime:
  763. with pytest.raises(ValueError):
  764. state.dict()[state_name][var_name]
  765. else:
  766. runtime_dict = state.dict()[state_name]
  767. assert runtime_dict[var_name] == expected_runtime
  768. def test_literal_var():
  769. complicated_var = LiteralVar.create(
  770. [
  771. {"a": 1, "b": 2, "c": {"d": 3, "e": 4}},
  772. [1, 2, 3, 4],
  773. 9,
  774. "string",
  775. True,
  776. False,
  777. None,
  778. {1, 2, 3},
  779. ]
  780. )
  781. assert (
  782. str(complicated_var)
  783. == '[({ ["a"] : 1, ["b"] : 2, ["c"] : ({ ["d"] : 3, ["e"] : 4 }) }), [1, 2, 3, 4], 9, "string", true, false, null, [1, 2, 3]]'
  784. )
  785. def test_function_var():
  786. addition_func = FunctionStringVar.create("((a, b) => a + b)")
  787. assert str(addition_func.call(1, 2)) == "(((a, b) => a + b)(1, 2))"
  788. manual_addition_func = ArgsFunctionOperation.create(
  789. ("a", "b"),
  790. {
  791. "args": [Var(_js_expr="a"), Var(_js_expr="b")],
  792. "result": Var(_js_expr="a + b"),
  793. },
  794. )
  795. assert (
  796. str(manual_addition_func.call(1, 2))
  797. == '(((a, b) => ({ ["args"] : [a, b], ["result"] : a + b }))(1, 2))'
  798. )
  799. increment_func = addition_func.partial(1)
  800. assert (
  801. str(increment_func.call(2))
  802. == "(((...args) => (((a, b) => a + b)(1, ...args)))(2))"
  803. )
  804. create_hello_statement = ArgsFunctionOperation.create(
  805. ("name",), f"Hello, {Var(_js_expr='name')}!"
  806. )
  807. first_name = LiteralStringVar.create("Steven")
  808. last_name = LiteralStringVar.create("Universe")
  809. assert (
  810. str(create_hello_statement.call(f"{first_name} {last_name}"))
  811. == '(((name) => ("Hello, "+name+"!"))("Steven Universe"))'
  812. )
  813. # Test with destructured arguments
  814. destructured_func = ArgsFunctionOperation.create(
  815. (DestructuredArg(fields=("a", "b")),),
  816. Var(_js_expr="a + b"),
  817. )
  818. assert (
  819. str(destructured_func.call({"a": 1, "b": 2}))
  820. == '((({a, b}) => a + b)(({ ["a"] : 1, ["b"] : 2 })))'
  821. )
  822. # Test with explicit return
  823. explicit_return_func = ArgsFunctionOperation.create(
  824. ("a", "b"), Var(_js_expr="return a + b"), explicit_return=True
  825. )
  826. assert str(explicit_return_func.call(1, 2)) == "(((a, b) => {return a + b})(1, 2))"
  827. def test_var_operation():
  828. @var_operation
  829. def add(a: NumberVar | int, b: NumberVar | int):
  830. return var_operation_return(js_expression=f"({a} + {b})", var_type=int)
  831. assert str(add(1, 2)) == "(1 + 2)"
  832. assert str(add(a=4, b=-9)) == "(4 + -9)"
  833. five = LiteralNumberVar.create(5)
  834. seven = add(2, five)
  835. assert isinstance(seven, NumberVar)
  836. def test_string_operations():
  837. basic_string = LiteralStringVar.create("Hello, World!")
  838. assert str(basic_string.length()) == '"Hello, World!".split("").length'
  839. assert str(basic_string.lower()) == '"Hello, World!".toLowerCase()'
  840. assert str(basic_string.upper()) == '"Hello, World!".toUpperCase()'
  841. assert str(basic_string.strip()) == '"Hello, World!".trim()'
  842. assert str(basic_string.contains("World")) == '"Hello, World!".includes("World")'
  843. assert (
  844. str(basic_string.split(" ").join(",")) == '"Hello, World!".split(" ").join(",")'
  845. )
  846. def test_all_number_operations():
  847. starting_number = LiteralNumberVar.create(-5.4)
  848. complicated_number = (((-(starting_number + 1)) * 2 / 3) // 2 % 3) ** 2
  849. assert (
  850. str(complicated_number)
  851. == "((Math.floor(((-((-5.4 + 1)) * 2) / 3) / 2) % 3) ** 2)"
  852. )
  853. even_more_complicated_number = ~(
  854. abs(math.floor(complicated_number)) | 2 & 3 & round(complicated_number)
  855. )
  856. assert (
  857. str(even_more_complicated_number)
  858. == "!(isTrue((Math.abs(Math.floor(((Math.floor(((-((-5.4 + 1)) * 2) / 3) / 2) % 3) ** 2))) || (2 && Math.round(((Math.floor(((-((-5.4 + 1)) * 2) / 3) / 2) % 3) ** 2))))))"
  859. )
  860. assert str(LiteralNumberVar.create(5) > False) == "(5 > 0)"
  861. assert str(LiteralBooleanVar.create(False) < 5) == "(Number(false) < 5)"
  862. assert (
  863. str(LiteralBooleanVar.create(False) < LiteralBooleanVar.create(True))
  864. == "(Number(false) < Number(true))"
  865. )
  866. @pytest.mark.parametrize(
  867. ("var", "expected"),
  868. [
  869. (Var.create(False), "false"),
  870. (Var.create(True), "true"),
  871. (Var.create("false"), 'isTrue("false")'),
  872. (Var.create([1, 2, 3]), "isTrue([1, 2, 3])"),
  873. (Var.create({"a": 1, "b": 2}), 'isTrue(({ ["a"] : 1, ["b"] : 2 }))'),
  874. (Var("mysterious_var"), "isTrue(mysterious_var)"),
  875. ],
  876. )
  877. def test_boolify_operations(var, expected):
  878. assert str(var.bool()) == expected
  879. def test_index_operation():
  880. array_var = LiteralArrayVar.create([1, 2, 3, 4, 5])
  881. assert str(array_var[0]) == "[1, 2, 3, 4, 5].at(0)"
  882. assert str(array_var[1:2]) == "[1, 2, 3, 4, 5].slice(1, 2)"
  883. assert (
  884. str(array_var[1:4:2])
  885. == "[1, 2, 3, 4, 5].slice(1, 4).filter((_, i) => i % 2 === 0)"
  886. )
  887. assert (
  888. str(array_var[::-1])
  889. == "[1, 2, 3, 4, 5].slice(0, [1, 2, 3, 4, 5].length).slice().reverse().slice(undefined, undefined).filter((_, i) => i % 1 === 0)"
  890. )
  891. assert str(array_var.reverse()) == "[1, 2, 3, 4, 5].slice().reverse()"
  892. assert str(array_var[0].to(NumberVar) + 9) == "([1, 2, 3, 4, 5].at(0) + 9)"
  893. @pytest.mark.parametrize(
  894. "var, expected_js",
  895. [
  896. (Var.create(float("inf")), "Infinity"),
  897. (Var.create(-float("inf")), "-Infinity"),
  898. (Var.create(float("nan")), "NaN"),
  899. ],
  900. )
  901. def test_inf_and_nan(var, expected_js):
  902. assert str(var) == expected_js
  903. assert isinstance(var, NumberVar)
  904. assert isinstance(var, LiteralVar)
  905. with pytest.raises(PrimitiveUnserializableToJSONError):
  906. var.json()
  907. def test_array_operations():
  908. array_var = LiteralArrayVar.create([1, 2, 3, 4, 5])
  909. assert str(array_var.length()) == "[1, 2, 3, 4, 5].length"
  910. assert str(array_var.contains(3)) == "[1, 2, 3, 4, 5].includes(3)"
  911. assert str(array_var.reverse()) == "[1, 2, 3, 4, 5].slice().reverse()"
  912. assert (
  913. str(ArrayVar.range(10))
  914. == "Array.from({ length: Math.ceil((10 - 0) / 1) }, (_, i) => 0 + i * 1)"
  915. )
  916. assert (
  917. str(ArrayVar.range(1, 10))
  918. == "Array.from({ length: Math.ceil((10 - 1) / 1) }, (_, i) => 1 + i * 1)"
  919. )
  920. assert (
  921. str(ArrayVar.range(1, 10, 2))
  922. == "Array.from({ length: Math.ceil((10 - 1) / 2) }, (_, i) => 1 + i * 2)"
  923. )
  924. assert (
  925. str(ArrayVar.range(1, 10, -1))
  926. == "Array.from({ length: Math.ceil((10 - 1) / -1) }, (_, i) => 1 + i * -1)"
  927. )
  928. def test_object_operations():
  929. object_var = LiteralObjectVar.create({"a": 1, "b": 2, "c": 3})
  930. assert (
  931. str(object_var.keys()) == 'Object.keys(({ ["a"] : 1, ["b"] : 2, ["c"] : 3 }))'
  932. )
  933. assert (
  934. str(object_var.values())
  935. == 'Object.values(({ ["a"] : 1, ["b"] : 2, ["c"] : 3 }))'
  936. )
  937. assert (
  938. str(object_var.entries())
  939. == 'Object.entries(({ ["a"] : 1, ["b"] : 2, ["c"] : 3 }))'
  940. )
  941. assert str(object_var.a) == '({ ["a"] : 1, ["b"] : 2, ["c"] : 3 })["a"]'
  942. assert str(object_var["a"]) == '({ ["a"] : 1, ["b"] : 2, ["c"] : 3 })["a"]'
  943. assert (
  944. str(object_var.merge(LiteralObjectVar.create({"c": 4, "d": 5})))
  945. == '({...({ ["a"] : 1, ["b"] : 2, ["c"] : 3 }), ...({ ["c"] : 4, ["d"] : 5 })})'
  946. )
  947. def test_var_component():
  948. class ComponentVarState(rx.State):
  949. field_var: rx.Field[rx.Component] = rx.field(rx.text("I am a field var"))
  950. @rx.var
  951. def computed_var(self) -> rx.Component:
  952. return rx.text("I am a computed var")
  953. def has_eval_react_component(var: Var):
  954. var_data = var._get_all_var_data()
  955. assert var_data is not None
  956. assert any(
  957. any(
  958. imported_object.name == "evalReactComponent"
  959. for imported_object in imported_objects
  960. )
  961. for _, imported_objects in var_data.imports
  962. )
  963. has_eval_react_component(ComponentVarState.field_var)
  964. has_eval_react_component(ComponentVarState.computed_var)
  965. def test_type_chains():
  966. object_var = LiteralObjectVar.create({"a": 1, "b": 2, "c": 3})
  967. assert (object_var._key_type(), object_var._value_type()) == (str, int)
  968. assert (object_var.keys()._var_type, object_var.values()._var_type) == (
  969. list[str],
  970. list[int],
  971. )
  972. assert (
  973. str(object_var.keys()[0].upper())
  974. == 'Object.keys(({ ["a"] : 1, ["b"] : 2, ["c"] : 3 })).at(0).toUpperCase()'
  975. )
  976. assert (
  977. str(object_var.entries()[1][1] - 1)
  978. == '(Object.entries(({ ["a"] : 1, ["b"] : 2, ["c"] : 3 })).at(1).at(1) - 1)'
  979. )
  980. assert (
  981. str(object_var["c"] + object_var["b"]) # pyright: ignore [reportCallIssue, reportOperatorIssue]
  982. == '(({ ["a"] : 1, ["b"] : 2, ["c"] : 3 })["c"] + ({ ["a"] : 1, ["b"] : 2, ["c"] : 3 })["b"])'
  983. )
  984. def test_nested_dict():
  985. arr = LiteralArrayVar.create([{"bar": ["foo", "bar"]}], list[dict[str, list[str]]])
  986. assert (
  987. str(arr[0]["bar"][0]) == '[({ ["bar"] : ["foo", "bar"] })].at(0)["bar"].at(0)' # pyright: ignore [reportIndexIssue]
  988. )
  989. def nested_base():
  990. class Boo(Base):
  991. foo: str
  992. bar: int
  993. class Foo(Base):
  994. bar: Boo
  995. baz: int
  996. parent_obj = LiteralObjectVar.create(
  997. Foo(bar=Boo(foo="bar", bar=5), baz=5).dict(), Foo
  998. )
  999. assert (
  1000. str(parent_obj.bar.foo)
  1001. == '({ ["bar"] : ({ ["foo"] : "bar", ["bar"] : 5 }), ["baz"] : 5 })["bar"]["foo"]'
  1002. )
  1003. def test_retrival():
  1004. var_without_data = Var(_js_expr="test")
  1005. assert var_without_data is not None
  1006. original_var_data = VarData(
  1007. state="Test",
  1008. imports={"react": [ImportVar(tag="useRef")]},
  1009. hooks={"const state = useContext(StateContexts.state)": None},
  1010. )
  1011. var_with_data = var_without_data._replace(merge_var_data=original_var_data)
  1012. f_string = f"foo{var_with_data}bar"
  1013. assert REFLEX_VAR_OPENING_TAG in f_string
  1014. assert REFLEX_VAR_CLOSING_TAG in f_string
  1015. result_var_data = LiteralVar.create(f_string)._get_all_var_data()
  1016. result_immutable_var_data = Var(_js_expr=f_string)._var_data
  1017. assert result_var_data is not None and result_immutable_var_data is not None
  1018. assert (
  1019. result_var_data.state
  1020. == result_immutable_var_data.state
  1021. == original_var_data.state
  1022. )
  1023. assert (
  1024. result_var_data.imports
  1025. == result_immutable_var_data.imports
  1026. == original_var_data.imports
  1027. )
  1028. assert (
  1029. tuple(result_var_data.hooks)
  1030. == tuple(result_immutable_var_data.hooks)
  1031. == tuple(original_var_data.hooks)
  1032. )
  1033. def test_fstring_concat():
  1034. original_var_with_data = LiteralVar.create(
  1035. "imagination", _var_data=VarData(state="fear")
  1036. )
  1037. immutable_var_with_data = Var(
  1038. _js_expr="consequences",
  1039. _var_data=VarData(
  1040. imports={
  1041. "react": [ImportVar(tag="useRef")],
  1042. "utils": [ImportVar(tag="useEffect")],
  1043. }
  1044. ),
  1045. )
  1046. f_string = f"foo{original_var_with_data}bar{immutable_var_with_data}baz"
  1047. string_concat = LiteralStringVar.create(
  1048. f_string,
  1049. _var_data=VarData(
  1050. hooks={"const state = useContext(StateContexts.state)": None}
  1051. ),
  1052. )
  1053. assert str(string_concat) == '("fooimaginationbar"+consequences+"baz")'
  1054. assert isinstance(string_concat, ConcatVarOperation)
  1055. assert string_concat._get_all_var_data() == VarData(
  1056. state="fear",
  1057. imports={
  1058. "react": [ImportVar(tag="useRef")],
  1059. "utils": [ImportVar(tag="useEffect")],
  1060. },
  1061. hooks={"const state = useContext(StateContexts.state)": None},
  1062. )
  1063. var = Var(_js_expr="var", _var_type=str)
  1064. myvar = Var(_js_expr="myvar", _var_type=int)._var_set_state("state")
  1065. x = Var(_js_expr="x", _var_type=str)
  1066. @pytest.mark.parametrize(
  1067. "out, expected",
  1068. [
  1069. (f"{var}", f"<reflex.Var>{hash(var)}</reflex.Var>var"),
  1070. (
  1071. f"testing f-string with {myvar}",
  1072. f"testing f-string with <reflex.Var>{hash(myvar)}</reflex.Var>state.myvar",
  1073. ),
  1074. (
  1075. f"testing local f-string {x}",
  1076. f"testing local f-string <reflex.Var>{hash(x)}</reflex.Var>x",
  1077. ),
  1078. ],
  1079. )
  1080. def test_fstrings(out, expected):
  1081. assert out == expected
  1082. @pytest.mark.parametrize(
  1083. ("value", "expect_state"),
  1084. [
  1085. ([1], ""),
  1086. ({"a": 1}, ""),
  1087. ([LiteralVar.create(1)._var_set_state("foo")], "foo"),
  1088. ({"a": LiteralVar.create(1)._var_set_state("foo")}, "foo"),
  1089. ],
  1090. )
  1091. def test_extract_state_from_container(value, expect_state):
  1092. """Test that _var_state is extracted from containers containing BaseVar.
  1093. Args:
  1094. value: The value to create a var from.
  1095. expect_state: The expected state.
  1096. """
  1097. var_data = LiteralVar.create(value)._get_all_var_data()
  1098. var_state = var_data.state if var_data else ""
  1099. assert var_state == expect_state
  1100. @pytest.mark.parametrize(
  1101. "value",
  1102. [
  1103. "var",
  1104. "\nvar",
  1105. ],
  1106. )
  1107. def test_fstring_roundtrip(value):
  1108. """Test that f-string roundtrip carries state.
  1109. Args:
  1110. value: The value to create a Var from.
  1111. """
  1112. var = Var(_js_expr=value)._var_set_state("state")
  1113. rt_var = LiteralVar.create(f"{var}")
  1114. assert var._var_state == rt_var._var_state
  1115. assert str(rt_var) == str(var)
  1116. @pytest.mark.parametrize(
  1117. "var",
  1118. [
  1119. Var(_js_expr="var", _var_type=int).guess_type(),
  1120. Var(_js_expr="var", _var_type=float).guess_type(),
  1121. Var(_js_expr="var", _var_type=str).guess_type(),
  1122. Var(_js_expr="var", _var_type=bool).guess_type(),
  1123. Var(_js_expr="var", _var_type=None).guess_type(),
  1124. ],
  1125. )
  1126. def test_unsupported_types_for_reverse(var):
  1127. """Test that unsupported types for reverse throw a type error.
  1128. Args:
  1129. var: The base var.
  1130. """
  1131. with pytest.raises(TypeError) as err:
  1132. var.reverse()
  1133. assert err.value.args[0] == "Cannot reverse non-list var."
  1134. @pytest.mark.parametrize(
  1135. "var",
  1136. [
  1137. Var(_js_expr="var", _var_type=int).guess_type(),
  1138. Var(_js_expr="var", _var_type=float).guess_type(),
  1139. Var(_js_expr="var", _var_type=bool).guess_type(),
  1140. Var(_js_expr="var", _var_type=type(None)).guess_type(),
  1141. ],
  1142. )
  1143. def test_unsupported_types_for_contains(var: Var):
  1144. """Test that unsupported types for contains throw a type error.
  1145. Args:
  1146. var: The base var.
  1147. """
  1148. with pytest.raises(TypeError) as err:
  1149. assert var.contains(1) # pyright: ignore [reportAttributeAccessIssue]
  1150. assert (
  1151. err.value.args[0]
  1152. == f"Var of type {var._var_type} does not support contains check."
  1153. )
  1154. @pytest.mark.parametrize(
  1155. "other",
  1156. [
  1157. Var(_js_expr="other", _var_type=int).guess_type(),
  1158. Var(_js_expr="other", _var_type=float).guess_type(),
  1159. Var(_js_expr="other", _var_type=bool).guess_type(),
  1160. Var(_js_expr="other", _var_type=list).guess_type(),
  1161. Var(_js_expr="other", _var_type=dict).guess_type(),
  1162. Var(_js_expr="other", _var_type=tuple).guess_type(),
  1163. Var(_js_expr="other", _var_type=set).guess_type(),
  1164. ],
  1165. )
  1166. def test_unsupported_types_for_string_contains(other):
  1167. with pytest.raises(TypeError) as err:
  1168. assert Var(_js_expr="var").to(str).contains(other)
  1169. assert (
  1170. err.value.args[0]
  1171. == f"Unsupported Operand type(s) for contains: StringCastedVar, {type(other).__name__}"
  1172. )
  1173. def test_unsupported_default_contains():
  1174. with pytest.raises(TypeError) as err:
  1175. assert 1 in Var(_js_expr="var", _var_type=str).guess_type() # pyright: ignore [reportOperatorIssue]
  1176. assert (
  1177. err.value.args[0]
  1178. == "'in' operator not supported for Var types, use Var.contains() instead."
  1179. )
  1180. @pytest.mark.parametrize(
  1181. "operand1_var,operand2_var,operators",
  1182. [
  1183. (
  1184. LiteralVar.create(10),
  1185. LiteralVar.create(5),
  1186. [
  1187. "+",
  1188. "-",
  1189. "/",
  1190. "//",
  1191. "*",
  1192. "%",
  1193. "**",
  1194. ">",
  1195. "<",
  1196. "<=",
  1197. ">=",
  1198. "|",
  1199. "&",
  1200. ],
  1201. ),
  1202. (
  1203. LiteralVar.create(10.5),
  1204. LiteralVar.create(5),
  1205. ["+", "-", "/", "//", "*", "%", "**", ">", "<", "<=", ">="],
  1206. ),
  1207. (
  1208. LiteralVar.create(5),
  1209. LiteralVar.create(True),
  1210. [
  1211. "+",
  1212. "-",
  1213. "/",
  1214. "//",
  1215. "*",
  1216. "%",
  1217. "**",
  1218. ">",
  1219. "<",
  1220. "<=",
  1221. ">=",
  1222. "|",
  1223. "&",
  1224. ],
  1225. ),
  1226. (
  1227. LiteralVar.create(10.5),
  1228. LiteralVar.create(5.5),
  1229. ["+", "-", "/", "//", "*", "%", "**", ">", "<", "<=", ">="],
  1230. ),
  1231. (
  1232. LiteralVar.create(10.5),
  1233. LiteralVar.create(True),
  1234. ["+", "-", "/", "//", "*", "%", "**", ">", "<", "<=", ">="],
  1235. ),
  1236. (LiteralVar.create("10"), LiteralVar.create("5"), ["+", ">", "<", "<=", ">="]),
  1237. (
  1238. LiteralVar.create([10, 20]),
  1239. LiteralVar.create([5, 6]),
  1240. ["+", ">", "<", "<=", ">="],
  1241. ),
  1242. (LiteralVar.create([10, 20]), LiteralVar.create(5), ["*"]),
  1243. (LiteralVar.create([10, 20]), LiteralVar.create(True), ["*"]),
  1244. (
  1245. LiteralVar.create(True),
  1246. LiteralVar.create(True),
  1247. [
  1248. "+",
  1249. "-",
  1250. "/",
  1251. "//",
  1252. "*",
  1253. "%",
  1254. "**",
  1255. ">",
  1256. "<",
  1257. "<=",
  1258. ">=",
  1259. "|",
  1260. "&",
  1261. ],
  1262. ),
  1263. ],
  1264. )
  1265. def test_valid_var_operations(operand1_var: Var, operand2_var, operators: list[str]):
  1266. """Test that operations do not raise a TypeError.
  1267. Args:
  1268. operand1_var: left operand.
  1269. operand2_var: right operand.
  1270. operators: list of supported operators.
  1271. """
  1272. for operator in operators:
  1273. print(
  1274. "testing",
  1275. operator,
  1276. "on",
  1277. operand1_var,
  1278. operand2_var,
  1279. " of types",
  1280. type(operand1_var),
  1281. type(operand2_var),
  1282. )
  1283. eval(f"operand1_var {operator} operand2_var")
  1284. eval(f"operand2_var {operator} operand1_var")
  1285. @pytest.mark.parametrize(
  1286. "operand1_var,operand2_var,operators",
  1287. [
  1288. (
  1289. LiteralVar.create(10),
  1290. LiteralVar.create(5),
  1291. [
  1292. "^",
  1293. "<<",
  1294. ">>",
  1295. ],
  1296. ),
  1297. (
  1298. LiteralVar.create(10.5),
  1299. LiteralVar.create(5),
  1300. [
  1301. "^",
  1302. "<<",
  1303. ">>",
  1304. ],
  1305. ),
  1306. (
  1307. LiteralVar.create(10.5),
  1308. LiteralVar.create(True),
  1309. [
  1310. "^",
  1311. "<<",
  1312. ">>",
  1313. ],
  1314. ),
  1315. (
  1316. LiteralVar.create(10.5),
  1317. LiteralVar.create(5.5),
  1318. [
  1319. "^",
  1320. "<<",
  1321. ">>",
  1322. ],
  1323. ),
  1324. (
  1325. LiteralVar.create("10"),
  1326. LiteralVar.create("5"),
  1327. [
  1328. "-",
  1329. "/",
  1330. "//",
  1331. "*",
  1332. "%",
  1333. "**",
  1334. "^",
  1335. "<<",
  1336. ">>",
  1337. ],
  1338. ),
  1339. (
  1340. LiteralVar.create([10, 20]),
  1341. LiteralVar.create([5, 6]),
  1342. [
  1343. "-",
  1344. "/",
  1345. "//",
  1346. "*",
  1347. "%",
  1348. "**",
  1349. "^",
  1350. "<<",
  1351. ">>",
  1352. ],
  1353. ),
  1354. (
  1355. LiteralVar.create([10, 20]),
  1356. LiteralVar.create(5),
  1357. [
  1358. "+",
  1359. "-",
  1360. "/",
  1361. "//",
  1362. "%",
  1363. "**",
  1364. ">",
  1365. "<",
  1366. "<=",
  1367. ">=",
  1368. "^",
  1369. "<<",
  1370. ">>",
  1371. ],
  1372. ),
  1373. (
  1374. LiteralVar.create([10, 20]),
  1375. LiteralVar.create(True),
  1376. [
  1377. "+",
  1378. "-",
  1379. "/",
  1380. "//",
  1381. "%",
  1382. "**",
  1383. ">",
  1384. "<",
  1385. "<=",
  1386. ">=",
  1387. "^",
  1388. "<<",
  1389. ">>",
  1390. ],
  1391. ),
  1392. (
  1393. LiteralVar.create([10, 20]),
  1394. LiteralVar.create("5"),
  1395. [
  1396. "+",
  1397. "-",
  1398. "/",
  1399. "//",
  1400. "*",
  1401. "%",
  1402. "**",
  1403. ">",
  1404. "<",
  1405. "<=",
  1406. ">=",
  1407. "^",
  1408. "<<",
  1409. ">>",
  1410. ],
  1411. ),
  1412. (
  1413. LiteralVar.create([10, 20]),
  1414. LiteralVar.create({"key": "value"}),
  1415. [
  1416. "+",
  1417. "-",
  1418. "/",
  1419. "//",
  1420. "*",
  1421. "%",
  1422. "**",
  1423. ">",
  1424. "<",
  1425. "<=",
  1426. ">=",
  1427. "^",
  1428. "<<",
  1429. ">>",
  1430. ],
  1431. ),
  1432. (
  1433. LiteralVar.create([10, 20]),
  1434. LiteralVar.create(5.5),
  1435. [
  1436. "+",
  1437. "-",
  1438. "/",
  1439. "//",
  1440. "*",
  1441. "%",
  1442. "**",
  1443. ">",
  1444. "<",
  1445. "<=",
  1446. ">=",
  1447. "^",
  1448. "<<",
  1449. ">>",
  1450. ],
  1451. ),
  1452. (
  1453. LiteralVar.create({"key": "value"}),
  1454. LiteralVar.create({"another_key": "another_value"}),
  1455. [
  1456. "+",
  1457. "-",
  1458. "/",
  1459. "//",
  1460. "*",
  1461. "%",
  1462. "**",
  1463. ">",
  1464. "<",
  1465. "<=",
  1466. ">=",
  1467. "^",
  1468. "<<",
  1469. ">>",
  1470. ],
  1471. ),
  1472. (
  1473. LiteralVar.create({"key": "value"}),
  1474. LiteralVar.create(5),
  1475. [
  1476. "+",
  1477. "-",
  1478. "/",
  1479. "//",
  1480. "*",
  1481. "%",
  1482. "**",
  1483. ">",
  1484. "<",
  1485. "<=",
  1486. ">=",
  1487. "^",
  1488. "<<",
  1489. ">>",
  1490. ],
  1491. ),
  1492. (
  1493. LiteralVar.create({"key": "value"}),
  1494. LiteralVar.create(True),
  1495. [
  1496. "+",
  1497. "-",
  1498. "/",
  1499. "//",
  1500. "*",
  1501. "%",
  1502. "**",
  1503. ">",
  1504. "<",
  1505. "<=",
  1506. ">=",
  1507. "^",
  1508. "<<",
  1509. ">>",
  1510. ],
  1511. ),
  1512. (
  1513. LiteralVar.create({"key": "value"}),
  1514. LiteralVar.create(5.5),
  1515. [
  1516. "+",
  1517. "-",
  1518. "/",
  1519. "//",
  1520. "*",
  1521. "%",
  1522. "**",
  1523. ">",
  1524. "<",
  1525. "<=",
  1526. ">=",
  1527. "^",
  1528. "<<",
  1529. ">>",
  1530. ],
  1531. ),
  1532. (
  1533. LiteralVar.create({"key": "value"}),
  1534. LiteralVar.create("5"),
  1535. [
  1536. "+",
  1537. "-",
  1538. "/",
  1539. "//",
  1540. "*",
  1541. "%",
  1542. "**",
  1543. ">",
  1544. "<",
  1545. "<=",
  1546. ">=",
  1547. "^",
  1548. "<<",
  1549. ">>",
  1550. ],
  1551. ),
  1552. ],
  1553. )
  1554. def test_invalid_var_operations(operand1_var: Var, operand2_var, operators: list[str]):
  1555. for operator in operators:
  1556. print(f"testing {operator} on {operand1_var!s} and {operand2_var!s}")
  1557. with pytest.raises(TypeError):
  1558. print(eval(f"operand1_var {operator} operand2_var"))
  1559. with pytest.raises(TypeError):
  1560. print(eval(f"operand2_var {operator} operand1_var"))
  1561. @pytest.mark.parametrize(
  1562. "var, expected",
  1563. [
  1564. (LiteralVar.create("string_value"), '"string_value"'),
  1565. (LiteralVar.create(1), "1"),
  1566. (LiteralVar.create([1, 2, 3]), "[1, 2, 3]"),
  1567. (LiteralVar.create({"foo": "bar"}), '({ ["foo"] : "bar" })'),
  1568. (
  1569. LiteralVar.create(ATestState.value),
  1570. f"{ATestState.get_full_name()}.value",
  1571. ),
  1572. (
  1573. LiteralVar.create(f"{ATestState.value} string"),
  1574. f'({ATestState.get_full_name()}.value+" string")',
  1575. ),
  1576. (
  1577. LiteralVar.create(ATestState.dict_val),
  1578. f"{ATestState.get_full_name()}.dict_val",
  1579. ),
  1580. ],
  1581. )
  1582. def test_var_name_unwrapped(var, expected):
  1583. assert str(var) == expected
  1584. def cv_fget(state: BaseState) -> int:
  1585. return 1
  1586. @pytest.mark.parametrize(
  1587. "deps,expected",
  1588. [
  1589. (["a"], {None: {"a"}}),
  1590. (["b"], {None: {"b"}}),
  1591. ([ComputedVar(fget=cv_fget)], {None: {"cv_fget"}}),
  1592. ],
  1593. )
  1594. def test_computed_var_deps(deps: list[str | Var], expected: set[str]):
  1595. @computed_var(deps=deps)
  1596. def test_var(state) -> int:
  1597. return 1
  1598. assert test_var._static_deps == expected
  1599. @pytest.mark.parametrize(
  1600. "deps",
  1601. [
  1602. [""],
  1603. [1],
  1604. ["", "abc"],
  1605. ],
  1606. )
  1607. def test_invalid_computed_var_deps(deps: list):
  1608. with pytest.raises(TypeError):
  1609. @computed_var(deps=deps)
  1610. def test_var(state) -> int:
  1611. return 1
  1612. def test_to_string_operation():
  1613. class Email(str): ...
  1614. class TestState(BaseState):
  1615. optional_email: Email | None = None
  1616. email: Email = Email("test@reflex.dev")
  1617. assert (
  1618. str(TestState.optional_email) == f"{TestState.get_full_name()}.optional_email"
  1619. )
  1620. my_state = TestState()
  1621. assert my_state.optional_email is None
  1622. assert my_state.email == "test@reflex.dev"
  1623. assert cast(Var, TestState.email)._var_type == Email
  1624. assert cast(Var, TestState.optional_email)._var_type == Email | None
  1625. single_var = Var.create(Email())
  1626. assert single_var._var_type == Email
  1627. @pytest.mark.asyncio
  1628. async def test_async_computed_var():
  1629. side_effect_counter = 0
  1630. class AsyncComputedVarState(BaseState):
  1631. v: int = 1
  1632. @computed_var(cache=True)
  1633. async def async_computed_var(self) -> int:
  1634. nonlocal side_effect_counter
  1635. side_effect_counter += 1
  1636. return self.v + 1
  1637. my_state = AsyncComputedVarState()
  1638. assert await my_state.async_computed_var == 2
  1639. assert await my_state.async_computed_var == 2
  1640. my_state.v = 2
  1641. assert await my_state.async_computed_var == 3
  1642. assert await my_state.async_computed_var == 3
  1643. assert side_effect_counter == 2
  1644. def test_var_data_hooks():
  1645. var_data_str = VarData(hooks="what")
  1646. var_data_list = VarData(hooks=["what"])
  1647. var_data_dict = VarData(hooks={"what": None})
  1648. assert var_data_str == var_data_list == var_data_dict
  1649. var_data_list_multiple = VarData(hooks=["what", "whot"])
  1650. var_data_dict_multiple = VarData(hooks={"what": None, "whot": None})
  1651. assert var_data_list_multiple == var_data_dict_multiple
  1652. def test_var_data_with_hooks_value():
  1653. var_data = VarData(hooks={"what": VarData(hooks={"whot": VarData(hooks="whott")})})
  1654. assert var_data == VarData(hooks=["whott", "whot", "what"])
  1655. def test_str_var_in_components(mocker: MockerFixture):
  1656. class StateWithVar(rx.State):
  1657. field: int = 1
  1658. mocker.patch(
  1659. "reflex.components.base.bare.get_performance_mode",
  1660. return_value=PerformanceMode.RAISE,
  1661. )
  1662. with pytest.raises(ValueError):
  1663. rx.vstack(
  1664. str(StateWithVar.field),
  1665. )
  1666. mocker.patch(
  1667. "reflex.components.base.bare.get_performance_mode",
  1668. return_value=PerformanceMode.OFF,
  1669. )
  1670. rx.vstack(
  1671. str(StateWithVar.field),
  1672. )
  1673. def test_decimal_number_operations():
  1674. """Test that decimal.Decimal values work with NumberVar operations."""
  1675. dec_num = Var.create(decimal.Decimal("123.456"))
  1676. assert isinstance(dec_num._var_value, decimal.Decimal)
  1677. assert str(dec_num) == "123.456"
  1678. result = dec_num + 10
  1679. assert str(result) == "(123.456 + 10)"
  1680. result = dec_num * 2
  1681. assert str(result) == "(123.456 * 2)"
  1682. result = dec_num / 2
  1683. assert str(result) == "(123.456 / 2)"
  1684. result = dec_num > 100
  1685. assert str(result) == "(123.456 > 100)"
  1686. result = dec_num < 200
  1687. assert str(result) == "(123.456 < 200)"
  1688. assert dec_num.json() == "123.456"
  1689. def test_decimal_var_type_compatibility():
  1690. """Test that decimal.Decimal values are compatible with NumberVar type system."""
  1691. dec_num = Var.create(decimal.Decimal("123.456"))
  1692. int_num = Var.create(42)
  1693. float_num = Var.create(3.14)
  1694. result = dec_num + int_num
  1695. assert str(result) == "(123.456 + 42)"
  1696. result = dec_num * float_num
  1697. assert str(result) == "(123.456 * 3.14)"
  1698. result = (dec_num + int_num) / float_num
  1699. assert str(result) == "((123.456 + 42) / 3.14)"
  1700. def test_computed_var_type_compatibility():
  1701. """Test that different ComputedVar are compatible with Var annotations of their returned type."""
  1702. class ComputedVarTypeState(BaseState):
  1703. @computed_var
  1704. def sync_plain(self) -> str:
  1705. return "Hello"
  1706. @computed_var(initial_value="Test")
  1707. def sync_wrapper(self) -> str:
  1708. return "World"
  1709. @computed_var
  1710. async def async_plain(self) -> str:
  1711. return "Hello"
  1712. @computed_var(initial_value="Test")
  1713. async def async_wrapper(self) -> str:
  1714. return "World"
  1715. # All of these vars should be assignable to a str field statically.
  1716. rx.input(placeholder=ComputedVarTypeState.sync_plain)
  1717. rx.input(placeholder=ComputedVarTypeState.sync_wrapper)
  1718. rx.input(placeholder=ComputedVarTypeState.async_plain)
  1719. rx.input(placeholder=ComputedVarTypeState.async_wrapper)