Skip to content

Commit 8d255a2

Browse files
ref: fix some typing in hybrid cloud modules (#72635)
fixes most errors in these modules when BaseManager becomes typed <!-- Describe your PR here. -->
1 parent 829f5f7 commit 8d255a2

File tree

13 files changed

+33
-27
lines changed

13 files changed

+33
-27
lines changed

src/sentry/db/models/outboxes.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import contextlib
44
import logging
5-
from collections.abc import Collection, Iterable, Mapping
5+
from collections.abc import Collection, Iterable, Mapping, Sequence
66
from typing import TYPE_CHECKING, Any, Protocol, TypeVar
77

88
from django.db import connections, router, transaction
@@ -80,7 +80,7 @@ class RegionOutboxProducingManager(BaseManager[_RM]):
8080
Provides bulk update and delete methods that respect outbox creation.
8181
"""
8282

83-
def bulk_create(self, objs: Iterable[_RM], *args: Any, **kwds: Any) -> Collection[_RM]:
83+
def bulk_create(self, objs: Iterable[_RM], *args: Any, **kwds: Any) -> list[_RM]:
8484
from sentry.models.outbox import outbox_context
8585

8686
tuple_of_objs: tuple[_RM, ...] = tuple(objs)
@@ -107,7 +107,9 @@ def bulk_create(self, objs: Iterable[_RM], *args: Any, **kwds: Any) -> Collectio
107107
type(outboxes[0]).objects.bulk_create(outboxes)
108108
return super().bulk_create(tuple_of_objs, *args, **kwds)
109109

110-
def bulk_update(self, objs: Iterable[_RM], fields: list[str], *args: Any, **kwds: Any) -> Any:
110+
def bulk_update(
111+
self, objs: Iterable[_RM], fields: Sequence[str], *args: Any, **kwds: Any
112+
) -> Any:
111113
from sentry.models.outbox import outbox_context
112114

113115
tuple_of_objs: tuple[_RM, ...] = tuple(objs)
@@ -262,7 +264,7 @@ class ControlOutboxProducingManager(BaseManager[_CM]):
262264
Provides bulk update and delete methods that respect outbox creation.
263265
"""
264266

265-
def bulk_create(self, objs: Iterable[_CM], *args: Any, **kwds: Any) -> Collection[_CM]:
267+
def bulk_create(self, objs: Iterable[_CM], *args: Any, **kwds: Any) -> list[_CM]:
266268
from sentry.models.outbox import outbox_context
267269

268270
tuple_of_objs: tuple[_CM, ...] = tuple(objs)
@@ -289,7 +291,9 @@ def bulk_create(self, objs: Iterable[_CM], *args: Any, **kwds: Any) -> Collectio
289291
type(outboxes[0]).objects.bulk_create(outboxes)
290292
return super().bulk_create(tuple_of_objs, *args, **kwds)
291293

292-
def bulk_update(self, objs: Iterable[_CM], fields: list[str], *args: Any, **kwds: Any) -> Any:
294+
def bulk_update(
295+
self, objs: Iterable[_CM], fields: Sequence[str], *args: Any, **kwds: Any
296+
) -> Any:
293297
from sentry.models.outbox import outbox_context
294298

295299
tuple_of_objs: tuple[_CM, ...] = tuple(objs)

src/sentry/hybridcloud/models/webhookpayload.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import datetime
4-
from typing import Any, Self
4+
from typing import Any
55

66
from django.db import models
77
from django.http import HttpRequest
@@ -79,7 +79,7 @@ def create_from_request(
7979
identifier: int | str,
8080
request: HttpRequest,
8181
integration_id: int | None = None,
82-
) -> Self:
82+
) -> WebhookPayload:
8383
metrics.incr("hybridcloud.deliver_webhooks.saved")
8484
return cls.objects.create(
8585
mailbox_name=f"{provider}:{identifier}",

src/sentry/hybridcloud/rpc_services/region_organization_provisioning/impl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def _get_previously_provisioned_org_and_validate(
9090
if matching_organizations_qs.count() > 1:
9191
raise PreProvisionCheckException("Multiple conflicting organizations found")
9292

93-
matching_org: Organization = matching_organizations_qs.first()
93+
matching_org = matching_organizations_qs.get()
9494

9595
try:
9696
provisioning_user_is_org_owner = (

src/sentry/receivers/outbox/control.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ def process_sentry_app_updates(object_identifier: int, region_name: str, **kwds:
5757
)
5858
# There isn't a constraint on org : sentryapp so we have to handle lists
5959
install_map: dict[int, list[int]] = defaultdict(list)
60-
for row in install_query:
61-
install_map[row["organization_id"]].append(row["id"])
60+
for install_row in install_query:
61+
install_map[install_row["organization_id"]].append(install_row["id"])
6262

6363
# Clear application_id cache
6464
region_caching_service.clear_key(
@@ -70,8 +70,8 @@ def process_sentry_app_updates(object_identifier: int, region_name: str, **kwds:
7070
region_query = OrganizationMapping.objects.filter(
7171
organization_id__in=list(install_map.keys()), region_name=region_name
7272
).values("organization_id")
73-
for row in region_query:
74-
installs = install_map[row["organization_id"]]
73+
for region_row in region_query:
74+
installs = install_map[region_row["organization_id"]]
7575
for install_id in installs:
7676
region_caching_service.clear_key(
7777
key=get_installation.key_from(install_id), region_name=region_name

src/sentry/services/hybrid_cloud/app/impl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class _AppServiceFilterQuery(
166166
):
167167
def base_query(self, select_related: bool = True) -> QuerySet[SentryAppInstallation]:
168168
if not select_related:
169-
return SentryAppInstallation.objects
169+
return SentryAppInstallation.objects.all()
170170
return SentryAppInstallation.objects.select_related("sentry_app")
171171

172172
def filter_arg_validator(

src/sentry/services/hybrid_cloud/identity/impl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def apply_filters(
109109
return query
110110

111111
def base_query(self, select_related: bool = True) -> QuerySet[Identity]:
112-
return Identity.objects
112+
return Identity.objects.all()
113113

114114
def filter_arg_validator(self) -> Callable[[IdentityFilterArgs], str | None]:
115115
return self._filter_has_any_key_validator(*IdentityFilterArgs.__annotations__.keys())

src/sentry/services/hybrid_cloud/integration/impl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ def get_integration_external_projects(
471471
except OrganizationIntegration.DoesNotExist:
472472
return []
473473

474-
iep_kwargs = {"organization_integration_id": oi.id}
474+
iep_kwargs: dict[str, Any] = {"organization_integration_id": oi.id}
475475
if external_id is not None:
476476
iep_kwargs["external_id"] = external_id
477477
external_projects = IntegrationExternalProject.objects.filter(**iep_kwargs)

src/sentry/services/hybrid_cloud/organization/model.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# from __future__ import annotations
33
# in modules such as this one where hybrid cloud data models or service classes are
44
# defined, because we want to reflect on type annotations and avoid forward references.
5-
from collections.abc import Mapping, Sequence
5+
from collections.abc import Iterable, Mapping, Sequence
66
from datetime import datetime
77
from enum import IntEnum
88
from typing import Any, TypedDict
@@ -255,7 +255,7 @@ def get_owners(self) -> Sequence[RpcUser]:
255255
from sentry.services.hybrid_cloud.user.service import user_service
256256

257257
if SiloMode.get_current_mode() == SiloMode.CONTROL:
258-
owners = OrganizationMemberMapping.objects.filter(
258+
owners: Iterable[int | None] = OrganizationMemberMapping.objects.filter(
259259
organization_id=self.id, role__in=[roles.get_top_dog().id]
260260
).values_list("user_id", flat=True)
261261
else:

src/sentry/services/hybrid_cloud/organization/serial.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ def serialize_rpc_organization(
142142
)
143143

144144
if include_projects:
145-
projects: list[Project] = Project.objects.filter(organization=org)
145+
projects = Project.objects.filter(organization=org)
146146
rpc_org.projects.extend(serialize_project(project) for project in projects)
147147
if include_teams:
148-
teams: list[Team] = Team.objects.filter(organization=org)
148+
teams = Team.objects.filter(organization=org)
149149
rpc_org.teams.extend(serialize_rpc_team(team) for team in teams)
150150

151151
return rpc_org

src/sentry/services/hybrid_cloud/organization_mapping/impl.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,18 @@ def _check_organization_mapping_integrity(
8888
def _upsert_organization_slug_reservation_for_monolith(
8989
self, organization_id: int, mapping_update: RpcOrganizationMappingUpdate
9090
) -> None:
91-
org_slug_reservation_qs = OrganizationSlugReservation.objects.filter(
91+
org_slug_reservation = OrganizationSlugReservation.objects.filter(
9292
organization_id=organization_id
93-
)
94-
if not org_slug_reservation_qs.exists():
93+
).first()
94+
if org_slug_reservation is None:
9595
OrganizationSlugReservation(
9696
region_name=mapping_update.region_name,
9797
slug=mapping_update.slug,
9898
organization_id=organization_id,
9999
user_id=-1,
100100
).save(unsafe_write=True)
101-
elif org_slug_reservation_qs.first().slug != mapping_update.slug:
102-
org_slug_reservation_qs.first().update(slug=mapping_update.slug, unsafe_write=True)
101+
elif org_slug_reservation.slug != mapping_update.slug:
102+
org_slug_reservation.update(slug=mapping_update.slug, unsafe_write=True)
103103

104104
def upsert(self, organization_id: int, update: RpcOrganizationMappingUpdate) -> None:
105105
update_dict: dict[str, Any] = dict(

src/sentry/services/hybrid_cloud/project/impl.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
class DatabaseBackedProjectService(ProjectService):
2525
def get_by_id(self, *, organization_id: int, id: int) -> RpcProject | None:
2626
try:
27-
project = Project.objects.get_from_cache(id=id, organization=organization_id)
27+
project: Project | None = Project.objects.get_from_cache(
28+
id=id, organization=organization_id
29+
)
2830
except ValueError:
2931
project = Project.objects.filter(id=id, organization=organization_id).first()
3032
except Project.DoesNotExist:

src/sentry/services/hybrid_cloud/user/impl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def apply_filters(self, query: QuerySet[User], filters: UserFilterArgs) -> Query
333333

334334
def base_query(self, select_related: bool = True) -> QuerySet[User]:
335335
if not select_related:
336-
return User.objects
336+
return User.objects.all()
337337

338338
return User.objects.extra(
339339
select={

src/sentry/services/hybrid_cloud/user_option/impl.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class _UserOptionFilterQuery(
5757
FilterQueryDatabaseImpl[UserOption, UserOptionFilterArgs, RpcUserOption, None]
5858
):
5959
def base_query(self, select_related: bool = True) -> QuerySet[UserOption]:
60-
return UserOption.objects
60+
return UserOption.objects.all()
6161

6262
def filter_arg_validator(self) -> Callable[[UserOptionFilterArgs], str | None]:
6363
return self._filter_has_any_key_validator("user_ids")

0 commit comments

Comments
 (0)