|
@@ -5,10 +5,7 @@ from __future__ import annotations
|
|
import dataclasses
|
|
import dataclasses
|
|
import sys
|
|
import sys
|
|
from datetime import date, datetime
|
|
from datetime import date, datetime
|
|
-from typing import Any, NoReturn, TypeVar, Union, overload
|
|
|
|
-
|
|
|
|
-from reflex.utils.exceptions import VarTypeError
|
|
|
|
-from reflex.vars.number import BooleanVar
|
|
|
|
|
|
+from typing import TypeVar, Union
|
|
|
|
|
|
from .base import (
|
|
from .base import (
|
|
CustomVarOperationReturn,
|
|
CustomVarOperationReturn,
|
|
@@ -24,97 +21,32 @@ DATETIME_T = TypeVar("DATETIME_T", datetime, date)
|
|
datetime_types = Union[datetime, date]
|
|
datetime_types = Union[datetime, date]
|
|
|
|
|
|
|
|
|
|
-def raise_var_type_error():
|
|
|
|
- """Raise a VarTypeError.
|
|
|
|
-
|
|
|
|
- Raises:
|
|
|
|
- VarTypeError: Cannot compare a datetime object with a non-datetime object.
|
|
|
|
- """
|
|
|
|
- raise VarTypeError("Cannot compare a datetime object with a non-datetime object.")
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-class DateTimeVar(Var[DATETIME_T], python_types=(datetime, date)):
|
|
|
|
- """A variable that holds a datetime or date object."""
|
|
|
|
-
|
|
|
|
- @overload
|
|
|
|
- def __lt__(self, other: datetime_types) -> BooleanVar: ...
|
|
|
|
-
|
|
|
|
- @overload
|
|
|
|
- def __lt__(self, other: NoReturn) -> NoReturn: ...
|
|
|
|
-
|
|
|
|
- def __lt__(self, other: Any):
|
|
|
|
- """Less than comparison.
|
|
|
|
-
|
|
|
|
- Args:
|
|
|
|
- other: The other datetime to compare.
|
|
|
|
-
|
|
|
|
- Returns:
|
|
|
|
- The result of the comparison.
|
|
|
|
- """
|
|
|
|
- if not isinstance(other, DATETIME_TYPES):
|
|
|
|
- raise_var_type_error()
|
|
|
|
- return date_lt_operation(self, other)
|
|
|
|
-
|
|
|
|
- @overload
|
|
|
|
- def __le__(self, other: datetime_types) -> BooleanVar: ...
|
|
|
|
-
|
|
|
|
- @overload
|
|
|
|
- def __le__(self, other: NoReturn) -> NoReturn: ...
|
|
|
|
-
|
|
|
|
- def __le__(self, other: Any):
|
|
|
|
- """Less than or equal comparison.
|
|
|
|
-
|
|
|
|
- Args:
|
|
|
|
- other: The other datetime to compare.
|
|
|
|
-
|
|
|
|
- Returns:
|
|
|
|
- The result of the comparison.
|
|
|
|
- """
|
|
|
|
- if not isinstance(other, DATETIME_TYPES):
|
|
|
|
- raise_var_type_error()
|
|
|
|
- return date_le_operation(self, other)
|
|
|
|
-
|
|
|
|
- @overload
|
|
|
|
- def __gt__(self, other: datetime_types) -> BooleanVar: ...
|
|
|
|
-
|
|
|
|
- @overload
|
|
|
|
- def __gt__(self, other: NoReturn) -> NoReturn: ...
|
|
|
|
-
|
|
|
|
- def __gt__(self, other: Any):
|
|
|
|
- """Greater than comparison.
|
|
|
|
-
|
|
|
|
- Args:
|
|
|
|
- other: The other datetime to compare.
|
|
|
|
-
|
|
|
|
- Returns:
|
|
|
|
- The result of the comparison.
|
|
|
|
- """
|
|
|
|
- if not isinstance(other, DATETIME_TYPES):
|
|
|
|
- raise_var_type_error()
|
|
|
|
- return date_gt_operation(self, other)
|
|
|
|
-
|
|
|
|
- @overload
|
|
|
|
- def __ge__(self, other: datetime_types) -> BooleanVar: ...
|
|
|
|
-
|
|
|
|
- @overload
|
|
|
|
- def __ge__(self, other: NoReturn) -> NoReturn: ...
|
|
|
|
-
|
|
|
|
- def __ge__(self, other: Any):
|
|
|
|
- """Greater than or equal comparison.
|
|
|
|
|
|
+def date_compare_operation(
|
|
|
|
+ lhs: Var[datetime_types],
|
|
|
|
+ rhs: Var[datetime_types],
|
|
|
|
+ strict: bool = False,
|
|
|
|
+) -> CustomVarOperationReturn[bool]:
|
|
|
|
+ """Check if the value is less than the other value.
|
|
|
|
|
|
- Args:
|
|
|
|
- other: The other datetime to compare.
|
|
|
|
|
|
+ Args:
|
|
|
|
+ lhs: The left-hand side of the operation.
|
|
|
|
+ rhs: The right-hand side of the operation.
|
|
|
|
+ strict: Whether to use strict comparison.
|
|
|
|
|
|
- Returns:
|
|
|
|
- The result of the comparison.
|
|
|
|
- """
|
|
|
|
- if not isinstance(other, DATETIME_TYPES):
|
|
|
|
- raise_var_type_error()
|
|
|
|
- return date_ge_operation(self, other)
|
|
|
|
|
|
+ Returns:
|
|
|
|
+ The result of the operation.
|
|
|
|
+ """
|
|
|
|
+ return var_operation_return(
|
|
|
|
+ f"({lhs} { '<' if strict else '<='} {rhs})",
|
|
|
|
+ bool,
|
|
|
|
+ )
|
|
|
|
|
|
|
|
|
|
@var_operation
|
|
@var_operation
|
|
-def date_gt_operation(lhs: Var | Any, rhs: Var | Any) -> CustomVarOperationReturn:
|
|
|
|
|
|
+def date_gt_operation(
|
|
|
|
+ lhs: Var[datetime_types],
|
|
|
|
+ rhs: Var[datetime_types],
|
|
|
|
+) -> CustomVarOperationReturn:
|
|
"""Greater than comparison.
|
|
"""Greater than comparison.
|
|
|
|
|
|
Args:
|
|
Args:
|
|
@@ -128,7 +60,10 @@ def date_gt_operation(lhs: Var | Any, rhs: Var | Any) -> CustomVarOperationRetur
|
|
|
|
|
|
|
|
|
|
@var_operation
|
|
@var_operation
|
|
-def date_lt_operation(lhs: Var | Any, rhs: Var | Any) -> CustomVarOperationReturn:
|
|
|
|
|
|
+def date_lt_operation(
|
|
|
|
+ lhs: Var[datetime_types],
|
|
|
|
+ rhs: Var[datetime_types],
|
|
|
|
+) -> CustomVarOperationReturn:
|
|
"""Less than comparison.
|
|
"""Less than comparison.
|
|
|
|
|
|
Args:
|
|
Args:
|
|
@@ -142,7 +77,9 @@ def date_lt_operation(lhs: Var | Any, rhs: Var | Any) -> CustomVarOperationRetur
|
|
|
|
|
|
|
|
|
|
@var_operation
|
|
@var_operation
|
|
-def date_le_operation(lhs: Var | Any, rhs: Var | Any) -> CustomVarOperationReturn:
|
|
|
|
|
|
+def date_le_operation(
|
|
|
|
+ lhs: Var[datetime_types], rhs: Var[datetime_types]
|
|
|
|
+) -> CustomVarOperationReturn:
|
|
"""Less than or equal comparison.
|
|
"""Less than or equal comparison.
|
|
|
|
|
|
Args:
|
|
Args:
|
|
@@ -156,7 +93,9 @@ def date_le_operation(lhs: Var | Any, rhs: Var | Any) -> CustomVarOperationRetur
|
|
|
|
|
|
|
|
|
|
@var_operation
|
|
@var_operation
|
|
-def date_ge_operation(lhs: Var | Any, rhs: Var | Any) -> CustomVarOperationReturn:
|
|
|
|
|
|
+def date_ge_operation(
|
|
|
|
+ lhs: Var[datetime_types], rhs: Var[datetime_types]
|
|
|
|
+) -> CustomVarOperationReturn:
|
|
"""Greater than or equal comparison.
|
|
"""Greater than or equal comparison.
|
|
|
|
|
|
Args:
|
|
Args:
|
|
@@ -169,25 +108,16 @@ def date_ge_operation(lhs: Var | Any, rhs: Var | Any) -> CustomVarOperationRetur
|
|
return date_compare_operation(rhs, lhs)
|
|
return date_compare_operation(rhs, lhs)
|
|
|
|
|
|
|
|
|
|
-def date_compare_operation(
|
|
|
|
- lhs: DateTimeVar[DATETIME_T] | Any,
|
|
|
|
- rhs: DateTimeVar[DATETIME_T] | Any,
|
|
|
|
- strict: bool = False,
|
|
|
|
-) -> CustomVarOperationReturn:
|
|
|
|
- """Check if the value is less than the other value.
|
|
|
|
|
|
+class DateTimeVar(Var[DATETIME_T], python_types=(datetime, date)):
|
|
|
|
+ """A variable that holds a datetime or date object."""
|
|
|
|
|
|
- Args:
|
|
|
|
- lhs: The left-hand side of the operation.
|
|
|
|
- rhs: The right-hand side of the operation.
|
|
|
|
- strict: Whether to use strict comparison.
|
|
|
|
|
|
+ __lt__ = date_lt_operation
|
|
|
|
|
|
- Returns:
|
|
|
|
- The result of the operation.
|
|
|
|
- """
|
|
|
|
- return var_operation_return(
|
|
|
|
- f"({lhs} { '<' if strict else '<='} {rhs})",
|
|
|
|
- bool,
|
|
|
|
- )
|
|
|
|
|
|
+ __le__ = date_le_operation
|
|
|
|
+
|
|
|
|
+ __gt__ = date_gt_operation
|
|
|
|
+
|
|
|
|
+ __ge__ = date_ge_operation
|
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass(
|
|
@dataclasses.dataclass(
|