Skip to content

Commit 8d35bb4

Browse files
authored
type tests.sentry.incidents.action_handlers.* (#98112)
Found a pattern where this module just needed their `run_test` methods typed.
1 parent b57211d commit 8d35bb4

File tree

14 files changed

+71
-48
lines changed

14 files changed

+71
-48
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ module = [
680680
"tests.sentry.flags.providers.*",
681681
"tests.sentry.grouping.*",
682682
"tests.sentry.hybridcloud.*",
683+
"tests.sentry.incidents.action_handlers.*",
683684
"tests.sentry.incidents.handlers.*",
684685
"tests.sentry.incidents.serializers.*",
685686
"tests.sentry.insights.*",

src/sentry/incidents/action_handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def generate_incident_trigger_email_context(
509509
trigger_threshold: float,
510510
user: User | RpcUser | None = None,
511511
notification_uuid: str | None = None,
512-
):
512+
) -> dict[str, Any]:
513513
from sentry.notifications.notification_action.utils import should_fire_workflow_actions
514514
from sentry.seer.anomaly_detection.types import AnomalyDetectionThresholdType
515515

tests/relay_integration/test_integration.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66
import requests
7-
from sentry_relay.auth import generate_key_pair
7+
from sentry_relay.auth import SecretKey, generate_key_pair
88

99
from sentry.models.eventattachment import EventAttachment
1010
from sentry.tasks.relay import invalidate_project_config
@@ -17,7 +17,7 @@
1717
pytestmark = [requires_kafka]
1818

1919

20-
def create_trusted_relay_signature(secret_key):
20+
def create_trusted_relay_signature(secret_key: SecretKey) -> str | bytes:
2121
"""
2222
Mimics how the trusted relay signature is built so we can test communication.
2323
"""
@@ -33,7 +33,7 @@ def create_trusted_relay_signature(secret_key):
3333

3434

3535
class SentryRemoteTest(RelayStoreHelper, TransactionTestCase):
36-
def setup_for_trusted_relay_signature(self):
36+
def setup_for_trusted_relay_signature(self) -> tuple[str, SecretKey]:
3737
"""
3838
sets up project config settings and returns the relay_id and the secret key which
3939
can be used to create the signature if required.

tests/sentry/incidents/action_handlers/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ class FireTest(TestCase, abc.ABC):
1010
__test__ = Abstract(__module__, __qualname__)
1111

1212
@abc.abstractmethod
13-
def run_test(self, incident: Incident, method: str, **kwargs):
13+
def run_test(self, incident: Incident, method: str, **kwargs: object) -> None:
1414
pass
1515

16-
def run_fire_test(self, method="fire", chart_url=None, status=IncidentStatus.CLOSED):
16+
def run_fire_test(
17+
self,
18+
method: str = "fire",
19+
chart_url: str | None = None,
20+
status: IncidentStatus = IncidentStatus.CLOSED,
21+
) -> None:
1722
kwargs = {}
1823
if chart_url:
1924
kwargs = {"chart_url": chart_url}
@@ -24,4 +29,4 @@ def run_fire_test(self, method="fire", chart_url=None, status=IncidentStatus.CLO
2429
update_incident_status(
2530
incident, IncidentStatus.CLOSED, status_method=IncidentStatusMethod.MANUAL
2631
)
27-
return self.run_test(incident, method, **kwargs)
32+
self.run_test(incident, method, **kwargs)

tests/sentry/incidents/action_handlers/test_discord.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from unittest.mock import MagicMock, patch
22

3-
import orjson
43
import responses
54

65
from sentry.incidents.models.alert_rule import AlertRuleTriggerAction
7-
from sentry.incidents.models.incident import IncidentStatus
6+
from sentry.incidents.models.incident import Incident, IncidentStatus
87
from sentry.integrations.discord.client import CHANNEL_URL, DISCORD_BASE_URL, MESSAGE_URL
98
from sentry.integrations.discord.spec import DiscordMessagingSpec
109
from sentry.integrations.messaging.spec import MessagingActionHandler
@@ -51,7 +50,7 @@ def setUp(self) -> None:
5150
)
5251

5352
@responses.activate
54-
def run_test(self, incident, method):
53+
def run_test(self, incident: Incident, method: str) -> None:
5554
responses.add(
5655
method=responses.POST,
5756
url=f"{DISCORD_BASE_URL}{MESSAGE_URL.format(channel_id=self.channel_id)}",
@@ -69,9 +68,6 @@ def run_test(self, incident, method):
6968
metric_value=metric_value,
7069
)
7170

72-
data = orjson.loads(responses.calls[0].request.body)
73-
return data
74-
7571
def test_fire_metric_alert(self) -> None:
7672
self.run_fire_test()
7773

tests/sentry/incidents/action_handlers/test_email.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from functools import cached_property
2+
from typing import Any
23
from unittest.mock import MagicMock, patch
34

45
import orjson
@@ -27,13 +28,19 @@
2728
)
2829
from sentry.incidents.logic import CRITICAL_TRIGGER_LABEL, WARNING_TRIGGER_LABEL
2930
from sentry.incidents.models.alert_rule import (
31+
AlertRule,
3032
AlertRuleDetectionType,
3133
AlertRuleSeasonality,
3234
AlertRuleSensitivity,
3335
AlertRuleThresholdType,
3436
AlertRuleTriggerAction,
3537
)
36-
from sentry.incidents.models.incident import INCIDENT_STATUS, IncidentStatus, TriggerStatus
38+
from sentry.incidents.models.incident import (
39+
INCIDENT_STATUS,
40+
Incident,
41+
IncidentStatus,
42+
TriggerStatus,
43+
)
3744
from sentry.incidents.typings.metric_detector import (
3845
AlertContext,
3946
MetricIssueContext,
@@ -51,8 +58,10 @@
5158
from sentry.testutils.helpers.datetime import freeze_time
5259
from sentry.testutils.helpers.features import with_feature
5360
from sentry.testutils.silo import assume_test_silo_mode_of
61+
from sentry.users.models.user import User
5462
from sentry.users.models.user_option import UserOption
5563
from sentry.users.models.useremail import UserEmail
64+
from sentry.users.services.user import RpcUser
5665

5766
from . import FireTest
5867

@@ -62,7 +71,7 @@
6271
@freeze_time()
6372
class EmailActionHandlerTest(FireTest):
6473
@responses.activate
65-
def run_test(self, incident, method):
74+
def run_test(self, incident: Incident, method: str) -> None:
6675
action = self.create_alert_rule_trigger_action(
6776
target_identifier=str(self.user.id),
6877
triggered_for_incident=incident,
@@ -111,7 +120,7 @@ def setUp(self) -> None:
111120
self.handler = EmailActionHandler()
112121

113122
@cached_property
114-
def incident(self):
123+
def incident(self) -> Incident:
115124
return self.create_incident()
116125

117126
def test_user(self) -> None:
@@ -268,20 +277,20 @@ def test_team_email_routing(self) -> None:
268277

269278
@freeze_time()
270279
class EmailActionHandlerGenerateEmailContextTest(TestCase):
271-
def serialize_incident(self, incident) -> DetailedIncidentSerializerResponse:
280+
def serialize_incident(self, incident: Incident) -> DetailedIncidentSerializerResponse:
272281
return serialize(incident, None, DetailedIncidentSerializer())
273282

274-
def serialize_alert_rule(self, alert_rule) -> AlertRuleSerializerResponse:
283+
def serialize_alert_rule(self, alert_rule: AlertRule) -> AlertRuleSerializerResponse:
275284
return serialize(alert_rule, None, AlertRuleSerializer())
276285

277286
def _generate_email_context(
278287
self,
279-
incident,
280-
trigger_status,
281-
trigger_threshold,
282-
user=None,
283-
notification_uuid=None,
284-
):
288+
incident: Incident,
289+
trigger_status: TriggerStatus,
290+
trigger_threshold: float,
291+
user: User | RpcUser | None = None,
292+
notification_uuid: str | None = None,
293+
) -> dict[str, Any]:
285294
"""
286295
Helper method to generate email context from an incident and trigger status.
287296
Encapsulates the common pattern of creating contexts and serializing models.

tests/sentry/incidents/action_handlers/test_msteams.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
AlertRuleSensitivity,
1515
AlertRuleTriggerAction,
1616
)
17-
from sentry.incidents.models.incident import IncidentStatus, IncidentStatusMethod
17+
from sentry.incidents.models.incident import Incident, IncidentStatus, IncidentStatusMethod
1818
from sentry.incidents.typings.metric_detector import AlertContext, MetricIssueContext
1919
from sentry.integrations.messaging.spec import MessagingActionHandler
2020
from sentry.integrations.msteams.card_builder.block import (
@@ -77,7 +77,7 @@ def setUp(self) -> None:
7777

7878
@responses.activate
7979
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
80-
def run_test(self, incident, method, mock_record):
80+
def run_test(self, incident: Incident, method: str, mock_record: MagicMock) -> None:
8181
from sentry.integrations.msteams.card_builder.incident_attachment import (
8282
build_incident_attachment,
8383
)

tests/sentry/incidents/action_handlers/test_opsgenie.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
AlertRuleSensitivity,
1414
AlertRuleTriggerAction,
1515
)
16-
from sentry.incidents.models.incident import IncidentStatus, IncidentStatusMethod
16+
from sentry.incidents.models.incident import Incident, IncidentStatus, IncidentStatusMethod
1717
from sentry.incidents.typings.metric_detector import AlertContext, MetricIssueContext
1818
from sentry.integrations.models.integration import Integration
1919
from sentry.integrations.models.organization_integration import OrganizationIntegration
@@ -174,7 +174,7 @@ def test_build_incident_attachment_dynamic_alert(self, mock_seer_request: MagicM
174174
)
175175

176176
@responses.activate
177-
def run_test(self, incident, method):
177+
def run_test(self, incident: Incident, method: str) -> None:
178178
from sentry.integrations.opsgenie.utils import (
179179
attach_custom_priority,
180180
build_incident_attachment,

tests/sentry/incidents/action_handlers/test_pagerduty.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
AlertRuleSensitivity,
1515
AlertRuleTriggerAction,
1616
)
17-
from sentry.incidents.models.incident import IncidentStatus, IncidentStatusMethod
17+
from sentry.incidents.models.incident import Incident, IncidentStatus, IncidentStatusMethod
1818
from sentry.incidents.typings.metric_detector import AlertContext, MetricIssueContext
1919
from sentry.integrations.pagerduty.utils import add_service
2020
from sentry.seer.anomaly_detection.types import StoreDataResponse
@@ -161,7 +161,7 @@ def test_build_incident_attachment_dynamic_alert(self, mock_seer_request: MagicM
161161
)
162162

163163
@responses.activate
164-
def run_test(self, incident, method):
164+
def run_test(self, incident: Incident, method: str) -> None:
165165
from sentry.integrations.pagerduty.utils import (
166166
attach_custom_severity,
167167
build_incident_attachment,

tests/sentry/incidents/action_handlers/test_sentry_app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from sentry.incidents.action_handlers import SentryAppActionHandler
88
from sentry.incidents.endpoints.serializers.incident import IncidentSerializer
99
from sentry.incidents.models.alert_rule import AlertRuleTriggerAction
10-
from sentry.incidents.models.incident import IncidentStatus
10+
from sentry.incidents.models.incident import Incident, IncidentStatus
1111
from sentry.incidents.typings.metric_detector import AlertContext, MetricIssueContext
1212
from sentry.integrations.types import EventLifecycleOutcome
1313
from sentry.sentry_apps.metrics import SentryAppWebhookHaltReason
@@ -46,7 +46,7 @@ def setUp(self) -> None:
4646
self.handler = SentryAppActionHandler()
4747

4848
@responses.activate
49-
def run_test(self, incident, method):
49+
def run_test(self, incident: Incident, method: str) -> None:
5050
from sentry.rules.actions.notify_event_service import build_incident_attachment
5151

5252
responses.add(
@@ -210,7 +210,7 @@ def setUp(self) -> None:
210210
self.handler = SentryAppActionHandler()
211211

212212
@responses.activate
213-
def run_test(self, incident, method):
213+
def run_test(self, incident: Incident, method: str) -> None:
214214
from sentry.rules.actions.notify_event_service import build_incident_attachment
215215

216216
trigger = self.create_alert_rule_trigger(self.alert_rule, "hi", 1000)

0 commit comments

Comments
 (0)