From eb5e94b03665365252bb49610da393f6c9f395bf Mon Sep 17 00:00:00 2001 From: Armen Zambrano G <44410+armenzg@users.noreply.github.com> Date: Tue, 7 Jan 2025 08:32:48 -0500 Subject: [PATCH] Use TypedDict --- src/sentry/issues/escalating.py | 11 ++++++++--- src/sentry/models/groupinbox.py | 17 +++++++++-------- .../endpoints/test_organization_group_index.py | 5 +++-- tests/sentry/models/test_groupinbox.py | 5 ----- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/sentry/issues/escalating.py b/src/sentry/issues/escalating.py index 99cfaa223ce31b..69947104c32023 100644 --- a/src/sentry/issues/escalating.py +++ b/src/sentry/issues/escalating.py @@ -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 @@ -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 @@ -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 diff --git a/src/sentry/models/groupinbox.py b/src/sentry/models/groupinbox.py index 86a77f243da948..e5dcb30a81319b 100644 --- a/src/sentry/models/groupinbox.py +++ b/src/sentry/models/groupinbox.py @@ -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 @@ -82,11 +82,12 @@ 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) @@ -94,7 +95,7 @@ def add_group_to_inbox( 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, @@ -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 diff --git a/tests/sentry/issues/endpoints/test_organization_group_index.py b/tests/sentry/issues/endpoints/test_organization_group_index.py index e90231d3ce1a42..37bca50a66b1ca 100644 --- a/tests/sentry/issues/endpoints/test_organization_group_index.py +++ b/tests/sentry/issues/endpoints/test_organization_group_index.py @@ -32,6 +32,7 @@ from sentry.models.groupinbox import ( GroupInbox, GroupInboxReason, + InboxReasonDetails, add_group_to_inbox, remove_group_from_inbox, ) @@ -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, @@ -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, diff --git a/tests/sentry/models/test_groupinbox.py b/tests/sentry/models/test_groupinbox.py index 5a83db553fda36..b77c6cacdfc9d9 100644 --- a/tests/sentry/models/test_groupinbox.py +++ b/tests/sentry/models/test_groupinbox.py @@ -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