Просмотр исходного кода

fix(typing): fix typing on BaseApiClient (#74921)

Cathy Teng 7 месяцев назад
Родитель
Сommit
69177a0309

+ 0 - 1
pyproject.toml

@@ -377,7 +377,6 @@ module = [
     "sentry.sentry_apps.installations",
     "sentry.sentry_metrics.consumers.indexer.slicing_router",
     "sentry.sentry_metrics.indexer.postgres.postgres_v2",
-    "sentry.shared_integrations.client.base",
     "sentry.shared_integrations.client.proxy",
     "sentry.similarity.features",
     "sentry.snuba.errors",

+ 1 - 1
src/sentry/integrations/discord/client.py

@@ -6,8 +6,8 @@ from typing import Any
 from urllib.parse import urlencode
 
 import orjson
+from requests.models import Response
 from rest_framework import status
-from rest_framework.response import Response
 
 from sentry import options
 from sentry.integrations.client import ApiClient

+ 15 - 8
src/sentry/shared_integrations/client/base.py

@@ -1,7 +1,7 @@
 from __future__ import annotations
 
 from collections.abc import Callable, Mapping, Sequence
-from typing import Any, Literal, Self, Union, overload
+from typing import Any, Literal, Self, TypedDict, Union, overload
 
 import sentry_sdk
 from django.core.cache import cache
@@ -31,6 +31,16 @@ from ..track_response import TrackResponseMixin
 BaseApiResponseX = Union[BaseApiResponse, Mapping[str, Any], Response]
 
 
+class SessionSettings(TypedDict):
+    timeout: int
+    allow_redirects: bool
+    # the below are taken from session.merge_environment_settings
+    proxies: Any
+    stream: Any
+    verify: Any
+    cert: Any
+
+
 class BaseApiClient(TrackResponseMixin):
     base_url: str | None = None
 
@@ -136,7 +146,7 @@ class BaseApiClient(TrackResponseMixin):
         headers: Mapping[str, str] | None = None,
         data: Mapping[str, str] | None = None,
         params: Mapping[str, str] | None = None,
-        auth: tuple[str, str] | str | None = None,
+        auth: tuple[str, str] | None = None,
         json: bool = True,
         allow_text: bool | None = None,
         allow_redirects: bool | None = None,
@@ -173,7 +183,7 @@ class BaseApiClient(TrackResponseMixin):
         headers: Mapping[str, str] | None = None,
         data: Mapping[str, str] | None = None,
         params: Mapping[str, str] | None = None,
-        auth: str | None = None,
+        auth: tuple[str, str] | str | None = None,
         json: bool = True,
         allow_text: bool | None = None,
         allow_redirects: bool | None = None,
@@ -231,15 +241,12 @@ class BaseApiClient(TrackResponseMixin):
                     verify=self.verify_ssl,
                     cert=None,
                 )
-                send_kwargs = {
+                session_settings: SessionSettings = {
                     "timeout": timeout,
                     "allow_redirects": allow_redirects,
                     **environment_settings,
                 }
-                resp: Response = session.send(
-                    finalized_request,
-                    **send_kwargs,
-                )
+                resp: Response = session.send(finalized_request, **session_settings)
                 if raw_response:
                     return resp
                 resp.raise_for_status()

+ 1 - 1
src/sentry/shared_integrations/client/internal.py

@@ -2,7 +2,7 @@ from __future__ import annotations
 
 from typing import Any
 
-from rest_framework.response import Response
+from requests.models import Response
 
 from sentry.api.client import ApiClient
 from sentry.shared_integrations.track_response import TrackResponseMixin

+ 1 - 1
src/sentry/shared_integrations/exceptions/__init__.py

@@ -98,7 +98,7 @@ class ApiHostError(ApiError):
     code = 503
 
     @classmethod
-    def from_exception(cls, exception: RequestException) -> ApiHostError:
+    def from_exception(cls, exception: Exception) -> ApiHostError:
         maybe_request = getattr(exception, "request", None)
         if maybe_request is not None:
             return cls.from_request(maybe_request)

+ 1 - 1
src/sentry/shared_integrations/track_response.py

@@ -4,7 +4,7 @@ import logging
 from collections.abc import Mapping
 
 from django.utils.functional import cached_property
-from rest_framework.response import Response
+from requests.models import Response
 
 from sentry.utils import metrics