From a6b0a2aff7df8d53292b0f46340ae9e2a782e9e4 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Wed, 12 Jun 2024 16:36:14 +0200 Subject: [PATCH 1/2] Fix and re-enable `AbstractModelTrackerTests` There used to be a [bug in Django](https://code.djangoproject.com/ticket/30427) that caused these tests to fail, according to the discussion of #370. That bug was fixed in Django 4.0. However, because the test model was using a `FieldTracker` rather than a `ModelTracker`, all model-specific test cases in `ModelTrackerTests` continued to fail. --- tests/models.py | 2 +- tests/test_fields/test_field_tracker.py | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/models.py b/tests/models.py index ac65931c..8b496540 100644 --- a/tests/models.py +++ b/tests/models.py @@ -249,7 +249,7 @@ class TrackedAbstract(AbstractTracked): number = models.IntegerField() mutable = MutableField(default=None) - tracker = FieldTracker() + tracker = ModelTracker() class TrackedNotDefault(models.Model): diff --git a/tests/test_fields/test_field_tracker.py b/tests/test_fields/test_field_tracker.py index 42bd66b1..f239bd37 100644 --- a/tests/test_fields/test_field_tracker.py +++ b/tests/test_fields/test_field_tracker.py @@ -1,7 +1,5 @@ from __future__ import annotations -from unittest import skip - from django.core.cache import cache from django.core.exceptions import FieldError from django.db import models @@ -877,7 +875,6 @@ def test_child_fields_not_tracked(self): self.assertTrue(self.tracker.has_changed('name2')) -@skip("has known failures") class AbstractModelTrackerTests(ModelTrackerTests): tracked_class = TrackedAbstract From 14cf1b0d40d5538e79e33527a2c13eb59c04893f Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Wed, 12 Jun 2024 16:46:03 +0200 Subject: [PATCH 2/2] Remove catching of `AttributeError` in `DescriptorWrapper` This catch was introduced in #367, but subsequent discussion in #370, #381 and #382 suggests that it wasn't the proper solution. Now that `AbstractModelTrackerTests` is re-enabled and passing, it becomes clear that `AttributeError` is no longer raised during unit testing when using a recent Django version. Therefore I think it is safe to remove the `try`/`except`. --- model_utils/tracker.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/model_utils/tracker.py b/model_utils/tracker.py index 5136990d..118d1acf 100644 --- a/model_utils/tracker.py +++ b/model_utils/tracker.py @@ -50,10 +50,7 @@ def __get__(self, instance, owner): if instance is None: return self was_deferred = self.field_name in instance.get_deferred_fields() - try: - value = self.descriptor.__get__(instance, owner) - except AttributeError: - value = self.descriptor + value = self.descriptor.__get__(instance, owner) if was_deferred: tracker_instance = getattr(instance, self.tracker_attname) tracker_instance.saved_data[self.field_name] = lightweight_deepcopy(value)