Skip to content

Commit

Permalink
make the thing compile again
Browse files Browse the repository at this point in the history
  • Loading branch information
adhami3310 committed Jan 15, 2025
1 parent 056de9e commit 2a02e96
Showing 1 changed file with 40 additions and 110 deletions.
150 changes: 40 additions & 110 deletions reflex/vars/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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(
Expand Down

0 comments on commit 2a02e96

Please sign in to comment.