12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142 |
- """Immutable number vars."""
- from __future__ import annotations
- import dataclasses
- import json
- import math
- import sys
- from typing import (
- TYPE_CHECKING,
- Any,
- Callable,
- NoReturn,
- Type,
- TypeVar,
- Union,
- overload,
- )
- from reflex.constants.base import Dirs
- from reflex.utils.exceptions import PrimitiveUnserializableToJSON, VarTypeError
- from reflex.utils.imports import ImportDict, ImportVar
- from reflex.utils.types import is_optional
- from .base import (
- CustomVarOperationReturn,
- LiteralVar,
- Var,
- VarData,
- unionize,
- var_operation,
- var_operation_return,
- )
- NUMBER_T = TypeVar("NUMBER_T", int, float, Union[int, float], bool)
- if TYPE_CHECKING:
- from .sequence import ArrayVar
- def raise_unsupported_operand_types(
- operator: str, operands_types: tuple[type, ...]
- ) -> NoReturn:
- """Raise an unsupported operand types error.
- Args:
- operator: The operator.
- operands_types: The types of the operands.
- Raises:
- VarTypeError: The operand types are unsupported.
- """
- raise VarTypeError(
- f"Unsupported Operand type(s) for {operator}: {', '.join(t.__name__ for t in operands_types)}"
- )
- class NumberVar(Var[NUMBER_T], python_types=(int, float)):
- """Base class for immutable number vars."""
- @overload
- def __add__(self, other: number_types) -> NumberVar: ...
- @overload
- def __add__(self, other: NoReturn) -> NoReturn: ...
- def __add__(self, other: Any):
- """Add two numbers.
- Args:
- other: The other number.
- Returns:
- The number addition operation.
- """
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types("+", (type(self), type(other)))
- return number_add_operation(self, +other)
- @overload
- def __radd__(self, other: number_types) -> NumberVar: ...
- @overload
- def __radd__(self, other: NoReturn) -> NoReturn: ...
- def __radd__(self, other: Any):
- """Add two numbers.
- Args:
- other: The other number.
- Returns:
- The number addition operation.
- """
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types("+", (type(other), type(self)))
- return number_add_operation(+other, self)
- @overload
- def __sub__(self, other: number_types) -> NumberVar: ...
- @overload
- def __sub__(self, other: NoReturn) -> NoReturn: ...
- def __sub__(self, other: Any):
- """Subtract two numbers.
- Args:
- other: The other number.
- Returns:
- The number subtraction operation.
- """
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types("-", (type(self), type(other)))
- return number_subtract_operation(self, +other)
- @overload
- def __rsub__(self, other: number_types) -> NumberVar: ...
- @overload
- def __rsub__(self, other: NoReturn) -> NoReturn: ...
- def __rsub__(self, other: Any):
- """Subtract two numbers.
- Args:
- other: The other number.
- Returns:
- The number subtraction operation.
- """
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types("-", (type(other), type(self)))
- return number_subtract_operation(+other, self)
- def __abs__(self):
- """Get the absolute value of the number.
- Returns:
- The number absolute operation.
- """
- return number_abs_operation(self)
- @overload
- def __mul__(self, other: number_types | boolean_types) -> NumberVar: ...
- @overload
- def __mul__(self, other: list | tuple | set | ArrayVar) -> ArrayVar: ...
- def __mul__(self, other: Any):
- """Multiply two numbers.
- Args:
- other: The other number.
- Returns:
- The number multiplication operation.
- """
- from .sequence import ArrayVar, LiteralArrayVar
- if isinstance(other, (list, tuple, set, ArrayVar)):
- if isinstance(other, ArrayVar):
- return other * self
- return LiteralArrayVar.create(other) * self
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types("*", (type(self), type(other)))
- return number_multiply_operation(self, +other)
- @overload
- def __rmul__(self, other: number_types | boolean_types) -> NumberVar: ...
- @overload
- def __rmul__(self, other: list | tuple | set | ArrayVar) -> ArrayVar: ...
- def __rmul__(self, other: Any):
- """Multiply two numbers.
- Args:
- other: The other number.
- Returns:
- The number multiplication operation.
- """
- from .sequence import ArrayVar, LiteralArrayVar
- if isinstance(other, (list, tuple, set, ArrayVar)):
- if isinstance(other, ArrayVar):
- return other * self
- return LiteralArrayVar.create(other) * self
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types("*", (type(other), type(self)))
- return number_multiply_operation(+other, self)
- @overload
- def __truediv__(self, other: number_types) -> NumberVar: ...
- @overload
- def __truediv__(self, other: NoReturn) -> NoReturn: ...
- def __truediv__(self, other: Any):
- """Divide two numbers.
- Args:
- other: The other number.
- Returns:
- The number true division operation.
- """
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types("/", (type(self), type(other)))
- return number_true_division_operation(self, +other)
- @overload
- def __rtruediv__(self, other: number_types) -> NumberVar: ...
- @overload
- def __rtruediv__(self, other: NoReturn) -> NoReturn: ...
- def __rtruediv__(self, other: Any):
- """Divide two numbers.
- Args:
- other: The other number.
- Returns:
- The number true division operation.
- """
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types("/", (type(other), type(self)))
- return number_true_division_operation(+other, self)
- @overload
- def __floordiv__(self, other: number_types) -> NumberVar: ...
- @overload
- def __floordiv__(self, other: NoReturn) -> NoReturn: ...
- def __floordiv__(self, other: Any):
- """Floor divide two numbers.
- Args:
- other: The other number.
- Returns:
- The number floor division operation.
- """
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types("//", (type(self), type(other)))
- return number_floor_division_operation(self, +other)
- @overload
- def __rfloordiv__(self, other: number_types) -> NumberVar: ...
- @overload
- def __rfloordiv__(self, other: NoReturn) -> NoReturn: ...
- def __rfloordiv__(self, other: Any):
- """Floor divide two numbers.
- Args:
- other: The other number.
- Returns:
- The number floor division operation.
- """
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types("//", (type(other), type(self)))
- return number_floor_division_operation(+other, self)
- @overload
- def __mod__(self, other: number_types) -> NumberVar: ...
- @overload
- def __mod__(self, other: NoReturn) -> NoReturn: ...
- def __mod__(self, other: Any):
- """Modulo two numbers.
- Args:
- other: The other number.
- Returns:
- The number modulo operation.
- """
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types("%", (type(self), type(other)))
- return number_modulo_operation(self, +other)
- @overload
- def __rmod__(self, other: number_types) -> NumberVar: ...
- @overload
- def __rmod__(self, other: NoReturn) -> NoReturn: ...
- def __rmod__(self, other: Any):
- """Modulo two numbers.
- Args:
- other: The other number.
- Returns:
- The number modulo operation.
- """
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types("%", (type(other), type(self)))
- return number_modulo_operation(+other, self)
- @overload
- def __pow__(self, other: number_types) -> NumberVar: ...
- @overload
- def __pow__(self, other: NoReturn) -> NoReturn: ...
- def __pow__(self, other: Any):
- """Exponentiate two numbers.
- Args:
- other: The other number.
- Returns:
- The number exponent operation.
- """
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types("**", (type(self), type(other)))
- return number_exponent_operation(self, +other)
- @overload
- def __rpow__(self, other: number_types) -> NumberVar: ...
- @overload
- def __rpow__(self, other: NoReturn) -> NoReturn: ...
- def __rpow__(self, other: Any):
- """Exponentiate two numbers.
- Args:
- other: The other number.
- Returns:
- The number exponent operation.
- """
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types("**", (type(other), type(self)))
- return number_exponent_operation(+other, self)
- def __neg__(self):
- """Negate the number.
- Returns:
- The number negation operation.
- """
- return number_negate_operation(self)
- def __invert__(self):
- """Boolean NOT the number.
- Returns:
- The boolean NOT operation.
- """
- return boolean_not_operation(self.bool())
- def __pos__(self) -> NumberVar:
- """Positive the number.
- Returns:
- The number.
- """
- return self
- def __round__(self):
- """Round the number.
- Returns:
- The number round operation.
- """
- return number_round_operation(self)
- def __ceil__(self):
- """Ceil the number.
- Returns:
- The number ceil operation.
- """
- return number_ceil_operation(self)
- def __floor__(self):
- """Floor the number.
- Returns:
- The number floor operation.
- """
- return number_floor_operation(self)
- def __trunc__(self):
- """Trunc the number.
- Returns:
- The number trunc operation.
- """
- return number_trunc_operation(self)
- @overload
- def __lt__(self, other: number_types) -> BooleanVar: ...
- @overload
- def __lt__(self, other: NoReturn) -> NoReturn: ...
- def __lt__(self, other: Any):
- """Less than comparison.
- Args:
- other: The other number.
- Returns:
- The result of the comparison.
- """
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types("<", (type(self), type(other)))
- return less_than_operation(self, +other)
- @overload
- def __le__(self, other: number_types) -> BooleanVar: ...
- @overload
- def __le__(self, other: NoReturn) -> NoReturn: ...
- def __le__(self, other: Any):
- """Less than or equal comparison.
- Args:
- other: The other number.
- Returns:
- The result of the comparison.
- """
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types("<=", (type(self), type(other)))
- return less_than_or_equal_operation(self, +other)
- def __eq__(self, other: Any):
- """Equal comparison.
- Args:
- other: The other number.
- Returns:
- The result of the comparison.
- """
- if isinstance(other, NUMBER_TYPES):
- return equal_operation(self, +other)
- return equal_operation(self, other)
- def __ne__(self, other: Any):
- """Not equal comparison.
- Args:
- other: The other number.
- Returns:
- The result of the comparison.
- """
- if isinstance(other, NUMBER_TYPES):
- return not_equal_operation(self, +other)
- return not_equal_operation(self, other)
- @overload
- def __gt__(self, other: number_types) -> BooleanVar: ...
- @overload
- def __gt__(self, other: NoReturn) -> NoReturn: ...
- def __gt__(self, other: Any):
- """Greater than comparison.
- Args:
- other: The other number.
- Returns:
- The result of the comparison.
- """
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types(">", (type(self), type(other)))
- return greater_than_operation(self, +other)
- @overload
- def __ge__(self, other: number_types) -> BooleanVar: ...
- @overload
- def __ge__(self, other: NoReturn) -> NoReturn: ...
- def __ge__(self, other: Any):
- """Greater than or equal comparison.
- Args:
- other: The other number.
- Returns:
- The result of the comparison.
- """
- if not isinstance(other, NUMBER_TYPES):
- raise_unsupported_operand_types(">=", (type(self), type(other)))
- return greater_than_or_equal_operation(self, +other)
- def bool(self):
- """Boolean conversion.
- Returns:
- The boolean value of the number.
- """
- if is_optional(self._var_type):
- return boolify((self != None) & (self != 0)) # noqa: E711
- return self != 0
- def _is_strict_float(self) -> bool:
- """Check if the number is a float.
- Returns:
- bool: True if the number is a float.
- """
- return issubclass(self._var_type, float)
- def _is_strict_int(self) -> bool:
- """Check if the number is an int.
- Returns:
- bool: True if the number is an int.
- """
- return issubclass(self._var_type, int)
- def binary_number_operation(
- func: Callable[[NumberVar, NumberVar], str],
- ) -> Callable[[number_types, number_types], NumberVar]:
- """Decorator to create a binary number operation.
- Args:
- func: The binary number operation function.
- Returns:
- The binary number operation.
- """
- @var_operation
- def operation(lhs: NumberVar, rhs: NumberVar):
- return var_operation_return(
- js_expression=func(lhs, rhs),
- var_type=unionize(lhs._var_type, rhs._var_type),
- )
- def wrapper(lhs: number_types, rhs: number_types) -> NumberVar:
- """Create the binary number operation.
- Args:
- lhs: The first number.
- rhs: The second number.
- Returns:
- The binary number operation.
- """
- return operation(lhs, rhs) # type: ignore
- return wrapper
- @binary_number_operation
- def number_add_operation(lhs: NumberVar, rhs: NumberVar):
- """Add two numbers.
- Args:
- lhs: The first number.
- rhs: The second number.
- Returns:
- The number addition operation.
- """
- return f"({lhs} + {rhs})"
- @binary_number_operation
- def number_subtract_operation(lhs: NumberVar, rhs: NumberVar):
- """Subtract two numbers.
- Args:
- lhs: The first number.
- rhs: The second number.
- Returns:
- The number subtraction operation.
- """
- return f"({lhs} - {rhs})"
- @var_operation
- def number_abs_operation(value: NumberVar):
- """Get the absolute value of the number.
- Args:
- value: The number.
- Returns:
- The number absolute operation.
- """
- return var_operation_return(
- js_expression=f"Math.abs({value})", var_type=value._var_type
- )
- @binary_number_operation
- def number_multiply_operation(lhs: NumberVar, rhs: NumberVar):
- """Multiply two numbers.
- Args:
- lhs: The first number.
- rhs: The second number.
- Returns:
- The number multiplication operation.
- """
- return f"({lhs} * {rhs})"
- @var_operation
- def number_negate_operation(
- value: NumberVar[NUMBER_T],
- ) -> CustomVarOperationReturn[NUMBER_T]:
- """Negate the number.
- Args:
- value: The number.
- Returns:
- The number negation operation.
- """
- return var_operation_return(js_expression=f"-({value})", var_type=value._var_type)
- @binary_number_operation
- def number_true_division_operation(lhs: NumberVar, rhs: NumberVar):
- """Divide two numbers.
- Args:
- lhs: The first number.
- rhs: The second number.
- Returns:
- The number true division operation.
- """
- return f"({lhs} / {rhs})"
- @binary_number_operation
- def number_floor_division_operation(lhs: NumberVar, rhs: NumberVar):
- """Floor divide two numbers.
- Args:
- lhs: The first number.
- rhs: The second number.
- Returns:
- The number floor division operation.
- """
- return f"Math.floor({lhs} / {rhs})"
- @binary_number_operation
- def number_modulo_operation(lhs: NumberVar, rhs: NumberVar):
- """Modulo two numbers.
- Args:
- lhs: The first number.
- rhs: The second number.
- Returns:
- The number modulo operation.
- """
- return f"({lhs} % {rhs})"
- @binary_number_operation
- def number_exponent_operation(lhs: NumberVar, rhs: NumberVar):
- """Exponentiate two numbers.
- Args:
- lhs: The first number.
- rhs: The second number.
- Returns:
- The number exponent operation.
- """
- return f"({lhs} ** {rhs})"
- @var_operation
- def number_round_operation(value: NumberVar):
- """Round the number.
- Args:
- value: The number.
- Returns:
- The number round operation.
- """
- return var_operation_return(js_expression=f"Math.round({value})", var_type=int)
- @var_operation
- def number_ceil_operation(value: NumberVar):
- """Ceil the number.
- Args:
- value: The number.
- Returns:
- The number ceil operation.
- """
- return var_operation_return(js_expression=f"Math.ceil({value})", var_type=int)
- @var_operation
- def number_floor_operation(value: NumberVar):
- """Floor the number.
- Args:
- value: The number.
- Returns:
- The number floor operation.
- """
- return var_operation_return(js_expression=f"Math.floor({value})", var_type=int)
- @var_operation
- def number_trunc_operation(value: NumberVar):
- """Trunc the number.
- Args:
- value: The number.
- Returns:
- The number trunc operation.
- """
- return var_operation_return(js_expression=f"Math.trunc({value})", var_type=int)
- class BooleanVar(NumberVar[bool], python_types=bool):
- """Base class for immutable boolean vars."""
- def __invert__(self):
- """NOT the boolean.
- Returns:
- The boolean NOT operation.
- """
- return boolean_not_operation(self)
- def __int__(self):
- """Convert the boolean to an int.
- Returns:
- The boolean to int operation.
- """
- return boolean_to_number_operation(self)
- def __pos__(self):
- """Convert the boolean to an int.
- Returns:
- The boolean to int operation.
- """
- return boolean_to_number_operation(self)
- def bool(self) -> BooleanVar:
- """Boolean conversion.
- Returns:
- The boolean value of the boolean.
- """
- return self
- def __lt__(self, other: Any):
- """Less than comparison.
- Args:
- other: The other boolean.
- Returns:
- The result of the comparison.
- """
- return +self < other
- def __le__(self, other: Any):
- """Less than or equal comparison.
- Args:
- other: The other boolean.
- Returns:
- The result of the comparison.
- """
- return +self <= other
- def __gt__(self, other: Any):
- """Greater than comparison.
- Args:
- other: The other boolean.
- Returns:
- The result of the comparison.
- """
- return +self > other
- def __ge__(self, other: Any):
- """Greater than or equal comparison.
- Args:
- other: The other boolean.
- Returns:
- The result of the comparison.
- """
- return +self >= other
- @var_operation
- def boolean_to_number_operation(value: BooleanVar):
- """Convert the boolean to a number.
- Args:
- value: The boolean.
- Returns:
- The boolean to number operation.
- """
- return var_operation_return(js_expression=f"Number({value})", var_type=int)
- def comparison_operator(
- func: Callable[[Var, Var], str],
- ) -> Callable[[Var | Any, Var | Any], BooleanVar]:
- """Decorator to create a comparison operation.
- Args:
- func: The comparison operation function.
- Returns:
- The comparison operation.
- """
- @var_operation
- def operation(lhs: Var, rhs: Var):
- return var_operation_return(
- js_expression=func(lhs, rhs),
- var_type=bool,
- )
- def wrapper(lhs: Var | Any, rhs: Var | Any) -> BooleanVar:
- """Create the comparison operation.
- Args:
- lhs: The first value.
- rhs: The second value.
- Returns:
- The comparison operation.
- """
- return operation(lhs, rhs)
- return wrapper
- @comparison_operator
- def greater_than_operation(lhs: Var, rhs: Var):
- """Greater than comparison.
- Args:
- lhs: The first value.
- rhs: The second value.
- Returns:
- The result of the comparison.
- """
- return f"({lhs} > {rhs})"
- @comparison_operator
- def greater_than_or_equal_operation(lhs: Var, rhs: Var):
- """Greater than or equal comparison.
- Args:
- lhs: The first value.
- rhs: The second value.
- Returns:
- The result of the comparison.
- """
- return f"({lhs} >= {rhs})"
- @comparison_operator
- def less_than_operation(lhs: Var, rhs: Var):
- """Less than comparison.
- Args:
- lhs: The first value.
- rhs: The second value.
- Returns:
- The result of the comparison.
- """
- return f"({lhs} < {rhs})"
- @comparison_operator
- def less_than_or_equal_operation(lhs: Var, rhs: Var):
- """Less than or equal comparison.
- Args:
- lhs: The first value.
- rhs: The second value.
- Returns:
- The result of the comparison.
- """
- return f"({lhs} <= {rhs})"
- @comparison_operator
- def equal_operation(lhs: Var, rhs: Var):
- """Equal comparison.
- Args:
- lhs: The first value.
- rhs: The second value.
- Returns:
- The result of the comparison.
- """
- return f"({lhs} === {rhs})"
- @comparison_operator
- def not_equal_operation(lhs: Var, rhs: Var):
- """Not equal comparison.
- Args:
- lhs: The first value.
- rhs: The second value.
- Returns:
- The result of the comparison.
- """
- return f"({lhs} !== {rhs})"
- @var_operation
- def boolean_not_operation(value: BooleanVar):
- """Boolean NOT the boolean.
- Args:
- value: The boolean.
- Returns:
- The boolean NOT operation.
- """
- return var_operation_return(js_expression=f"!({value})", var_type=bool)
- @dataclasses.dataclass(
- eq=False,
- frozen=True,
- **{"slots": True} if sys.version_info >= (3, 10) else {},
- )
- class LiteralNumberVar(LiteralVar, NumberVar):
- """Base class for immutable literal number vars."""
- _var_value: float | int = dataclasses.field(default=0)
- def json(self) -> str:
- """Get the JSON representation of the var.
- Returns:
- The JSON representation of the var.
- Raises:
- PrimitiveUnserializableToJSON: If the var is unserializable to JSON.
- """
- if math.isinf(self._var_value) or math.isnan(self._var_value):
- raise PrimitiveUnserializableToJSON(
- f"No valid JSON representation for {self}"
- )
- return json.dumps(self._var_value)
- def __hash__(self) -> int:
- """Calculate the hash value of the object.
- Returns:
- int: The hash value of the object.
- """
- return hash((type(self).__name__, self._var_value))
- @classmethod
- def create(cls, value: float | int, _var_data: VarData | None = None):
- """Create the number var.
- Args:
- value: The value of the var.
- _var_data: Additional hooks and imports associated with the Var.
- Returns:
- The number var.
- """
- if math.isinf(value):
- js_expr = "Infinity" if value > 0 else "-Infinity"
- elif math.isnan(value):
- js_expr = "NaN"
- else:
- js_expr = str(value)
- return cls(
- _js_expr=js_expr,
- _var_type=type(value),
- _var_data=_var_data,
- _var_value=value,
- )
- @dataclasses.dataclass(
- eq=False,
- frozen=True,
- **{"slots": True} if sys.version_info >= (3, 10) else {},
- )
- class LiteralBooleanVar(LiteralVar, BooleanVar):
- """Base class for immutable literal boolean vars."""
- _var_value: bool = dataclasses.field(default=False)
- def json(self) -> str:
- """Get the JSON representation of the var.
- Returns:
- The JSON representation of the var.
- """
- return "true" if self._var_value else "false"
- def __hash__(self) -> int:
- """Calculate the hash value of the object.
- Returns:
- int: The hash value of the object.
- """
- return hash((type(self).__name__, self._var_value))
- @classmethod
- def create(cls, value: bool, _var_data: VarData | None = None):
- """Create the boolean var.
- Args:
- value: The value of the var.
- _var_data: Additional hooks and imports associated with the Var.
- Returns:
- The boolean var.
- """
- return cls(
- _js_expr="true" if value else "false",
- _var_type=bool,
- _var_data=_var_data,
- _var_value=value,
- )
- number_types = Union[NumberVar, int, float]
- boolean_types = Union[BooleanVar, bool]
- _IS_TRUE_IMPORT: ImportDict = {
- f"$/{Dirs.STATE_PATH}": [ImportVar(tag="isTrue")],
- }
- @var_operation
- def boolify(value: Var):
- """Convert the value to a boolean.
- Args:
- value: The value.
- Returns:
- The boolean value.
- """
- return var_operation_return(
- js_expression=f"isTrue({value})",
- var_type=bool,
- var_data=VarData(imports=_IS_TRUE_IMPORT),
- )
- T = TypeVar("T")
- U = TypeVar("U")
- @var_operation
- def ternary_operation(
- condition: BooleanVar, if_true: Var[T], if_false: Var[U]
- ) -> CustomVarOperationReturn[Union[T, U]]:
- """Create a ternary operation.
- Args:
- condition: The condition.
- if_true: The value if the condition is true.
- if_false: The value if the condition is false.
- Returns:
- The ternary operation.
- """
- type_value: Union[Type[T], Type[U]] = unionize(
- if_true._var_type, if_false._var_type
- )
- value: CustomVarOperationReturn[Union[T, U]] = var_operation_return(
- js_expression=f"({condition} ? {if_true} : {if_false})",
- var_type=type_value,
- )
- return value
- NUMBER_TYPES = (int, float, NumberVar)
|