Browse Source

chore(hybridcloud) Remove more RPC code from the naughty list (#51311)

Fix a handful of mypy errors by updating types to reflect possible
realities as defined by the ORM models.
Mark Story 1 year ago
parent
commit
14d4d5433a

+ 0 - 3
pyproject.toml

@@ -940,10 +940,7 @@ module = [
     "sentry.sentry_metrics.consumers.indexer.slicing_router",
     "sentry.sentry_metrics.indexer.postgres.postgres_v2",
     "sentry.services.hybrid_cloud.actor",
-    "sentry.services.hybrid_cloud.app.serial",
     "sentry.services.hybrid_cloud.auth.impl",
-    "sentry.services.hybrid_cloud.hook.serial",
-    "sentry.services.hybrid_cloud.identity.serial",
     "sentry.services.hybrid_cloud.integration.impl",
     "sentry.services.hybrid_cloud.integration.service",
     "sentry.services.hybrid_cloud.log.impl",

+ 3 - 1
src/sentry/services/hybrid_cloud/app/model.py

@@ -37,7 +37,7 @@ class RpcSentryApp(RpcModel):
     id: int = -1
     scope_list: List[str] = Field(default_factory=list)
     application_id: int = -1
-    application: RpcApiApplication = Field(default_factory=RpcApiApplication)
+    application: Optional[RpcApiApplication] = None
     proxy_user_id: Optional[int] = None  # can be null on deletion.
     owner_id: int = -1  # relation to an organization
     name: str = ""
@@ -56,6 +56,8 @@ class RpcSentryApp(RpcModel):
         return set(self.scope_list).issubset(encoded_scopes)
 
     def build_signature(self, body: str) -> str:
+        assert self.application, "Cannot build_signature without an application"
+
         secret = self.application.client_secret
         return hmac.new(
             key=secret.encode("utf-8"), msg=body.encode("utf-8"), digestmod=sha256

+ 4 - 1
src/sentry/services/hybrid_cloud/app/serial.py

@@ -9,7 +9,9 @@ from sentry.services.hybrid_cloud.app import (
 )
 
 
-def serialize_api_application(api_app: ApiApplication) -> RpcApiApplication:
+def serialize_api_application(api_app: Optional[ApiApplication]) -> Optional[RpcApiApplication]:
+    if not api_app:
+        return None
     return RpcApiApplication(
         id=api_app.id,
         client_id=api_app.client_id,
@@ -43,6 +45,7 @@ def serialize_sentry_app_installation(
 ) -> RpcSentryAppInstallation:
     if app is None:
         app = installation.sentry_app
+        assert app is not None
 
     return RpcSentryAppInstallation(
         id=installation.id,

+ 1 - 1
src/sentry/services/hybrid_cloud/hook/model.py

@@ -12,7 +12,7 @@ from sentry.services.hybrid_cloud import RpcModel
 
 class RpcServiceHook(RpcModel):
     id: int = -1
-    guid: str = ""
+    guid: Optional[str] = ""
     application_id: int = -1
     installation_id: Optional[int] = None
     project_id: Optional[int] = None

+ 3 - 1
src/sentry/services/hybrid_cloud/identity/model.py

@@ -2,6 +2,8 @@
 #     from __future__ import annotations
 # in modules such as this one where hybrid cloud data models or service classes are
 # defined, because we want to reflect on type annotations and avoid forward references.
+from typing import Optional
+
 from typing_extensions import TypedDict
 
 from sentry.services.hybrid_cloud import RpcModel
@@ -10,7 +12,7 @@ from sentry.services.hybrid_cloud import RpcModel
 class RpcIdentityProvider(RpcModel):
     id: int
     type: str
-    external_id: str
+    external_id: Optional[str]
 
 
 class RpcIdentity(RpcModel):