Skip to content

Commit

Permalink
Make compatible with pre-3.10 versions
Browse files Browse the repository at this point in the history
Use Union instead of an or operator
  • Loading branch information
dimitarOnGithub committed Sep 24, 2024
1 parent 9aa917c commit a8a1e3c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
3 changes: 2 additions & 1 deletion temporals/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import time, date, datetime
from typing import Union
from .utils import get_datetime
from .periods import DatePeriod, TimePeriod, DatetimePeriod, Duration

Expand All @@ -19,7 +20,7 @@ class PeriodFactory:
def __new__(cls,
start,
end,
force_datetime: bool = False) -> TimePeriod | DatePeriod | DatetimePeriod:
force_datetime: bool = False) -> Union[TimePeriod, DatePeriod, DatetimePeriod]:
start = get_datetime(start, force_datetime)
end = get_datetime(end, force_datetime)
if type(start) is time and type(end) is time:
Expand Down
10 changes: 5 additions & 5 deletions temporals/periods.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
class Period:

def __init__(self,
start: time | date | datetime,
end: time | date | datetime
start: Union[time, date, datetime],
end: Union[time, date, datetime]
):
if start > end:
raise ValueError('The start of a period cannot be before its end')
Expand Down Expand Up @@ -578,7 +578,7 @@ def __init__(self,
super().__init__(start, end)

def _time_repeats(self,
_t: time | TimePeriod) -> bool:
_t: Union[time, TimePeriod]) -> bool:
""" Internal method that checks if the provided time or TimePeriod will repeat within the duration of this
period.
Expand Down Expand Up @@ -984,8 +984,8 @@ class Duration:
def __init__(self,
*,
period: Period = None,
start: time | date | datetime = None,
end: time | date | datetime = None):
start: Union[time, date, datetime] = None,
end: Union[time, date, datetime] = None):
if period:
if isinstance(period, Period) or issubclass(type(period), Period):
self.period: Period = period
Expand Down
15 changes: 8 additions & 7 deletions temporals/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import time, date, datetime
from itertools import zip_longest
from typing import Union

TIME_PATTERNS = [
"%I:%M%p", # 01:51AM
Expand Down Expand Up @@ -65,7 +66,7 @@


def get_datetime(point_in_time: str,
force_datetime: bool = False) -> time | date | datetime:
force_datetime: bool = False) -> Union[time, date, datetime]:
"""
A helper function used by the PeriodFactory object when either the start or the end of a period is a string.
The goal of this function is to best determine what type the provided string fits (time, date, datetime) and return
Expand Down Expand Up @@ -126,7 +127,7 @@ def get_datetime(point_in_time: str,


def _test_pattern(point_in_time: str,
pattern: str) -> datetime | None:
pattern: str) -> Union[datetime, None]:
"""
Helper method used by the `get_datetime` function above to try and initialize the point_in_time string as a datetime
object by using the provided `pattern` parameter.
Expand All @@ -144,7 +145,7 @@ def _test_pattern(point_in_time: str,
return None


def _test_defaults(point_in_time: str | int | float) -> time | date | datetime | None:
def _test_defaults(point_in_time: Union[str, int, float]) -> Union[time, date, datetime, None]:
"""
Layer to invoke the different datetime constructor functions below.
Expand All @@ -160,15 +161,15 @@ def _test_defaults(point_in_time: str | int | float) -> time | date | datetime |
return resolved_pit


def _test_time(point_in_time: str) -> time | None:
def _test_time(point_in_time: str) -> Union[time, None]:
""" Try to create a datetime.time object """
try:
return time.fromisoformat(point_in_time)
except (ValueError, TypeError):
return None


def _test_date(point_in_time: str | int) -> date | None:
def _test_date(point_in_time: Union[str, int]) -> Union[date, None]:
""" Try to create a datetime.date object """
pit_str: str = _convert_to_type(point_in_time, str)
pit_int: int = _convert_to_type(point_in_time, int)
Expand All @@ -183,7 +184,7 @@ def _test_date(point_in_time: str | int) -> date | None:
return None


def _test_datetime(point_in_time: str | float) -> datetime | None:
def _test_datetime(point_in_time: Union[str, float]) -> Union[datetime, None]:
""" Try to create a datetime.date object """
pit_str: str = _convert_to_type(point_in_time, str)
pit_float: float = _convert_to_type(point_in_time, float)
Expand All @@ -205,7 +206,7 @@ def _convert_to_type(value, target_type):
return None


def convert_to_datetime(value: time | date) -> datetime:
def convert_to_datetime(value: Union[time, date]) -> datetime:
if isinstance(value, datetime):
return value
if isinstance(value, date):
Expand Down

0 comments on commit a8a1e3c

Please sign in to comment.