Skip to content

Commit a5750fd

Browse files
ref: fix typing for endpoints.project_rule_preview (#82089)
<!-- Describe your PR here. -->
1 parent ec46b92 commit a5750fd

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed

Diff for: pyproject.toml

-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ module = [
150150
"sentry.api.endpoints.project_ownership",
151151
"sentry.api.endpoints.project_release_files",
152152
"sentry.api.endpoints.project_repo_path_parsing",
153-
"sentry.api.endpoints.project_rule_preview",
154153
"sentry.api.endpoints.project_rules_configuration",
155154
"sentry.api.endpoints.project_transaction_names",
156155
"sentry.api.endpoints.team_details",

Diff for: src/sentry/api/endpoints/project_rule_preview.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from collections.abc import Mapping
24
from typing import Any
35

@@ -10,10 +12,11 @@
1012
from sentry.api.api_publish_status import ApiPublishStatus
1113
from sentry.api.base import region_silo_endpoint
1214
from sentry.api.bases.project import ProjectAlertRulePermission, ProjectEndpoint
13-
from sentry.api.serializers import GroupSerializer, serialize
15+
from sentry.api.serializers import serialize
16+
from sentry.api.serializers.models.group import BaseGroupSerializerResponse, GroupSerializer
1417
from sentry.api.serializers.rest_framework.rule import RulePreviewSerializer
1518
from sentry.models.group import Group
16-
from sentry.models.groupinbox import get_inbox_details
19+
from sentry.models.groupinbox import InboxDetails, get_inbox_details
1720
from sentry.rules.history.preview import preview
1821

1922

@@ -86,12 +89,19 @@ def post(self, request: Request, project) -> Response:
8689
return response
8790

8891

92+
class _PreviewResponse(BaseGroupSerializerResponse):
93+
inbox: InboxDetails
94+
lastTriggered: int
95+
96+
8997
class PreviewSerializer(GroupSerializer):
9098
def serialize(
91-
self, obj: dict[str, Any], attrs: Mapping[Any, Any], user: Any, **kwargs: Any
92-
) -> dict[str, Any]:
99+
self, obj: Group, attrs: Mapping[Any, Any], user: Any, **kwargs: Any
100+
) -> _PreviewResponse:
93101
result = super().serialize(obj, attrs, user, **kwargs)
94102
group_id = int(result["id"])
95-
result["inbox"] = kwargs["inbox_details"].get(group_id)
96-
result["lastTriggered"] = kwargs["group_fires"][group_id]
97-
return result
103+
return {
104+
**result,
105+
"inbox": kwargs["inbox_details"].get(group_id),
106+
"lastTriggered": kwargs["group_fires"][group_id],
107+
}

Diff for: src/sentry/api/serializers/models/group.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def get_attrs(
317317
return result
318318

319319
def serialize(
320-
self, obj: Group, attrs: MutableMapping[str, Any], user: Any, **kwargs: Any
320+
self, obj: Group, attrs: Mapping[str, Any], user: Any, **kwargs: Any
321321
) -> BaseGroupSerializerResponse:
322322
status_details, status_label = self._get_status(attrs, obj)
323323
permalink = self._get_permalink(attrs, obj)
@@ -390,7 +390,7 @@ def _collapse(self, key) -> bool:
390390
return False
391391
return key in self.collapse
392392

393-
def _get_status(self, attrs: MutableMapping[str, Any], obj: Group):
393+
def _get_status(self, attrs: Mapping[str, Any], obj: Group):
394394
status = obj.status
395395
status_details = {}
396396
if attrs["ignore_until"]:
@@ -850,7 +850,7 @@ def __seen_stats_impl(
850850

851851
class SharedGroupSerializer(GroupSerializer):
852852
def serialize(
853-
self, obj: Group, attrs: MutableMapping[str, Any], user: Any, **kwargs: Any
853+
self, obj: Group, attrs: Mapping[str, Any], user: Any, **kwargs: Any
854854
) -> BaseGroupSerializerResponse:
855855
result = super().serialize(obj, attrs, user)
856856

Diff for: src/sentry/models/groupinbox.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
from __future__ import annotations
2+
13
import logging
4+
from collections.abc import Iterable
5+
from datetime import datetime
26
from enum import Enum
7+
from typing import TypedDict
38

49
import jsonschema
510
import sentry_sdk
@@ -135,16 +140,28 @@ def bulk_remove_groups_from_inbox(groups, action=None, user=None, referrer=None)
135140
pass
136141

137142

138-
def get_inbox_details(group_list):
143+
class InboxReasonDetails(TypedDict):
144+
until: str | None
145+
count: int | None
146+
window: int | None
147+
user_count: int | None
148+
user_window: int | None
149+
150+
151+
class InboxDetails(TypedDict):
152+
reason: int
153+
reason_details: InboxReasonDetails | None
154+
date_added: datetime
155+
156+
157+
def get_inbox_details(group_list: Iterable[Group]) -> dict[int, InboxDetails]:
139158
group_ids = [g.id for g in group_list]
140159
group_inboxes = GroupInbox.objects.filter(group__in=group_ids)
141-
inbox_stats = {
160+
return {
142161
gi.group_id: {
143162
"reason": gi.reason,
144163
"reason_details": gi.reason_details,
145164
"date_added": gi.date_added,
146165
}
147166
for gi in group_inboxes
148167
}
149-
150-
return inbox_stats

0 commit comments

Comments
 (0)