Skip to content

Commit

Permalink
fix: more concise type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
constantinius committed Nov 25, 2021
1 parent 484e0b3 commit 87e46a2
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pygeofilter/backends/native/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# THE SOFTWARE.
# ------------------------------------------------------------------------------

from typing import Any, Dict, Callable, Tuple, Union
from typing import Any, Dict, Callable, Optional, Tuple, Union

from datetime import date, time, datetime, timedelta, timezone

Expand Down Expand Up @@ -294,7 +294,7 @@ def adopt_result(self, result):


MaybeInterval = Union[values.Interval, date, datetime, str, None]
InternalInterval = Union[Tuple[datetime, datetime], Tuple[None, None]]
InternalInterval = Tuple[Optional[datetime], Optional[datetime]]


def to_interval(value: MaybeInterval) -> InternalInterval:
Expand Down Expand Up @@ -322,18 +322,24 @@ def to_interval(value: MaybeInterval) -> InternalInterval:
if isinstance(value, values.Interval):
low = value.start
high = value.end

# convert low and high dates to their respective datetime
# by using 00:00 time for the low part and 23:59:59 for the high
# part
if isinstance(low, date):
low = datetime.combine(low, time.min, timezone.utc)
if isinstance(high, date):
high = datetime.combine(high, time.max, timezone.utc)

# low and high are now either datetimes, timedeltas or None

if isinstance(low, timedelta):
if isinstance(high, (date, datetime)):
if isinstance(high, datetime):
low = high - low
else:
raise ValueError(f'Cannot combine {low} with {high}')
elif isinstance(high, timedelta):
if isinstance(high, (date, datetime)):
if isinstance(low, datetime):
high = low + high
else:
raise ValueError(f'Cannot combine {low} with {high}')
Expand Down

0 comments on commit 87e46a2

Please sign in to comment.