Skip to content

Commit 0070cdb

Browse files
committed
Add some more type hints
Add some more type hints that mypy is still happy about.
1 parent e4249f5 commit 0070cdb

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

src/croniter/croniter.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
import sys
1212
import traceback as _traceback
1313
from time import time
14-
from typing import Optional, Union
14+
from typing import Literal, Optional, Union
1515

1616
from dateutil.relativedelta import relativedelta
1717
from dateutil.tz import tzutc
1818

19+
type ExpandedExpression = list[Union[int, Literal["*", "l"]]]
1920

20-
def is_32bit():
21+
22+
def is_32bit() -> bool:
2123
"""
2224
Detect if Python is running in 32-bit mode.
2325
Returns True if running on 32-bit Python, False for 64-bit.
@@ -292,7 +294,9 @@ def get_current(self, ret_type=None):
292294
return self.timestamp_to_datetime(self.cur)
293295
return self.cur
294296

295-
def set_current(self, start_time, force=True):
297+
def set_current(
298+
self, start_time: Optional[Union[datetime.datetime, float]], force: bool = True
299+
) -> Optional[float]:
296300
if (force or (self.cur is None)) and start_time is not None:
297301
if isinstance(start_time, datetime.datetime):
298302
self.tzinfo = start_time.tzinfo
@@ -304,7 +308,7 @@ def set_current(self, start_time, force=True):
304308
return self.cur
305309

306310
@staticmethod
307-
def datetime_to_timestamp(d):
311+
def datetime_to_timestamp(d: datetime.datetime) -> float:
308312
"""
309313
Converts a `datetime` object `d` into a UNIX timestamp.
310314
"""
@@ -471,7 +475,13 @@ def _calc_next(self, is_prev: bool) -> datetime.datetime:
471475

472476
return self._calc(current, expanded, nth_weekday_of_month, is_prev)
473477

474-
def _calc(self, now, expanded, nth_weekday_of_month, is_prev):
478+
def _calc(
479+
self,
480+
now: datetime.datetime,
481+
expanded: list[ExpandedExpression],
482+
nth_weekday_of_month: dict[int, set[int]],
483+
is_prev: bool,
484+
) -> datetime.datetime:
475485
if is_prev:
476486
nearest_diff_method = self._get_prev_nearest_diff
477487
offset = relativedelta(microseconds=-1)
@@ -757,7 +767,7 @@ def _get_prev_nearest_diff(x, to_check, range_val):
757767
return candidate - x - range_val
758768

759769
@staticmethod
760-
def _get_nth_weekday_of_month(year, month, day_of_week):
770+
def _get_nth_weekday_of_month(year: int, month: int, day_of_week: int) -> tuple[int, ...]:
761771
"""For a given year/month return a list of days in nth-day-of-month order.
762772
The last weekday of the month is always [-1].
763773
"""
@@ -1019,11 +1029,11 @@ def _expand(
10191029
@classmethod
10201030
def expand(
10211031
cls,
1022-
expr_format,
1023-
hash_id=None,
1024-
second_at_beginning=False,
1025-
from_timestamp=None,
1026-
):
1032+
expr_format: str,
1033+
hash_id: Optional[Union[bytes, str]] = None,
1034+
second_at_beginning: bool = False,
1035+
from_timestamp: Optional[float] = None,
1036+
) -> tuple[list[ExpandedExpression], dict[int, set[int]]]:
10271037
"""
10281038
Expand a cron expression format into a noramlized format of
10291039
list[list[int | 'l' | '*']]. The first list representing each element

0 commit comments

Comments
 (0)