Skip to content

Commit

Permalink
ref: annotate query type for ReleaseQuerySet
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile-sentry committed Jul 1, 2024
1 parent f2f13a5 commit 586ecf4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/sentry/models/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def _get_cache_key(project_id: int, group_id: int, first: bool) -> str:


class ReleaseModelManager(BaseManager["Release"]):
def get_queryset(self):
def get_queryset(self) -> ReleaseQuerySet:
return ReleaseQuerySet(self.model, using=self._db)

def annotate_prerelease_column(self):
Expand Down
14 changes: 9 additions & 5 deletions src/sentry/models/releases/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections import namedtuple
from collections.abc import Sequence
from dataclasses import dataclass
from typing import TYPE_CHECKING, Self

from django.db import models
from django.db.models import Case, F, Func, Q, Subquery, Value, When
Expand All @@ -17,6 +18,9 @@
from sentry.models.releases.release_project import ReleaseProject
from sentry.utils.numbers import validate_bigint

if TYPE_CHECKING:
from sentry.models.release import Release # noqa: F401

logger = logging.getLogger(__name__)


Expand All @@ -34,7 +38,7 @@ class SemverFilter:
negated: bool = False


class ReleaseQuerySet(BaseQuerySet):
class ReleaseQuerySet(BaseQuerySet["Release"]):
def annotate_prerelease_column(self):
"""
Adds a `prerelease_case` column to the queryset which is used to properly sort
Expand All @@ -60,7 +64,7 @@ def filter_by_semver_build(
build: str,
project_ids: Sequence[int] | None = None,
negated: bool = False,
) -> models.QuerySet:
) -> Self:
"""
Filters released by build. If the passed `build` is a numeric string, we'll filter on
`build_number` and make use of the passed operator.
Expand Down Expand Up @@ -92,7 +96,7 @@ def filter_by_semver(
organization_id: int,
semver_filter: SemverFilter,
project_ids: Sequence[int] | None = None,
) -> models.QuerySet:
) -> Self:
"""
Filters releases based on a based `SemverFilter` instance.
`SemverFilter.version_parts` can contain up to 6 components, which should map
Expand Down Expand Up @@ -138,7 +142,7 @@ def filter_by_stage(
value,
project_ids: Sequence[int] | None = None,
environments: list[str] | None = None,
) -> models.QuerySet:
) -> Self:
from sentry.models.releaseprojectenvironment import ReleaseProjectEnvironment, ReleaseStages
from sentry.search.events.filter import to_list

Expand Down Expand Up @@ -176,7 +180,7 @@ def filter_by_stage(
qs = self.filter(id__in=Subquery(rpes.filter(query).values_list("release_id", flat=True)))
return qs

def order_by_recent(self):
def order_by_recent(self) -> Self:
return self.order_by("-date_added", "-id")

@staticmethod
Expand Down

0 comments on commit 586ecf4

Please sign in to comment.