Skip to content

Commit 28c52c8

Browse files
ref: type custom managers as ClassVars (#59138)
this is needed to upgrade django-stubs / mypy <!-- Describe your PR here. -->
1 parent fca58c8 commit 28c52c8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+169
-132
lines changed

src/sentry/db/models/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Any, Callable, Iterable, Mapping, TypeVar
3+
from typing import Any, Callable, ClassVar, Iterable, Mapping, TypeVar
44

55
from django.apps.config import AppConfig
66
from django.db import models
@@ -50,7 +50,7 @@ class Meta:
5050
__relocation_scope__: RelocationScope | set[RelocationScope]
5151
__relocation_dependencies__: set[str]
5252

53-
objects: BaseManager[Self] = BaseManager()
53+
objects: ClassVar[BaseManager[Self]] = BaseManager()
5454

5555
update = update
5656

src/sentry/discover/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import ClassVar
2+
13
from django.db import models, router, transaction
24
from django.db.models import Q, UniqueConstraint
35
from django.utils import timezone
@@ -87,7 +89,7 @@ def set_projects(self, project_ids):
8789
)
8890

8991

90-
class TeamKeyTransactionModelManager(BaseManager):
92+
class TeamKeyTransactionModelManager(BaseManager["TeamKeyTransaction"]):
9193
@staticmethod
9294
def __schedule_invalidate_project_config_transaction_commit(instance, trigger):
9395
try:
@@ -144,7 +146,7 @@ class TeamKeyTransaction(Model):
144146
organization = FlexibleForeignKey("sentry.Organization")
145147

146148
# Custom Model Manager required to override post_save/post_delete method
147-
objects = TeamKeyTransactionModelManager()
149+
objects: ClassVar[TeamKeyTransactionModelManager] = TeamKeyTransactionModelManager()
148150

149151
class Meta:
150152
app_label = "sentry"

src/sentry/event_manager.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,12 @@ def save(
423423

424424
projects = {project.id: project}
425425

426-
job = {"data": self._data, "project_id": project.id, "raw": raw, "start_time": start_time}
426+
job: dict[str, Any] = {
427+
"data": self._data,
428+
"project_id": project.id,
429+
"raw": raw,
430+
"start_time": start_time,
431+
}
427432

428433
# After calling _pull_out_data we get some keys in the job like the platform
429434
with sentry_sdk.start_span(op="event_manager.save.pull_out_data"):

src/sentry/models/activity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from enum import Enum
4-
from typing import TYPE_CHECKING, Any, Mapping, Optional, Sequence
4+
from typing import TYPE_CHECKING, Any, ClassVar, Mapping, Optional, Sequence
55

66
from django.conf import settings
77
from django.db import models
@@ -100,7 +100,7 @@ class Activity(Model):
100100
datetime = models.DateTimeField(default=timezone.now)
101101
data: models.Field[dict[str, Any], dict[str, Any]] = GzippedDictField(null=True)
102102

103-
objects: ActivityManager = ActivityManager()
103+
objects: ClassVar[ActivityManager] = ActivityManager()
104104

105105
class Meta:
106106
app_label = "sentry"

src/sentry/models/apiapplication.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import secrets
2-
from typing import List
2+
from typing import ClassVar, List
33
from urllib.parse import urlparse
44

55
import petname
66
from django.db import models, router, transaction
77
from django.utils import timezone
88
from django.utils.translation import gettext_lazy as _
9+
from typing_extensions import Self
910

1011
from sentry.backup.scopes import RelocationScope
1112
from sentry.db.models import (
@@ -62,7 +63,7 @@ class ApiApplication(Model):
6263

6364
date_added = models.DateTimeField(default=timezone.now)
6465

65-
objects = BaseManager(cache_fields=("client_id",))
66+
objects: ClassVar[BaseManager[Self]] = BaseManager(cache_fields=("client_id",))
6667

6768
class Meta:
6869
app_label = "sentry"

src/sentry/models/apikey.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import secrets
2+
from typing import ClassVar
23

34
from django.db import models
45
from django.utils import timezone
56
from django.utils.translation import gettext_lazy as _
7+
from typing_extensions import Self
68

79
from sentry.backup.scopes import RelocationScope
810
from sentry.db.models import (
@@ -41,7 +43,7 @@ class ApiKey(ReplicatedControlModel, HasApiScopes):
4143
date_added = models.DateTimeField(default=timezone.now)
4244
allowed_origins = models.TextField(blank=True, null=True)
4345

44-
objects = BaseManager(cache_fields=("key",))
46+
objects: ClassVar[BaseManager[Self]] = BaseManager(cache_fields=("key",))
4547

4648
class Meta:
4749
app_label = "sentry"

src/sentry/models/apitoken.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import secrets
44
from datetime import timedelta
5-
from typing import Collection, Optional, Tuple
5+
from typing import ClassVar, Collection, Optional, Tuple
66

77
from django.db import models, router, transaction
88
from django.utils import timezone
@@ -42,7 +42,9 @@ class ApiToken(ReplicatedControlModel, HasApiScopes):
4242
expires_at = models.DateTimeField(null=True, default=default_expiration)
4343
date_added = models.DateTimeField(default=timezone.now)
4444

45-
objects = ControlOutboxProducingManager["ApiToken"](cache_fields=("token",))
45+
objects: ClassVar[ControlOutboxProducingManager[ApiToken]] = ControlOutboxProducingManager(
46+
cache_fields=("token",)
47+
)
4648

4749
class Meta:
4850
app_label = "sentry"

src/sentry/models/authenticator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import base64
44
import copy
5-
from typing import Any, List
5+
from typing import Any, ClassVar, List
66

77
from django.db import models
88
from django.utils import timezone
@@ -31,7 +31,7 @@
3131
from sentry.types.region import find_regions_for_user
3232

3333

34-
class AuthenticatorManager(BaseManager):
34+
class AuthenticatorManager(BaseManager["Authenticator"]):
3535
def all_interfaces_for_user(self, user, return_missing=False, ignore_backup=False):
3636
"""Returns a correctly sorted list of all interfaces the user
3737
has enabled. If `return_missing` is set to `True` then all
@@ -154,7 +154,7 @@ class Authenticator(ControlOutboxProducingModel):
154154

155155
config = AuthenticatorConfig()
156156

157-
objects = AuthenticatorManager()
157+
objects: ClassVar[AuthenticatorManager] = AuthenticatorManager()
158158

159159
class AlreadyEnrolled(Exception):
160160
pass

src/sentry/models/avatars/sentry_app_avatar.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from collections import defaultdict
44
from enum import Enum
5-
from typing import TYPE_CHECKING, List
5+
from typing import TYPE_CHECKING, ClassVar, List
66

77
from django.db import models
88

@@ -24,7 +24,7 @@ def get_choices(cls):
2424
return tuple((_.value, _.name.lower()) for _ in SentryAppAvatarTypes)
2525

2626

27-
class SentryAppAvatarManager(BaseManager):
27+
class SentryAppAvatarManager(BaseManager["SentryAppAvatar"]):
2828
def get_by_apps_as_dict(self, sentry_apps: List[SentryApp]):
2929
"""
3030
Returns a dict mapping sentry_app_id (key) to List[SentryAppAvatar] (value)
@@ -43,7 +43,7 @@ class SentryAppAvatar(ControlAvatarBase):
4343
and specifies which type of logo it is.
4444
"""
4545

46-
objects = SentryAppAvatarManager()
46+
objects: ClassVar[SentryAppAvatarManager] = SentryAppAvatarManager()
4747

4848
AVATAR_TYPES = SentryAppAvatarTypes.get_choices()
4949

src/sentry/models/avatars/user_avatar.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import contextlib
44
from enum import IntEnum
5-
from typing import Any, List, Tuple
5+
from typing import Any, ClassVar, List, Tuple
66

77
from django.db import models, router, transaction
8+
from typing_extensions import Self
89

910
from sentry.db.models import BaseManager, FlexibleForeignKey, control_silo_only_model
1011

@@ -44,7 +45,7 @@ class UserAvatar(ControlAvatarBase):
4445
user = FlexibleForeignKey("sentry.User", unique=True, related_name="avatar")
4546
avatar_type = models.PositiveSmallIntegerField(default=0, choices=UserAvatarType.as_choices())
4647

47-
objects = BaseManager(cache_fields=["user"])
48+
objects: ClassVar[BaseManager[Self]] = BaseManager(cache_fields=["user"])
4849

4950
class Meta:
5051
app_label = "sentry"

0 commit comments

Comments
 (0)