From 6f6836f0cc8f785bcbeab43ee78fa3176931e4dd Mon Sep 17 00:00:00 2001 From: dylan Date: Thu, 18 Jul 2024 14:49:37 -0700 Subject: [PATCH] appeasing mypy --- frontend/src/scenes/surveys/surveyActivityDescriber.tsx | 2 +- posthog/api/survey.py | 2 +- posthog/api/test/test_survey.py | 4 +++- posthog/models/activity_logging/activity_log.py | 8 ++++++-- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/frontend/src/scenes/surveys/surveyActivityDescriber.tsx b/frontend/src/scenes/surveys/surveyActivityDescriber.tsx index 47c346ab471c3..5841aa6289357 100644 --- a/frontend/src/scenes/surveys/surveyActivityDescriber.tsx +++ b/frontend/src/scenes/surveys/surveyActivityDescriber.tsx @@ -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( diff --git a/posthog/api/survey.py b/posthog/api/survey.py index 48f22ac745e6b..8c18fe6170032 100644 --- a/posthog/api/survey.py +++ b/posthog/api/survey.py @@ -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, ) diff --git a/posthog/api/test/test_survey.py b/posthog/api/test/test_survey.py index bd34766add2c2..1a22cf2bad4e3 100644 --- a/posthog/api/test/test_survey.py +++ b/posthog/api/test/test_survey.py @@ -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 @@ -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}, diff --git a/posthog/models/activity_logging/activity_log.py b/posthog/models/activity_logging/activity_log.py index 58f203a55ad41..da575a3fb89b5 100644 --- a/posthog/models/activity_logging/activity_log.py +++ b/posthog/models/activity_logging/activity_log.py @@ -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 @@ -241,8 +243,10 @@ 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): @@ -250,7 +254,7 @@ def safely_get_field_value(instance: models.Model, field: str): # 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