Skip to content

Commit a584653

Browse files
authored
feat(typing): Make monitor_config a TypedDict (#2931)
1 parent a1ab339 commit a584653

File tree

4 files changed

+71
-23
lines changed

4 files changed

+71
-23
lines changed

sentry_sdk/_types.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,37 @@
178178

179179
BucketKey = Tuple[MetricType, str, MeasurementUnit, MetricTagsInternal]
180180
MetricMetaKey = Tuple[MetricType, str, MeasurementUnit]
181+
182+
MonitorConfigScheduleType = Literal["crontab", "interval"]
183+
MonitorConfigScheduleUnit = Literal[
184+
"year",
185+
"month",
186+
"week",
187+
"day",
188+
"hour",
189+
"minute",
190+
"second", # not supported in Sentry and will result in a warning
191+
]
192+
193+
MonitorConfigSchedule = TypedDict(
194+
"MonitorConfigSchedule",
195+
{
196+
"type": MonitorConfigScheduleType,
197+
"value": Union[int, str],
198+
"unit": MonitorConfigScheduleUnit,
199+
},
200+
total=False,
201+
)
202+
203+
MonitorConfig = TypedDict(
204+
"MonitorConfig",
205+
{
206+
"schedule": MonitorConfigSchedule,
207+
"timezone": str,
208+
"checkin_margin": int,
209+
"max_runtime": int,
210+
"failure_issue_threshold": int,
211+
"recovery_threshold": int,
212+
},
213+
total=False,
214+
)

sentry_sdk/crons/api.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55

66

77
if TYPE_CHECKING:
8-
from typing import Any, Dict, Optional
9-
from sentry_sdk._types import Event
8+
from typing import Optional
9+
from sentry_sdk._types import Event, MonitorConfig
1010

1111

1212
def _create_check_in_event(
13-
monitor_slug=None,
14-
check_in_id=None,
15-
status=None,
16-
duration_s=None,
17-
monitor_config=None,
13+
monitor_slug=None, # type: Optional[str]
14+
check_in_id=None, # type: Optional[str]
15+
status=None, # type: Optional[str]
16+
duration_s=None, # type: Optional[float]
17+
monitor_config=None, # type: Optional[MonitorConfig]
1818
):
19-
# type: (Optional[str], Optional[str], Optional[str], Optional[float], Optional[Dict[str, Any]]) -> Event
19+
# type: (...) -> Event
2020
options = Hub.current.client.options if Hub.current.client else {}
2121
check_in_id = check_in_id or uuid.uuid4().hex # type: str
2222

@@ -37,13 +37,13 @@ def _create_check_in_event(
3737

3838

3939
def capture_checkin(
40-
monitor_slug=None,
41-
check_in_id=None,
42-
status=None,
43-
duration=None,
44-
monitor_config=None,
40+
monitor_slug=None, # type: Optional[str]
41+
check_in_id=None, # type: Optional[str]
42+
status=None, # type: Optional[str]
43+
duration=None, # type: Optional[float]
44+
monitor_config=None, # type: Optional[MonitorConfig]
4545
):
46-
# type: (Optional[str], Optional[str], Optional[str], Optional[float], Optional[Dict[str, Any]]) -> str
46+
# type: (...) -> str
4747
check_in_event = _create_check_in_event(
4848
monitor_slug=monitor_slug,
4949
check_in_id=check_in_id,

sentry_sdk/crons/decorator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
from sentry_sdk.utils import now
66

77
if TYPE_CHECKING:
8-
from typing import Any, Optional, Type
8+
from typing import Optional, Type
99
from types import TracebackType
10+
from sentry_sdk._types import MonitorConfig
1011

1112
if PY2:
1213
from sentry_sdk.crons._decorator_py2 import MonitorMixin
@@ -48,7 +49,7 @@ def test(arg):
4849
"""
4950

5051
def __init__(self, monitor_slug=None, monitor_config=None):
51-
# type: (Optional[str], Optional[dict[str, Any]]) -> None
52+
# type: (Optional[str], Optional[MonitorConfig]) -> None
5253
self.monitor_slug = monitor_slug
5354
self.monitor_config = monitor_config
5455

sentry_sdk/integrations/celery.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import sys
44
import time
55

6+
try:
7+
from typing import cast
8+
except ImportError:
9+
cast = lambda _, o: o
10+
611
from sentry_sdk.api import continue_trace
712
from sentry_sdk.consts import OP
813
from sentry_sdk._compat import reraise
@@ -31,7 +36,15 @@
3136
from typing import Union
3237

3338
from sentry_sdk.tracing import Span
34-
from sentry_sdk._types import EventProcessor, Event, Hint, ExcInfo
39+
from sentry_sdk._types import (
40+
EventProcessor,
41+
Event,
42+
Hint,
43+
ExcInfo,
44+
MonitorConfig,
45+
MonitorConfigScheduleType,
46+
MonitorConfigScheduleUnit,
47+
)
3548

3649
F = TypeVar("F", bound=Callable[..., Any])
3750

@@ -416,7 +429,7 @@ def _get_headers(task):
416429

417430

418431
def _get_humanized_interval(seconds):
419-
# type: (float) -> Tuple[int, str]
432+
# type: (float) -> Tuple[int, MonitorConfigScheduleUnit]
420433
TIME_UNITS = ( # noqa: N806
421434
("day", 60 * 60 * 24.0),
422435
("hour", 60 * 60.0),
@@ -427,17 +440,17 @@ def _get_humanized_interval(seconds):
427440
for unit, divider in TIME_UNITS:
428441
if seconds >= divider:
429442
interval = int(seconds / divider)
430-
return (interval, unit)
443+
return (interval, cast("MonitorConfigScheduleUnit", unit))
431444

432445
return (int(seconds), "second")
433446

434447

435448
def _get_monitor_config(celery_schedule, app, monitor_name):
436-
# type: (Any, Celery, str) -> Dict[str, Any]
437-
monitor_config = {} # type: Dict[str, Any]
438-
schedule_type = None # type: Optional[str]
449+
# type: (Any, Celery, str) -> MonitorConfig
450+
monitor_config = {} # type: MonitorConfig
451+
schedule_type = None # type: Optional[MonitorConfigScheduleType]
439452
schedule_value = None # type: Optional[Union[str, int]]
440-
schedule_unit = None # type: Optional[str]
453+
schedule_unit = None # type: Optional[MonitorConfigScheduleUnit]
441454

442455
if isinstance(celery_schedule, crontab):
443456
schedule_type = "crontab"

0 commit comments

Comments
 (0)