Skip to content

Commit

Permalink
appeasing mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarticus committed Jul 18, 2024
1 parent ef262d8 commit 6f6836f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion frontend/src/scenes/surveys/surveyActivityDescriber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ export function describeQuestionChanges(before: SurveyQuestion, after: SurveyQue
]
: []

const specificChanges = match([before, after] as const)
const specificChanges = match([before, after])
.with([{ type: SurveyQuestionType.Link }, { type: SurveyQuestionType.Link }], describeLinkChanges)
.with([{ type: SurveyQuestionType.Rating }, { type: SurveyQuestionType.Rating }], describeRatingChanges)
.with(
Expand Down
2 changes: 1 addition & 1 deletion posthog/api/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def update(self, instance: Survey, validated_data):
Change(
type="Survey",
field="targeting_flag_filters",
action="updated",
action="changed",
before=existing_targeting_flag_filters,
after=new_filters,
)
Expand Down
4 changes: 3 additions & 1 deletion posthog/api/test/test_survey.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import re
from datetime import datetime, timedelta
from typing import Any
from unittest.mock import ANY

import pytest
from django.core.cache import cache
from django.test.client import Client

from freezegun.api import freeze_time
from posthog.api.survey import nh3_clean_with_allow_list
from posthog.models.cohort.cohort import Cohort
Expand Down Expand Up @@ -1188,7 +1190,7 @@ def test_update_survey_targeting_flag_filters_records_activity(self):
questions=[{"type": "open", "question": "What's a hedgehog?"}],
)

new_filters = {
new_filters: dict[str, Any] = {
"targeting_flag_filters": {
"groups": [
{"variant": None, "properties": [], "rollout_percentage": 69},
Expand Down
8 changes: 6 additions & 2 deletions posthog/models/activity_logging/activity_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import structlog
from django.core.paginator import Paginator
from django.core.exceptions import ObjectDoesNotExist

from django.db import models
from django.utils import timezone
from django.conf import settings
Expand Down Expand Up @@ -241,16 +243,18 @@ def _read_through_relation(relation: models.Manager) -> list[Union[dict, str]]:
return described_models


def safely_get_field_value(instance: models.Model, field: str):
def safely_get_field_value(instance: models.Model | None, field: str):
"""Helper function to get the value of a field, handling related objects and exceptions."""
if instance is None:
return None
try:
value = getattr(instance, field, None)
if isinstance(value, models.Manager):
value = _read_through_relation(value)
# If the field is a related field and the related object has been deleted, this will raise an ObjectDoesNotExist
# exception. We catch this exception and return None, since the related object has been deleted, and we
# don't need any additional information about it other than the fact that it was deleted.
except models.ObjectDoesNotExist:
except ObjectDoesNotExist:
value = None
return value

Expand Down

0 comments on commit 6f6836f

Please sign in to comment.