From 2a02e96d87a43d94f6e05a3299634f08b2fc96f8 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Wed, 15 Jan 2025 10:25:25 -0800 Subject: [PATCH] make the thing compile again --- reflex/vars/datetime.py | 234 ++++++++++++++-------------------------- 1 file changed, 82 insertions(+), 152 deletions(-) diff --git a/reflex/vars/datetime.py b/reflex/vars/datetime.py index b6f4c24c6..740f768e4 100644 --- a/reflex/vars/datetime.py +++ b/reflex/vars/datetime.py @@ -5,10 +5,7 @@ from __future__ import annotations import dataclasses import sys 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 ( CustomVarOperationReturn, @@ -24,156 +21,11 @@ DATETIME_T = TypeVar("DATETIME_T", 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. - - 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_ge_operation(self, other) - - -@var_operation -def date_gt_operation(lhs: Var | Any, rhs: Var | Any) -> CustomVarOperationReturn: - """Greater than comparison. - - Args: - lhs: The left-hand side of the operation. - rhs: The right-hand side of the operation. - - Returns: - The result of the operation. - """ - return date_compare_operation(rhs, lhs, strict=True) - - -@var_operation -def date_lt_operation(lhs: Var | Any, rhs: Var | Any) -> CustomVarOperationReturn: - """Less than comparison. - - Args: - lhs: The left-hand side of the operation. - rhs: The right-hand side of the operation. - - Returns: - The result of the operation. - """ - return date_compare_operation(lhs, rhs, strict=True) - - -@var_operation -def date_le_operation(lhs: Var | Any, rhs: Var | Any) -> CustomVarOperationReturn: - """Less than or equal comparison. - - Args: - lhs: The left-hand side of the operation. - rhs: The right-hand side of the operation. - - Returns: - The result of the operation. - """ - return date_compare_operation(lhs, rhs) - - -@var_operation -def date_ge_operation(lhs: Var | Any, rhs: Var | Any) -> CustomVarOperationReturn: - """Greater than or equal comparison. - - Args: - lhs: The left-hand side of the operation. - rhs: The right-hand side of the operation. - - Returns: - The result of the operation. - """ - return date_compare_operation(rhs, lhs) - - def date_compare_operation( - lhs: DateTimeVar[DATETIME_T] | Any, - rhs: DateTimeVar[DATETIME_T] | Any, + lhs: Var[datetime_types], + rhs: Var[datetime_types], strict: bool = False, -) -> CustomVarOperationReturn: +) -> CustomVarOperationReturn[bool]: """Check if the value is less than the other value. Args: @@ -190,6 +42,84 @@ def date_compare_operation( ) +@var_operation +def date_gt_operation( + lhs: Var[datetime_types], + rhs: Var[datetime_types], +) -> CustomVarOperationReturn: + """Greater than comparison. + + Args: + lhs: The left-hand side of the operation. + rhs: The right-hand side of the operation. + + Returns: + The result of the operation. + """ + return date_compare_operation(rhs, lhs, strict=True) + + +@var_operation +def date_lt_operation( + lhs: Var[datetime_types], + rhs: Var[datetime_types], +) -> CustomVarOperationReturn: + """Less than comparison. + + Args: + lhs: The left-hand side of the operation. + rhs: The right-hand side of the operation. + + Returns: + The result of the operation. + """ + return date_compare_operation(lhs, rhs, strict=True) + + +@var_operation +def date_le_operation( + lhs: Var[datetime_types], rhs: Var[datetime_types] +) -> CustomVarOperationReturn: + """Less than or equal comparison. + + Args: + lhs: The left-hand side of the operation. + rhs: The right-hand side of the operation. + + Returns: + The result of the operation. + """ + return date_compare_operation(lhs, rhs) + + +@var_operation +def date_ge_operation( + lhs: Var[datetime_types], rhs: Var[datetime_types] +) -> CustomVarOperationReturn: + """Greater than or equal comparison. + + Args: + lhs: The left-hand side of the operation. + rhs: The right-hand side of the operation. + + Returns: + The result of the operation. + """ + return date_compare_operation(rhs, lhs) + + +class DateTimeVar(Var[DATETIME_T], python_types=(datetime, date)): + """A variable that holds a datetime or date object.""" + + __lt__ = date_lt_operation + + __le__ = date_le_operation + + __gt__ = date_gt_operation + + __ge__ = date_ge_operation + + @dataclasses.dataclass( eq=False, frozen=True,