From 9931195d660fcc6e9e170adac059c7678b86dd5f Mon Sep 17 00:00:00 2001 From: anthony sottile Date: Mon, 29 Jul 2024 10:07:32 -0400 Subject: [PATCH] ref: make create or update typesafe --- src/sentry/models/release.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/sentry/models/release.py b/src/sentry/models/release.py index 53bcfa9646b2c8..2ece9a52ebe0d2 100644 --- a/src/sentry/models/release.py +++ b/src/sentry/models/release.py @@ -5,7 +5,7 @@ import re from collections.abc import Mapping, Sequence from time import time -from typing import ClassVar +from typing import ClassVar, TypedDict import orjson import sentry_sdk @@ -36,6 +36,7 @@ from sentry.locks import locks from sentry.models.activity import Activity from sentry.models.artifactbundle import ArtifactBundle +from sentry.models.commitauthor import CommitAuthor from sentry.models.commitfilechange import CommitFileChange from sentry.models.grouphistory import GroupHistoryStatus, record_group_history from sentry.models.groupinbox import GroupInbox, GroupInboxRemoveAction, remove_group_from_inbox @@ -178,6 +179,12 @@ def get_group_release_version( return release_version or None +class _CommitDataKwargs(TypedDict, total=False): + author: CommitAuthor + message: str + date_added: str + + @region_silo_model class Release(Model): """ @@ -658,7 +665,6 @@ def set_commits(self, commit_list): # TODO(dcramer): this function could use some cleanup/refactoring as it's a bit unwieldy from sentry.models.commit import Commit - from sentry.models.commitauthor import CommitAuthor from sentry.models.group import Group, GroupStatus from sentry.models.grouplink import GroupLink from sentry.models.groupresolution import GroupResolution @@ -752,7 +758,7 @@ def set_commits(self, commit_list): else: author = authors[author_email] - commit_data = {} + commit_data: _CommitDataKwargs = {} # Update/set message and author if they are provided. if author is not None: @@ -768,14 +774,10 @@ def set_commits(self, commit_list): key=data["id"], defaults=commit_data, ) - if not created: - commit_data = { - key: value - for key, value in commit_data.items() - if getattr(commit, key) != value - } - if commit_data: - commit.update(**commit_data) + if not created and any( + getattr(commit, key) != value for key, value in commit_data.items() + ): + commit.update(**commit_data) if author is None: author = commit.author