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 | 150 +++++++++++----------------------------- 1 file changed, 40 insertions(+), 110 deletions(-) diff --git a/reflex/vars/datetime.py b/reflex/vars/datetime.py index b6f4c24c61..740f768e43 100644 --- a/reflex/vars/datetime.py +++ b/reflex/vars/datetime.py @@ -5,10 +5,7 @@ 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,97 +21,32 @@ 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 -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. Args: @@ -128,7 +60,10 @@ def date_gt_operation(lhs: Var | Any, rhs: Var | Any) -> CustomVarOperationRetur @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. Args: @@ -142,7 +77,9 @@ def date_lt_operation(lhs: Var | Any, rhs: Var | Any) -> CustomVarOperationRetur @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. Args: @@ -156,7 +93,9 @@ def date_le_operation(lhs: Var | Any, rhs: Var | Any) -> CustomVarOperationRetur @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. Args: @@ -169,25 +108,16 @@ def date_ge_operation(lhs: Var | Any, rhs: Var | Any) -> CustomVarOperationRetur 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(