Browse Source

ref: use TypeGuard to fix a few type ignores (#75473)

<!-- Describe your PR here. -->
anthony sottile 7 months ago
parent
commit
c89514bdb2

+ 5 - 3
src/sentry/notifications/helpers.py

@@ -3,7 +3,7 @@ from __future__ import annotations
 import logging
 from collections import defaultdict
 from collections.abc import Iterable, Mapping, MutableMapping
-from typing import TYPE_CHECKING, Any
+from typing import TYPE_CHECKING, Any, TypeGuard
 
 from django.db.models import Subquery
 
@@ -109,7 +109,9 @@ def get_reason_context(extra_context: Mapping[str, Any]) -> MutableMapping[str,
     }
 
 
-def recipient_is_user(recipient: Actor | Team | RpcUser | User) -> bool:
+def recipient_is_user(
+    recipient: Actor | Team | RpcUser | User,
+) -> TypeGuard[Actor | RpcUser | User]:
     from sentry.models.user import User
 
     if isinstance(recipient, Actor) and recipient.is_user:
@@ -117,7 +119,7 @@ def recipient_is_user(recipient: Actor | Team | RpcUser | User) -> bool:
     return isinstance(recipient, (RpcUser, User))
 
 
-def recipient_is_team(recipient: Actor | Team | RpcUser | User) -> bool:
+def recipient_is_team(recipient: Actor | Team | RpcUser | User) -> TypeGuard[Actor | Team]:
     from sentry.models.team import Team
 
     if isinstance(recipient, Actor) and recipient.is_team:

+ 3 - 5
src/sentry/notifications/notificationcontroller.py

@@ -82,13 +82,11 @@ class NotificationController:
         if org and features.has("organizations:team-workflow-notifications", org):
             self.recipients: list[Recipient] = []
             for recipient in recipients:
-                if recipient_is_team(
-                    recipient
-                ):  # this call assures the recipient type is okay (so can safely ignore below type errors)
-                    if team_is_valid_recipient(recipient):  # type: ignore[arg-type]
+                if recipient_is_team(recipient):
+                    if team_is_valid_recipient(recipient):
                         self.recipients.append(recipient)
                     else:
-                        self.recipients += get_team_members(recipient)  # type: ignore[arg-type]
+                        self.recipients += get_team_members(recipient)
                 else:
                     self.recipients.append(recipient)
         else: