Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mypy): fix mypy errors for src/sentry/api/serializers/models (part 1) #53905

Merged
merged 8 commits into from
Aug 3, 2023
6 changes: 0 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -338,19 +338,15 @@ module = [
"sentry.api.permissions",
"sentry.api.serializers.models",
"sentry.api.serializers.models.alert_rule",
"sentry.api.serializers.models.alert_rule_trigger",
"sentry.api.serializers.models.auth_provider",
"sentry.api.serializers.models.authenticator",
"sentry.api.serializers.models.commit",
"sentry.api.serializers.models.dashboard",
"sentry.api.serializers.models.discoversavedquery",
"sentry.api.serializers.models.doc_integration",
"sentry.api.serializers.models.environment",
"sentry.api.serializers.models.event",
"sentry.api.serializers.models.external_actor",
"sentry.api.serializers.models.group",
"sentry.api.serializers.models.group_stream",
"sentry.api.serializers.models.grouprelease",
"sentry.api.serializers.models.incident",
"sentry.api.serializers.models.integration",
"sentry.api.serializers.models.integration_feature",
Expand All @@ -360,11 +356,9 @@ module = [
"sentry.api.serializers.models.plugin",
"sentry.api.serializers.models.project",
"sentry.api.serializers.models.projectcodeowners",
"sentry.api.serializers.models.release_file",
"sentry.api.serializers.models.role",
"sentry.api.serializers.models.rule",
"sentry.api.serializers.models.sentry_app",
"sentry.api.serializers.models.tagvalue",
"sentry.api.serializers.models.team",
"sentry.api.serializers.models.user",
"sentry.api.serializers.models.user_identity_config",
Expand Down
5 changes: 3 additions & 2 deletions src/sentry/api/serializers/models/alert_rule_trigger.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import defaultdict
from typing import DefaultDict, Dict, List

from django.db.models import prefetch_related_objects

Expand All @@ -17,7 +18,7 @@ def get_attrs(self, item_list, user, **kwargs):
prefetch_related_objects(item_list, "alert_rule")

triggers = {item.id: item for item in item_list}
result = defaultdict(dict)
result: DefaultDict[str, Dict[str, List[str]]] = defaultdict(dict)

actions = AlertRuleTriggerAction.objects.filter(alert_rule_trigger__in=item_list).order_by(
"id"
Expand Down Expand Up @@ -49,7 +50,7 @@ def serialize(self, obj, attrs, user, **kwargs):
class DetailedAlertRuleTriggerSerializer(AlertRuleTriggerSerializer):
def get_attrs(self, item_list, user, **kwargs):
triggers = {item.id: item for item in item_list}
result = defaultdict(dict)
result: DefaultDict[str, Dict[str, List[str]]] = defaultdict(dict)
for trigger_id, project_slug in AlertRuleTriggerExclusion.objects.filter(
alert_rule_trigger__in=item_list
).values_list("alert_rule_trigger_id", "query_subscription__project__slug"):
Expand Down
4 changes: 2 additions & 2 deletions src/sentry/api/serializers/models/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from typing import Mapping

from sentry.api.serializers import Serializer, register, serialize
from sentry.api.serializers.models.release import Author, CommitAuthor, get_users_for_authors
from sentry.models import Commit, PullRequest, Repository
from sentry.api.serializers.models.release import Author, get_users_for_authors
from sentry.models import Commit, CommitAuthor, PullRequest, Repository


def get_users_for_commits(item_list, user=None) -> Mapping[str, Author]:
Expand Down
3 changes: 2 additions & 1 deletion src/sentry/api/serializers/models/discoversavedquery.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import defaultdict
from typing import DefaultDict, Dict

from sentry.api.serializers import Serializer, register
from sentry.constants import ALL_ACCESS_PROJECTS
Expand All @@ -10,7 +11,7 @@
@register(DiscoverSavedQuery)
class DiscoverSavedQuerySerializer(Serializer):
def get_attrs(self, item_list, user):
result = defaultdict(lambda: {"created_by": {}})
result: DefaultDict[str, Dict] = defaultdict(lambda: {"created_by": {}})

service_serialized = user_service.serialize_many(
filter={
Expand Down
5 changes: 3 additions & 2 deletions src/sentry/api/serializers/models/grouprelease.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import namedtuple
from datetime import timedelta
from typing import Dict, List

from django.utils import timezone

Expand Down Expand Up @@ -54,7 +55,7 @@ def get_attrs(self, item_list, user):
else None
)

items = {}
items: Dict[str, List[str]] = {}
for item in item_list:
items.setdefault(item.group_id, []).append(item.id)
attrs[item]["stats"] = {}
Expand All @@ -64,7 +65,7 @@ def get_attrs(self, item_list, user):
since = self.since or until - (segments * interval)

try:
stats = tsdb.get_frequency_series(
stats = tsdb.backend.get_frequency_series(
model=TSDBModel.frequent_releases_by_group,
items=items,
start=since,
Expand Down
3 changes: 1 addition & 2 deletions src/sentry/api/serializers/models/release_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def decode_release_file_id(id: str):
except ValueError:
decoded = urlsafe_b64decode(id).decode()
dist, url = decoded.split("_", 1)
dist = dist or None
return dist, url
return dist or None, url


@register(ReleaseFile)
Expand Down
10 changes: 5 additions & 5 deletions src/sentry/api/serializers/models/tagvalue.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Dict

from sentry.api.serializers import Serializer, serialize
from sentry.models import EventUser
from sentry.search.utils import convert_user_tag_to_query
Expand All @@ -21,11 +23,9 @@ def get_attrs(self, item_list, user):
return result

def serialize(self, obj, attrs, user):
if not attrs["user"]:
result = {"id": None}
else:
result = serialize(attrs["user"], user)

result: Dict[str, Any] = (
{"id": None} if not attrs["user"] else serialize(attrs["user"], user)
)
query = convert_user_tag_to_query("user", obj.value)
if query:
result["query"] = query
Expand Down
Loading