Browse Source

ref: Add typing to a bunch of relay-related modules (#50869)

Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com>
Markus Unterwaditzer 1 year ago
parent
commit
635ca1b84c

+ 0 - 18
pyproject.toml

@@ -184,7 +184,6 @@ module = [
     "sentry.api.endpoints.avatar.team",
     "sentry.api.endpoints.avatar.user",
     "sentry.api.endpoints.broadcast_index",
-    "sentry.api.endpoints.builtin_symbol_sources",
     "sentry.api.endpoints.catchall",
     "sentry.api.endpoints.chunk",
     "sentry.api.endpoints.codeowners",
@@ -240,7 +239,6 @@ module = [
     "sentry.api.endpoints.integrations.sentry_apps.requests",
     "sentry.api.endpoints.integrations.sentry_apps.stats.details",
     "sentry.api.endpoints.integrations.sentry_apps.stats.index",
-    "sentry.api.endpoints.internal.beacon",
     "sentry.api.endpoints.internal.integration_proxy",
     "sentry.api.endpoints.internal.mail",
     "sentry.api.endpoints.internal.stats",
@@ -334,15 +332,8 @@ module = [
     "sentry.api.endpoints.project_tags",
     "sentry.api.endpoints.project_transaction_names",
     "sentry.api.endpoints.project_transfer",
-    "sentry.api.endpoints.project_user_reports",
     "sentry.api.endpoints.project_user_stats",
     "sentry.api.endpoints.prompts_activity",
-    "sentry.api.endpoints.relay.health_check",
-    "sentry.api.endpoints.relay.project_configs",
-    "sentry.api.endpoints.relay.project_ids",
-    "sentry.api.endpoints.relay.public_keys",
-    "sentry.api.endpoints.relay.register_challenge",
-    "sentry.api.endpoints.relay.register_response",
     "sentry.api.endpoints.rpc",
     "sentry.api.endpoints.rule_snooze",
     "sentry.api.endpoints.setup_wizard",
@@ -577,7 +568,6 @@ module = [
     "sentry.integrations.aws_lambda.utils",
     "sentry.integrations.base",
     "sentry.integrations.bitbucket.client",
-    "sentry.integrations.bitbucket.descriptor",
     "sentry.integrations.bitbucket.installed",
     "sentry.integrations.bitbucket.integration",
     "sentry.integrations.bitbucket.issues",
@@ -613,7 +603,6 @@ module = [
     "sentry.integrations.jira.actions.create_ticket",
     "sentry.integrations.jira.actions.form",
     "sentry.integrations.jira.client",
-    "sentry.integrations.jira.endpoints.descriptor",
     "sentry.integrations.jira.endpoints.search",
     "sentry.integrations.jira.integration",
     "sentry.integrations.jira.views.base",
@@ -672,7 +661,6 @@ module = [
     "sentry.integrations.slack.webhooks.command",
     "sentry.integrations.slack.webhooks.event",
     "sentry.integrations.utils.commit_context",
-    "sentry.integrations.utils.scope",
     "sentry.integrations.utils.sync",
     "sentry.integrations.vercel.client",
     "sentry.integrations.vercel.integration",
@@ -903,8 +891,6 @@ module = [
     "sentry.receivers.releases",
     "sentry.receivers.sentry_apps",
     "sentry.receivers.users",
-    "sentry.relay.config",
-    "sentry.relay.config.metric_extraction",
     "sentry.release_health.metrics",
     "sentry.release_health.metrics_sessions_v2",
     "sentry.release_health.sessions",
@@ -1069,7 +1055,6 @@ module = [
     "sentry.tasks.merge",
     "sentry.tasks.post_process",
     "sentry.tasks.process_buffer",
-    "sentry.tasks.relay",
     "sentry.tasks.release_registry",
     "sentry.tasks.reprocessing",
     "sentry.tasks.reprocessing2",
@@ -1100,7 +1085,6 @@ module = [
     "sentry.testutils.modelmanifest",
     "sentry.testutils.performance_issues.span_builder",
     "sentry.testutils.performance_issues.store_transaction",
-    "sentry.testutils.relay",
     "sentry.tsdb.base",
     "sentry.tsdb.dummy",
     "sentry.tsdb.inmemory",
@@ -1385,7 +1369,6 @@ module = [
     "tests.sentry.api_gateway.test_api_gateway",
     "tests.sentry.api_gateway.test_proxy",
     "tests.sentry.apidocs.test_extensions",
-    "tests.sentry.apidocs.test_schema",
     "tests.sentry.auth.authenticators.test_sms",
     "tests.sentry.auth.authenticators.test_u2f",
     "tests.sentry.auth.providers.google.test_provider",
@@ -1488,7 +1471,6 @@ module = [
     "tests.sentry.integrations.aws_lambda.test_client",
     "tests.sentry.integrations.aws_lambda.test_integration",
     "tests.sentry.integrations.aws_lambda.test_utils",
-    "tests.sentry.integrations.bitbucket.test_descriptor",
     "tests.sentry.integrations.bitbucket.test_installed",
     "tests.sentry.integrations.bitbucket.test_issues",
     "tests.sentry.integrations.github.test_client",

+ 3 - 1
src/sentry/api/authentication.py

@@ -1,3 +1,5 @@
+from typing import Optional, Tuple
+
 from django.conf import settings
 from django.contrib.auth.models import AnonymousUser
 from django.utils.crypto import constant_time_compare
@@ -39,7 +41,7 @@ def is_static_relay(request):
     return relay_info is not None
 
 
-def relay_from_id(request, relay_id):
+def relay_from_id(request, relay_id) -> Tuple[Optional[Relay], bool]:
     """
     Tries to find a Relay for a given id
     If the id is statically registered than no DB access will be done.

+ 2 - 2
src/sentry/api/base.py

@@ -126,8 +126,8 @@ def allow_cors_options(func):
 
 class Endpoint(APIView):
     # Note: the available renderer and parser classes can be found in conf/server.py.
-    authentication_classes = DEFAULT_AUTHENTICATION
-    permission_classes: Tuple[Type[permissions.BasePermission]] = (NoPermission,)
+    authentication_classes: Tuple[Type[BaseAuthentication], ...] = DEFAULT_AUTHENTICATION
+    permission_classes: Tuple[Type[permissions.BasePermission], ...] = (NoPermission,)
 
     cursor_name = "cursor"
 

+ 2 - 2
src/sentry/api/endpoints/relay/health_check.py

@@ -15,8 +15,8 @@ class RelayHealthCheck(Endpoint):
     Relay doesn't need to care if it connects to another Relay or directly to sentry.
     """
 
-    authentication_classes = []
-    permission_classes = []
+    authentication_classes = ()
+    permission_classes = ()
 
     def get(self, request: Request) -> Response:
         return Response({"is_healthy": True}, status=200)

+ 17 - 15
src/sentry/api/endpoints/relay/project_configs.py

@@ -1,6 +1,6 @@
 import logging
 import random
-from typing import Optional
+from typing import Any, List, MutableMapping, Optional, Set
 
 from django.conf import settings
 from rest_framework.request import Request
@@ -20,6 +20,8 @@ logger = logging.getLogger(__name__)
 # We'll log project IDS if their config size is larger than this value
 PROJECT_CONFIG_SIZE_THRESHOLD = 10000
 
+ProjectConfig = MutableMapping[str, Any]
+
 
 def _sample_apm():
     return random.random() < getattr(settings, "SENTRY_RELAY_ENDPOINT_APM_SAMPLING", 0)
@@ -134,7 +136,7 @@ class RelayProjectConfigsEndpoint(Endpoint):
 
         Debouncing of the project happens after the task has been scheduled.
         """
-        cached_config = projectconfig_cache.get(public_key)
+        cached_config = projectconfig_cache.backend.get(public_key)
         if cached_config:
             return cached_config
 
@@ -145,8 +147,8 @@ class RelayProjectConfigsEndpoint(Endpoint):
         public_keys = request.relay_request_data.get("publicKeys")
         public_keys = set(public_keys or ())
 
-        project_keys = {}  # type: dict[str, ProjectKey]
-        project_ids = set()  # type: set[int]
+        project_keys: MutableMapping[str, ProjectKey] = {}
+        project_ids: Set[int] = set()
 
         with start_span(op="relay_fetch_keys"):
             with metrics.timer("relay_project_configs.fetching_keys.duration"):
@@ -157,8 +159,8 @@ class RelayProjectConfigsEndpoint(Endpoint):
                     project_keys[key.public_key] = key
                     project_ids.add(key.project_id)
 
-        projects = {}  # type: dict[int, Project]
-        organization_ids = set()  # type: set[int]
+        projects: MutableMapping[int, Project] = {}
+        organization_ids: Set[int] = set()
 
         with start_span(op="relay_fetch_projects"):
             with metrics.timer("relay_project_configs.fetching_projects.duration"):
@@ -169,7 +171,7 @@ class RelayProjectConfigsEndpoint(Endpoint):
         # Preload all organizations and their options to prevent repeated
         # database access when computing the project configuration.
 
-        orgs = {}  # type: dict[int, Organization]
+        orgs: MutableMapping[int, Organization] = {}
 
         with start_span(op="relay_fetch_orgs"):
             with metrics.timer("relay_project_configs.fetching_orgs.duration"):
@@ -186,7 +188,7 @@ class RelayProjectConfigsEndpoint(Endpoint):
         metrics.timing("relay_project_configs.projects_fetched", len(projects))
         metrics.timing("relay_project_configs.orgs_fetched", len(orgs))
 
-        configs = {}
+        configs: MutableMapping[str, ProjectConfig] = {}
         for public_key in public_keys:
             configs[public_key] = {"disabled": True}
 
@@ -216,7 +218,7 @@ class RelayProjectConfigsEndpoint(Endpoint):
             configs[public_key] = project_config.to_dict()
 
         if full_config_requested:
-            projectconfig_cache.set_many(configs)
+            projectconfig_cache.backend.set_many(configs)
 
         return Response({"configs": configs}, status=200)
 
@@ -233,11 +235,11 @@ class RelayProjectConfigsEndpoint(Endpoint):
         with start_span(op="relay_fetch_orgs"):
             # Preload all organizations and their options to prevent repeated
             # database access when computing the project configuration.
-            org_ids = {project.organization_id for project in projects.values()}
+            org_ids: Set[int] = {project.organization_id for project in projects.values()}
             if org_ids:
                 with metrics.timer("relay_project_configs.fetching_orgs.duration"):
-                    orgs = Organization.objects.get_many_from_cache(org_ids)
-                    orgs = {o.id: o for o in orgs if request.relay.has_org_access(o)}
+                    orgs_seq = Organization.objects.get_many_from_cache(org_ids)
+                    orgs = {o.id: o for o in orgs_seq if request.relay.has_org_access(o)}
             else:
                 orgs = {}
 
@@ -246,7 +248,7 @@ class RelayProjectConfigsEndpoint(Endpoint):
                     OrganizationOption.objects.get_all_values(org_id)
 
         with start_span(op="relay_fetch_keys"):
-            project_keys = {}
+            project_keys: MutableMapping[int, List[ProjectKey]] = {}
             for key in ProjectKey.objects.filter(project_id__in=project_ids):
                 project_keys.setdefault(key.project_id, []).append(key)
 
@@ -254,7 +256,7 @@ class RelayProjectConfigsEndpoint(Endpoint):
         metrics.timing("relay_project_configs.projects_fetched", len(projects))
         metrics.timing("relay_project_configs.orgs_fetched", len(orgs))
 
-        configs = {}
+        configs: MutableMapping[str, ProjectConfig] = {}
         for project_id in project_ids:
             configs[str(project_id)] = {"disabled": True}
 
@@ -280,6 +282,6 @@ class RelayProjectConfigsEndpoint(Endpoint):
             configs[str(project_id)] = project_config.to_dict()
 
         if full_config_requested:
-            projectconfig_cache.set_many(configs)
+            projectconfig_cache.backend.set_many(configs)
 
         return Response({"configs": configs}, status=200)

+ 1 - 1
src/sentry/api/endpoints/relay/register_challenge.py

@@ -55,7 +55,7 @@ class RelayRegisterChallengeEndpoint(Endpoint):
 
         public_key = json_data.get("public_key")
         if not public_key:
-            return Response({"detail": "Missing public key"}, status=status.HTTP_400_FORBIDDEN)
+            return Response({"detail": "Missing public key"}, status=status.HTTP_400_BAD_REQUEST)
 
         if not settings.SENTRY_RELAY_OPEN_REGISTRATION and not (
             is_internal_relay(request, public_key) or is_static_relay(request)

+ 1 - 0
src/sentry/api/endpoints/relay/register_response.py

@@ -98,4 +98,5 @@ class RelayRegisterResponseEndpoint(Endpoint):
                 relay_usage.public_key = public_key
                 relay_usage.save()
 
+        assert relay is not None
         return Response(serialize({"relay_id": relay.relay_id}))

+ 1 - 1
src/sentry/db/models/base.py

@@ -202,7 +202,7 @@ class ModelSiloLimit(SiloLimit):
 
         return handle
 
-    def __call__(self, model_class: ModelClass) -> Type[ModelClass]:
+    def __call__(self, model_class: Type[ModelClass]) -> Type[ModelClass]:
         if not (isinstance(model_class, type) and issubclass(model_class, models.Model)):
             raise TypeError("`@ModelSiloLimit ` must decorate a Model class")
 

+ 2 - 1
src/sentry/db/models/manager/base.py

@@ -8,6 +8,7 @@ from enum import IntEnum, auto
 from typing import (
     Any,
     Callable,
+    Collection,
     Dict,
     Generator,
     Generic,
@@ -329,7 +330,7 @@ class BaseManager(DjangoBaseManager.from_queryset(BaseQuerySet), Generic[M]):  #
         else:
             raise ValueError("We cannot cache this query. Just hit the database.")
 
-    def get_many_from_cache(self, values: Sequence[str], key: str = "pk") -> Sequence[Any]:
+    def get_many_from_cache(self, values: Collection[str | int], key: str = "pk") -> Sequence[Any]:
         """
         Wrapper around `QuerySet.filter(pk__in=values)` which supports caching of
         the intermediate value.  Callee is responsible for making sure the

+ 3 - 0
src/sentry/eventstore/__init__.py

@@ -5,4 +5,7 @@ from .base import EventStorage, Filter  # NOQA
 backend = LazyServiceWrapper(
     EventStorage, "sentry.eventstore.snuba.SnubaEventStorage", {}, metrics_path="eventstore"
 )
+
 backend.expose(locals())
+
+__all__ = ["backend", "Filter"]

Some files were not shown because too many files changed in this diff