diff options
author | Alex Grönholm <alex.gronholm@nextday.fi> | 2021-09-05 23:42:05 +0300 |
---|---|---|
committer | Alex Grönholm <alex.gronholm@nextday.fi> | 2021-09-06 01:39:07 +0300 |
commit | 148b29270eb8fa0974f29be4d85a0ee03b848d1a (patch) | |
tree | 4d874a4bb230151eedaed19ddf14af0ec0e7c060 /src/apscheduler/triggers | |
parent | 2a685fe105b6c715c16912109dfc0f982e0acd5c (diff) | |
download | apscheduler-148b29270eb8fa0974f29be4d85a0ee03b848d1a.tar.gz |
Migrated annotations to the py3.10 style
Using "from __future__ import annotations" we can do this even on Python 3.7.
Diffstat (limited to 'src/apscheduler/triggers')
-rw-r--r-- | src/apscheduler/triggers/calendarinterval.py | 10 | ||||
-rw-r--r-- | src/apscheduler/triggers/combining.py | 18 | ||||
-rw-r--r-- | src/apscheduler/triggers/cron/__init__.py | 22 | ||||
-rw-r--r-- | src/apscheduler/triggers/cron/expressions.py | 9 | ||||
-rw-r--r-- | src/apscheduler/triggers/cron/fields.py | 5 | ||||
-rw-r--r-- | src/apscheduler/triggers/date.py | 6 | ||||
-rw-r--r-- | src/apscheduler/triggers/interval.py | 6 |
7 files changed, 44 insertions, 32 deletions
diff --git a/src/apscheduler/triggers/calendarinterval.py b/src/apscheduler/triggers/calendarinterval.py index c6d63da..fe4e9f6 100644 --- a/src/apscheduler/triggers/calendarinterval.py +++ b/src/apscheduler/triggers/calendarinterval.py @@ -1,5 +1,7 @@ +from __future__ import annotations + from datetime import date, datetime, time, timedelta, tzinfo -from typing import Optional, Union +from typing import Optional from ..abc import Trigger from ..marshalling import marshal_date, marshal_timezone, unmarshal_date, unmarshal_timezone @@ -56,9 +58,9 @@ class CalendarIntervalTrigger(Trigger): def __init__(self, *, years: int = 0, months: int = 0, weeks: int = 0, days: int = 0, hour: int = 0, minute: int = 0, second: int = 0, - start_date: Union[date, str, None] = None, - end_date: Union[date, str, None] = None, - timezone: Union[str, tzinfo] = 'local'): + start_date: date | str | None = None, + end_date: date | str | None = None, + timezone: str | tzinfo = 'local'): self.years = years self.months = months self.weeks = weeks diff --git a/src/apscheduler/triggers/combining.py b/src/apscheduler/triggers/combining.py index 04363fd..2bcaa7e 100644 --- a/src/apscheduler/triggers/combining.py +++ b/src/apscheduler/triggers/combining.py @@ -1,6 +1,8 @@ +from __future__ import annotations + from abc import abstractmethod from datetime import datetime, timedelta -from typing import Any, Dict, List, Optional, Sequence, Union +from typing import Any, Optional, Sequence from ..abc import Trigger from ..exceptions import MaxIterationsReached @@ -13,9 +15,9 @@ class BaseCombiningTrigger(Trigger): def __init__(self, triggers: Sequence[Trigger]): self.triggers = as_list(triggers, Trigger, 'triggers') - self._next_fire_times: List[Optional[datetime]] = [] + self._next_fire_times: list[Optional[datetime]] = [] - def __getstate__(self) -> Dict[str, Any]: + def __getstate__(self) -> dict[str, Any]: return { 'version': 1, 'triggers': [marshal_object(trigger) for trigger in self.triggers], @@ -23,7 +25,7 @@ class BaseCombiningTrigger(Trigger): } @abstractmethod - def __setstate__(self, state: Dict[str, Any]) -> None: + def __setstate__(self, state: dict[str, Any]) -> None: self.triggers = [unmarshal_object(*trigger_state) for trigger_state in state['triggers']] self._next_fire_times = state['next_fire_times'] @@ -49,7 +51,7 @@ class AndTrigger(BaseCombiningTrigger): __slots__ = 'threshold', 'max_iterations' - def __init__(self, triggers: Sequence[Trigger], *, threshold: Union[float, timedelta] = 1, + def __init__(self, triggers: Sequence[Trigger], *, threshold: float | timedelta = 1, max_iterations: Optional[int] = 10000): super().__init__(triggers) self.threshold = as_timedelta(threshold, 'threshold') @@ -87,13 +89,13 @@ class AndTrigger(BaseCombiningTrigger): else: raise MaxIterationsReached - def __getstate__(self) -> Dict[str, Any]: + def __getstate__(self) -> dict[str, Any]: state = super().__getstate__() state['threshold'] = self.threshold.total_seconds() state['max_iterations'] = self.max_iterations return state - def __setstate__(self, state: Dict[str, Any]) -> None: + def __setstate__(self, state: dict[str, Any]) -> None: require_state_version(self, state, 1) super().__setstate__(state) self.threshold = timedelta(seconds=state['threshold']) @@ -133,7 +135,7 @@ class OrTrigger(BaseCombiningTrigger): return earliest_time - def __setstate__(self, state: Dict[str, Any]) -> None: + def __setstate__(self, state: dict[str, Any]) -> None: require_state_version(self, state, 1) super().__setstate__(state) diff --git a/src/apscheduler/triggers/cron/__init__.py b/src/apscheduler/triggers/cron/__init__.py index 682cd04..89ac676 100644 --- a/src/apscheduler/triggers/cron/__init__.py +++ b/src/apscheduler/triggers/cron/__init__.py @@ -1,5 +1,7 @@ +from __future__ import annotations + from datetime import datetime, timedelta, tzinfo -from typing import ClassVar, List, Optional, Sequence, Tuple, Union +from typing import ClassVar, List, Optional, Sequence, Tuple from ...abc import Trigger from ...marshalling import marshal_date, marshal_timezone, unmarshal_date, unmarshal_timezone @@ -43,13 +45,13 @@ class CronTrigger(Trigger): ('second', BaseField) ] - def __init__(self, *, year: Union[int, str, None] = None, month: Union[int, str, None] = None, - day: Union[int, str, None] = None, week: Union[int, str, None] = None, - day_of_week: Union[int, str, None] = None, hour: Union[int, str, None] = None, - minute: Union[int, str, None] = None, second: Union[int, str, None] = None, - start_time: Union[datetime, str, None] = None, - end_time: Union[datetime, str, None] = None, - timezone: Union[str, tzinfo, None] = None): + def __init__(self, *, year: int | str | None = None, month: int | str | None = None, + day: int | str | None = None, week: int | str | None = None, + day_of_week: int | str | None = None, hour: int | str | None = None, + minute: int | str | None = None, second: int | str | None = None, + start_time: datetime | str | None = None, + end_time: datetime | str | None = None, + timezone: str | tzinfo | None = None): self.timezone = as_timezone(timezone) self.start_time = (as_aware_datetime(start_time, self.timezone) or datetime.now(self.timezone)) @@ -57,7 +59,7 @@ class CronTrigger(Trigger): self._set_fields([year, month, day, week, day_of_week, hour, minute, second]) self._last_fire_time: Optional[datetime] = None - def _set_fields(self, values: Sequence[Union[int, str, None]]) -> None: + def _set_fields(self, values: Sequence[int | str | None]) -> None: self._fields = [] assigned_values = {field_name: value for (field_name, _), value in zip(self.FIELDS_MAP, values) @@ -71,7 +73,7 @@ class CronTrigger(Trigger): self._fields.append(field) @classmethod - def from_crontab(cls, expr: str, timezone: Union[str, tzinfo] = 'local') -> 'CronTrigger': + def from_crontab(cls, expr: str, timezone: str | tzinfo = 'local') -> 'CronTrigger': """ Create a :class:`~CronTrigger` from a standard crontab expression. diff --git a/src/apscheduler/triggers/cron/expressions.py b/src/apscheduler/triggers/cron/expressions.py index b2d098a..e417dea 100644 --- a/src/apscheduler/triggers/cron/expressions.py +++ b/src/apscheduler/triggers/cron/expressions.py @@ -1,9 +1,10 @@ """This module contains the expressions applicable for CronTrigger's fields.""" +from __future__ import annotations import re from calendar import monthrange from datetime import datetime -from typing import Optional, Union +from typing import Optional from ...validators import as_int @@ -23,7 +24,7 @@ class AllExpression: value_re = re.compile(r'\*(?:/(?P<step>\d+))?$') - def __init__(self, step: Union[str, int, None] = None): + def __init__(self, step: str | int | None = None): self.step = as_int(step) if self.step == 0: raise ValueError('Step must be higher than 0') @@ -57,8 +58,8 @@ class RangeExpression(AllExpression): value_re = re.compile(r'(?P<first>\d+)(?:-(?P<last>\d+))?(?:/(?P<step>\d+))?$') - def __init__(self, first: Union[str, int], last: Union[str, int, None] = None, - step: Union[str, int, None] = None): + def __init__(self, first: str | int, last: str | int | None = None, + step: str | int | None = None): super().__init__(step) self.first = as_int(first) self.last = as_int(last) diff --git a/src/apscheduler/triggers/cron/fields.py b/src/apscheduler/triggers/cron/fields.py index 1e19bb9..d15fcd5 100644 --- a/src/apscheduler/triggers/cron/fields.py +++ b/src/apscheduler/triggers/cron/fields.py @@ -1,9 +1,10 @@ """Fields represent CronTrigger options which map to :class:`~datetime.datetime` fields.""" +from __future__ import annotations import re from calendar import monthrange from datetime import datetime -from typing import Any, ClassVar, List, Optional, Sequence, Union +from typing import Any, ClassVar, List, Optional, Sequence from .expressions import ( WEEKDAYS, AllExpression, LastDayOfMonthExpression, MonthRangeExpression, RangeExpression, @@ -29,7 +30,7 @@ class BaseField: if extra_compilers: cls.compilers += extra_compilers - def __init__(self, name: str, exprs: Union[int, str]): + def __init__(self, name: str, exprs: int | str): self.name = name self.expressions: List = [] for expr in SEPARATOR.split(str(exprs).strip()): diff --git a/src/apscheduler/triggers/date.py b/src/apscheduler/triggers/date.py index 636d978..c142ca7 100644 --- a/src/apscheduler/triggers/date.py +++ b/src/apscheduler/triggers/date.py @@ -1,5 +1,7 @@ +from __future__ import annotations + from datetime import datetime, tzinfo -from typing import Optional, Union +from typing import Optional from ..abc import Trigger from ..marshalling import marshal_date, unmarshal_date @@ -17,7 +19,7 @@ class DateTrigger(Trigger): __slots__ = 'run_time', '_completed' - def __init__(self, run_time: datetime, timezone: Union[tzinfo, str] = 'local'): + def __init__(self, run_time: datetime, timezone: tzinfo | str = 'local'): timezone = as_timezone(timezone) self.run_time = as_aware_datetime(run_time, timezone) self._completed = False diff --git a/src/apscheduler/triggers/interval.py b/src/apscheduler/triggers/interval.py index bd5029a..8e933cf 100644 --- a/src/apscheduler/triggers/interval.py +++ b/src/apscheduler/triggers/interval.py @@ -1,5 +1,7 @@ +from __future__ import annotations + from datetime import datetime, timedelta, tzinfo -from typing import Optional, Union +from typing import Optional from ..abc import Trigger from ..marshalling import marshal_date, unmarshal_date @@ -32,7 +34,7 @@ class IntervalTrigger(Trigger): def __init__(self, *, weeks: float = 0, days: float = 0, hours: float = 0, minutes: float = 0, seconds: float = 0, microseconds: float = 0, start_time: Optional[datetime] = None, end_time: Optional[datetime] = None, - timezone: Union[tzinfo, str] = 'local'): + timezone: tzinfo | str = 'local'): self.weeks = weeks self.days = days self.hours = hours |