Skip to content

Commit 1a68ffa

Browse files
ref: fix some types for sentry.api (#72641)
fixes errors when BaseManager becomes typed <!-- Describe your PR here. -->
1 parent 4195f82 commit 1a68ffa

17 files changed

+75
-33
lines changed

src/sentry/api/authentication.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,13 @@ def authenticate_credentials(self, userid, password, request=None):
243243
if password:
244244
return None
245245

246+
key: ApiKeyReplica | ApiKey
246247
if SiloMode.get_current_mode() == SiloMode.REGION:
247-
key = ApiKeyReplica.objects.filter(key=userid).last()
248-
if key is None:
248+
key_replica = ApiKeyReplica.objects.filter(key=userid).last()
249+
if key_replica is None:
249250
raise AuthenticationFailed("API key is not valid")
251+
else:
252+
key = key_replica
250253
else:
251254
try:
252255
key = ApiKey.objects.get_from_cache(key=userid)
@@ -452,9 +455,9 @@ def accepts_auth(self, auth: list[bytes]) -> bool:
452455
return token_str.startswith(SENTRY_ORG_AUTH_TOKEN_PREFIX)
453456

454457
def authenticate_token(self, request: Request, token_str: str) -> tuple[Any, Any]:
455-
token = None
456458
token_hashed = hash_token(token_str)
457459

460+
token: OrgAuthTokenReplica | OrgAuthToken
458461
if SiloMode.get_current_mode() == SiloMode.REGION:
459462
try:
460463
token = OrgAuthTokenReplica.objects.get(

src/sentry/api/endpoints/artifact_bundles.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ def delete(self, request: Request, project) -> Response:
135135
if bundle_id:
136136
error = None
137137

138-
project_artifact_bundles = ProjectArtifactBundle.objects.filter(
138+
project_artifact_bundles_qs = ProjectArtifactBundle.objects.filter(
139139
organization_id=project.organization_id,
140140
artifact_bundle__bundle_id=bundle_id,
141141
).select_related("artifact_bundle")
142142
# We group the bundles by their id, since we might have multiple bundles with the same bundle_id due to a
143143
# problem that was fixed in https://github.com/getsentry/sentry/pull/49836.
144144
grouped_bundles = defaultdict(list)
145-
for project_artifact_bundle in project_artifact_bundles:
145+
for project_artifact_bundle in project_artifact_bundles_qs:
146146
grouped_bundles[project_artifact_bundle.artifact_bundle].append(
147147
project_artifact_bundle
148148
)

src/sentry/api/endpoints/artifact_lookup.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ def download_file(self, download_id, project: Project):
6262
)
6363
return HttpResponse({"Too many download requests"}, status=429)
6464

65-
file = None
65+
file_m: ArtifactBundle | ReleaseFile | None = None
6666
if ty == "artifact_bundle":
67-
file = (
67+
file_m = (
6868
ArtifactBundle.objects.filter(
6969
id=ty_id,
7070
projectartifactbundle__project_id=project.id,
@@ -76,16 +76,16 @@ def download_file(self, download_id, project: Project):
7676
elif ty == "release_file":
7777
# NOTE: `ReleaseFile` does have a `project_id`, but that seems to
7878
# be always empty, so using the `organization_id` instead.
79-
file = (
79+
file_m = (
8080
ReleaseFile.objects.filter(id=ty_id, organization_id=project.organization.id)
8181
.select_related("file")
8282
.first()
8383
)
8484
metrics.incr("sourcemaps.download.release_file")
8585

86-
if file is None:
86+
if file_m is None:
8787
raise Http404
88-
file = file.file
88+
file = file_m.file
8989

9090
try:
9191
fp = file.getfile()

src/sentry/api/endpoints/group_current_release.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ def _get_current_release(self, group, environments):
2424
"release_id", flat=True
2525
)
2626

27-
release_envs = ReleaseEnvironment.objects.filter(
27+
release_envs_qs = ReleaseEnvironment.objects.filter(
2828
release_id__in=release_projects,
2929
organization_id=group.project.organization_id,
3030
)
3131
if environments:
32-
release_envs = release_envs.filter(environment_id__in=[env.id for env in environments])
33-
release_envs = release_envs.order_by("-first_seen").values_list("release_id", flat=True)
32+
release_envs_qs = release_envs_qs.filter(
33+
environment_id__in=[env.id for env in environments]
34+
)
35+
release_envs = release_envs_qs.order_by("-first_seen").values_list("release_id", flat=True)
3436

3537
group_releases = GroupRelease.objects.filter(
3638
group_id=group.id,

src/sentry/api/endpoints/group_details.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,11 @@ def get(self, request: Request, group) -> Response:
230230
data.update({"integrationIssues": integration_issues})
231231

232232
if "sentryAppIssues" in expand:
233-
external_issues = PlatformExternalIssue.objects.filter(group_id=group.id)
233+
platform_external_issues = PlatformExternalIssue.objects.filter(group_id=group.id)
234234
sentry_app_issues = serialize(
235-
list(external_issues), request, serializer=PlatformExternalIssueSerializer()
235+
list(platform_external_issues),
236+
request,
237+
serializer=PlatformExternalIssueSerializer(),
236238
)
237239
data.update({"sentryAppIssues": sentry_app_issues})
238240

src/sentry/api/endpoints/organization_artifactbundle_assemble.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ def post(self, request: Request, organization) -> Response:
6060
if len(projects) == 0:
6161
return Response({"error": "You need to specify at least one project"}, status=400)
6262

63-
project_ids = Project.objects.filter(
64-
organization=organization, status=ObjectStatus.ACTIVE, slug__in=projects
65-
).values_list("id", flat=True)
63+
project_ids = list(
64+
Project.objects.filter(
65+
organization=organization, status=ObjectStatus.ACTIVE, slug__in=projects
66+
).values_list("id", flat=True)
67+
)
6668
if len(project_ids) != len(projects):
6769
return Response({"error": "One or more projects are invalid"}, status=400)
6870

src/sentry/api/endpoints/organization_codeowners_associations.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def get(self, request: Request, organization: Organization):
4747
)
4848
result = {}
4949
for pco in project_code_owners:
50+
assert pco.raw is not None # XXX: model field `raw` is nullable? seems wrong?
5051
associations, errors = validate_codeowners_associations(pco.raw, pco.project)
5152
result[pco.project.slug] = {"associations": associations, "errors": errors}
5253
return self.respond(result, status=status.HTTP_200_OK)

src/sentry/api/endpoints/organization_release_meta.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from __future__ import annotations
2+
13
from collections import defaultdict
4+
from typing import TypedDict
25

36
from rest_framework.request import Request
47
from rest_framework.response import Response
@@ -15,6 +18,15 @@
1518
from sentry.models.releases.release_project import ReleaseProject
1619

1720

21+
class _ProjectDict(TypedDict):
22+
id: int
23+
slug: str | None
24+
name: str
25+
newGroups: int | None
26+
platform: str | None
27+
platforms: list[str]
28+
29+
1830
@region_silo_endpoint
1931
class OrganizationReleaseMetaEndpoint(OrganizationReleasesBaseEndpoint):
2032
publish_status = {
@@ -70,7 +82,7 @@ def get(self, request: Request, organization, version) -> Response:
7082
platforms_by_project[project_id].append(platform)
7183

7284
# This must match what is returned from the `Release` serializer
73-
projects = [
85+
projects: list[_ProjectDict] = [
7486
{
7587
"id": pr["project__id"],
7688
"slug": pr["project__slug"],

src/sentry/api/endpoints/prompts_activity.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ def get(self, request: Request, **kwargs) -> Response:
6262
condition = Q(feature=feature, **filters)
6363
conditions = condition if conditions is None else (conditions | condition)
6464

65-
result = PromptsActivity.objects.filter(conditions, user_id=request.user.id)
66-
featuredata = {k.feature: k.data for k in result}
65+
result_qs = PromptsActivity.objects.filter(conditions, user_id=request.user.id)
66+
featuredata = {k.feature: k.data for k in result_qs}
6767
if len(features) == 1:
68-
result = result.first()
68+
result = result_qs.first()
6969
data = None if result is None else result.data
7070
return Response({"data": data, "features": featuredata})
7171
else:

src/sentry/api/helpers/teams.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def get_teams(request, organization, teams=None):
2222
# do normal teams lookup based on request params
2323
requested_teams = set(request.GET.getlist("team", []) if teams is None else teams)
2424

25-
verified_ids = set()
25+
verified_ids: set[int] = set()
2626

2727
if "myteams" in requested_teams:
2828
requested_teams.remove("myteams")

0 commit comments

Comments
 (0)