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

ref: fix remaining 'valid-type' typing errors (#51909)

<!-- Describe your PR here. -->
anthony sottile 1 год назад
Родитель
Сommit
6c1b171196

+ 0 - 2
pyproject.toml

@@ -1467,7 +1467,6 @@ module = [
     "tests.sentry.testutils.helpers.test_features",
     "tests.sentry.tsdb.test_redissnuba",
     "tests.sentry.utils.email.test_list_resolver",
-    "tests.sentry.utils.kvstore.test_common",
     "tests.sentry.utils.locking.backends.test_redis",
     "tests.sentry.utils.suspect_resolutions.test_metric_correlation",
     "tests.sentry.utils.test_audit",
@@ -1565,7 +1564,6 @@ disable_error_code = [
     "typeddict-unknown-key",
     "union-attr",
     "unreachable",
-    "valid-type",
     "var-annotated",
 ]
 # end: sentry modules with typing issues

+ 13 - 4
src/sentry/scim/endpoints/members.py

@@ -1,6 +1,6 @@
 from __future__ import annotations
 
-from typing import Any, Dict, Union
+from typing import Any, Dict, List, Union
 
 import sentry_sdk
 from django.conf import settings
@@ -12,6 +12,7 @@ from rest_framework.exceptions import PermissionDenied, ValidationError
 from rest_framework.fields import Field
 from rest_framework.request import Request
 from rest_framework.response import Response
+from typing_extensions import TypedDict
 
 from sentry import audit_log, roles
 from sentry.api.base import control_silo_endpoint
@@ -32,6 +33,7 @@ from sentry.apidocs.constants import (
 )
 from sentry.apidocs.examples.scim_examples import SCIMExamples
 from sentry.apidocs.parameters import GlobalParams, SCIMParams
+from sentry.apidocs.utils import inline_sentry_response_serializer
 from sentry.auth.providers.saml2.activedirectory.apps import ACTIVE_DIRECTORY_PROVIDER_NAME
 from sentry.models import AuthIdentity, AuthProvider, InviteStatus, OrganizationMember
 from sentry.roles import organization_roles
@@ -52,7 +54,6 @@ from .utils import (
     SCIMApiError,
     SCIMEndpoint,
     SCIMQueryParamSerializer,
-    scim_response_envelope,
 )
 
 ERR_ONLY_OWNER = "You cannot remove the only remaining owner of the organization."
@@ -358,6 +359,14 @@ class OrganizationSCIMMemberDetails(SCIMEndpoint, OrganizationMemberEndpoint):
         return Response(context, status=200)
 
 
+class SCIMListResponseDict(TypedDict):
+    schemas: List[str]
+    totalResults: int
+    startIndex: int
+    itemsPerPage: int
+    Resources: List[OrganizationMemberSCIMSerializerResponse]
+
+
 @control_silo_endpoint
 class OrganizationSCIMMemberIndex(SCIMEndpoint):
     permission_classes = (OrganizationSCIMMemberPermission,)
@@ -368,8 +377,8 @@ class OrganizationSCIMMemberIndex(SCIMEndpoint):
         parameters=[GlobalParams.ORG_SLUG, SCIMQueryParamSerializer],
         request=None,
         responses={
-            200: scim_response_envelope(
-                "SCIMMemberIndexResponse", OrganizationMemberSCIMSerializerResponse
+            200: inline_sentry_response_serializer(
+                "SCIMListResponseEnvelopeSCIMMemberIndexResponse", SCIMListResponseDict
             ),
             401: RESPONSE_UNAUTHORIZED,
             403: RESPONSE_FORBIDDEN,

+ 13 - 3
src/sentry/scim/endpoints/teams.py

@@ -1,5 +1,6 @@
 import logging
 import re
+from typing import List
 
 import sentry_sdk
 from django.db import IntegrityError, transaction
@@ -9,6 +10,7 @@ from rest_framework import serializers
 from rest_framework.exceptions import ParseError
 from rest_framework.request import Request
 from rest_framework.response import Response
+from typing_extensions import TypedDict
 
 from sentry import audit_log
 from sentry.api.base import control_silo_endpoint
@@ -29,6 +31,7 @@ from sentry.apidocs.constants import (
 )
 from sentry.apidocs.examples.scim_examples import SCIMExamples
 from sentry.apidocs.parameters import GlobalParams, SCIMParams
+from sentry.apidocs.utils import inline_sentry_response_serializer
 from sentry.models import OrganizationMember, OrganizationMemberTeam, Team, TeamStatus
 from sentry.utils import json, metrics
 from sentry.utils.cursors import SCIMCursor
@@ -49,7 +52,6 @@ from .utils import (
     SCIMFilterError,
     SCIMQueryParamSerializer,
     parse_filter_conditions,
-    scim_response_envelope,
 )
 
 delete_logger = logging.getLogger("sentry.deletions.api")
@@ -82,6 +84,14 @@ def _team_expand(excluded_attributes):
     return None if "members" in excluded_attributes else ["members"]
 
 
+class SCIMListResponseDict(TypedDict):
+    schemas: List[str]
+    totalResults: int
+    startIndex: int
+    itemsPerPage: int
+    Resources: List[OrganizationTeamSCIMSerializerResponse]
+
+
 @extend_schema(tags=["SCIM"])
 @control_silo_endpoint
 class OrganizationSCIMTeamIndex(SCIMEndpoint, OrganizationTeamsEndpoint):
@@ -99,8 +109,8 @@ class OrganizationSCIMTeamIndex(SCIMEndpoint, OrganizationTeamsEndpoint):
         parameters=[GlobalParams.ORG_SLUG, SCIMQueryParamSerializer],
         request=None,
         responses={
-            200: scim_response_envelope(
-                "SCIMTeamIndexResponse", OrganizationTeamSCIMSerializerResponse
+            200: inline_sentry_response_serializer(
+                "SCIMListResponseEnvelopeSCIMTeamIndexResponse", SCIMListResponseDict
             ),
             401: RESPONSE_UNAUTHORIZED,
             403: RESPONSE_FORBIDDEN,

+ 0 - 22
src/sentry/scim/endpoints/utils.py

@@ -1,12 +1,9 @@
-from typing import Dict, List
-
 import sentry_sdk
 from drf_spectacular.utils import extend_schema
 from rest_framework import serializers
 from rest_framework.exceptions import APIException, ParseError
 from rest_framework.negotiation import BaseContentNegotiation
 from rest_framework.request import Request
-from typing_extensions import TypedDict
 
 from sentry.api.bases.organization import OrganizationEndpoint, OrganizationPermission
 from sentry.models import AuthProvider
@@ -30,25 +27,6 @@ class SCIMFilterError(ValueError):
     pass
 
 
-def scim_response_envelope(name, envelope):
-    from sentry.apidocs.utils import inline_sentry_response_serializer
-
-    class SCIMListResponseEnvelope(SCIMListResponseDict):
-        Resources: List[envelope]
-
-    return inline_sentry_response_serializer(
-        f"SCIMListResponseEnvelope{name}", SCIMListResponseEnvelope
-    )
-
-
-class SCIMListResponseDict(TypedDict):
-    schemas: List[str]
-    totalResults: int
-    startIndex: int
-    itemsPerPage: int
-    Resources: List[Dict]
-
-
 class SCIMClientNegotiation(BaseContentNegotiation):
     # SCIM uses the content type "application/json+scim"
     # which is just json for our purposes.

+ 4 - 2
tests/sentry/utils/kvstore/test_common.py

@@ -1,7 +1,9 @@
+from __future__ import annotations
+
 import itertools
 from dataclasses import dataclass
 from datetime import timedelta
-from typing import Iterator, Tuple
+from typing import Generic, Iterator, Tuple
 
 import pytest
 
@@ -9,7 +11,7 @@ from sentry.utils.kvstore.abstract import K, KVStorage, V
 
 
 @dataclass
-class Properties:
+class Properties(Generic[K, V]):
     store: KVStorage[K, V]
     keys: Iterator[K]
     values: Iterator[V]