Skip to content

Commit 3d4d43e

Browse files
fix: handle TypeError exposed by simple history logic (#5476)
1 parent a0d4061 commit 3d4d43e

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

api/core/models.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ class _BaseHistoricalModel(models.Model):
8383
class Meta:
8484
abstract = True
8585

86-
def get_change_details(self) -> typing.Optional[typing.List[ModelChange]]: # type: ignore[return]
87-
if self.history_type == "~": # type: ignore[attr-defined]
86+
def get_change_details(self) -> typing.List[ModelChange]:
87+
# Note that self.prev_record should never be None when self.history_type == "~" but we have
88+
# seen rare cases in Sentry which cause an unhandled exception, so we instead handle the case
89+
# and fall back to just return an empty list.
90+
if self.history_type == "~" and self.prev_record is not None: # type: ignore[attr-defined]
8891
return [
8992
change
9093
for change in self.diff_against(self.prev_record).changes # type: ignore[attr-defined]
@@ -96,11 +99,10 @@ def get_change_details(self) -> typing.Optional[typing.List[ModelChange]]: # ty
9699
for key, value in self.instance.to_dict().items() # type: ignore[attr-defined]
97100
if key not in self._change_details_excluded_fields # type: ignore[attr-defined]
98101
]
99-
elif self.history_type == "-": # type: ignore[attr-defined]
100-
# Ignore deletes because they get painful due to cascade deletes
101-
# Maybe we can resolve this in the future but for now it's not
102-
# critical.
103-
return []
102+
103+
# Note that we ignore deletes because they get painful due to cascade deletes. Maybe we can
104+
# resolve this in the future but for now it's not critical.
105+
return []
104106

105107

106108
def base_historical_model_factory(

api/features/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ def validate(self, attrs): # type: ignore[no-untyped-def]
567567
)
568568

569569
mv_values = attrs.get("multivariate_feature_state_values", [])
570-
if sum([v["percentage_allocation"] for v in mv_values]) > 100:
570+
if sum([v.get("percentage_allocation", 0) for v in mv_values]) > 100:
571571
raise serializers.ValidationError(
572572
"Multivariate percentage values exceed 100%."
573573
)

0 commit comments

Comments
 (0)