Skip to content

Commit

Permalink
Use TypedDict
Browse files Browse the repository at this point in the history
  • Loading branch information
armenzg committed Jan 7, 2025
1 parent 76816b3 commit eb5e94b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
11 changes: 8 additions & 3 deletions src/sentry/issues/escalating.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import logging
import math
from collections import defaultdict
from collections.abc import Iterable, Mapping, MutableMapping, Sequence
from collections.abc import Iterable, Mapping, Sequence
from datetime import datetime, timedelta
from typing import Any, TypedDict

Expand Down Expand Up @@ -37,7 +37,12 @@
from sentry.models.activity import Activity
from sentry.models.group import Group, GroupStatus
from sentry.models.grouphistory import GroupHistoryStatus, record_group_history
from sentry.models.groupinbox import INBOX_REASON_DETAILS, GroupInboxReason, add_group_to_inbox
from sentry.models.groupinbox import (
INBOX_REASON_DETAILS,
GroupInboxReason,
InboxReasonDetails,
add_group_to_inbox,
)
from sentry.models.organization import Organization
from sentry.models.project import Project
from sentry.sentry_metrics.use_case_id_registry import UseCaseID
Expand Down Expand Up @@ -491,7 +496,7 @@ def manage_issue_states(
group: Group,
group_inbox_reason: GroupInboxReason,
event: GroupEvent | None = None,
snooze_details: MutableMapping[str, Any] | None = None,
snooze_details: InboxReasonDetails | None = None,
activity_data: Mapping[str, Any] | None = None,
) -> None:
from sentry.integrations.tasks.kick_off_status_syncs import kick_off_status_syncs
Expand Down
17 changes: 9 additions & 8 deletions src/sentry/models/groupinbox.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import annotations

import logging
from collections.abc import Iterable, MutableMapping
from collections.abc import Iterable
from datetime import datetime
from enum import Enum
from typing import TYPE_CHECKING, Any, TypedDict
from typing import TYPE_CHECKING, TypedDict

import jsonschema
import sentry_sdk
Expand Down Expand Up @@ -82,19 +82,20 @@ class Meta:


def add_group_to_inbox(
group: Group, reason: GroupInboxReason, reason_details: MutableMapping[str, Any] | None = None
group: Group,
reason: GroupInboxReason,
reason_details: InboxReasonDetails | None = None,
) -> GroupInbox:
if reason_details is not None:
if "until" in reason_details and reason_details["until"] is not None:
reason_details["until"] = reason_details["until"].replace(microsecond=0).isoformat()
if reason_details is not None and reason_details["until"] is not None:
reason_details["until"] = reason_details["until"].replace(microsecond=0)

try:
jsonschema.validate(reason_details, INBOX_REASON_DETAILS)
except jsonschema.ValidationError:
logging.exception("GroupInbox invalid jsonschema: %s", reason_details)
reason_details = None

group_inbox, created = GroupInbox.objects.get_or_create(
group_inbox, _ = GroupInbox.objects.get_or_create(
group=group,
defaults={
"project": group.project,
Expand Down Expand Up @@ -157,7 +158,7 @@ def bulk_remove_groups_from_inbox(


class InboxReasonDetails(TypedDict):
until: str | None
until: datetime | None
count: int | None
window: int | None
user_count: int | None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from sentry.models.groupinbox import (
GroupInbox,
GroupInboxReason,
InboxReasonDetails,
add_group_to_inbox,
remove_group_from_inbox,
)
Expand Down Expand Up @@ -1754,7 +1755,7 @@ def test_inbox_fields(self, _: MagicMock) -> None:
assert response.data[0]["inbox"]["reason"] == GroupInboxReason.NEW.value
assert response.data[0]["inbox"]["reason_details"] is None
remove_group_from_inbox(event.group)
snooze_details = {
snooze_details: InboxReasonDetails = {
"until": None,
"count": 3,
"window": None,
Expand Down Expand Up @@ -1786,7 +1787,7 @@ def test_inbox_fields_issue_states(self, _: MagicMock) -> None:
assert int(response.data[0]["id"]) == event.group.id
assert response.data[0]["inbox"]["reason"] == GroupInboxReason.NEW.value
remove_group_from_inbox(event.group)
snooze_details = {
snooze_details: InboxReasonDetails = {
"until": None,
"count": 3,
"window": None,
Expand Down
5 changes: 0 additions & 5 deletions tests/sentry/models/test_groupinbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,3 @@ def test_remove_from_inbox(self):
activities = Activity.objects.all()
assert len(activities) == 1
assert activities[0].type == ActivityType.MARK_REVIEWED.value

def test_invalid_reason_details(self):
reason_details = {"meow": 123}
add_group_to_inbox(self.group, GroupInboxReason.NEW, reason_details)
assert GroupInbox.objects.get(group=self.group.id).reason_details is None

0 comments on commit eb5e94b

Please sign in to comment.