Skip to content

Commit

Permalink
Add minimal type annotations to make mypy pass
Browse files Browse the repository at this point in the history
  • Loading branch information
mthuurne committed Mar 25, 2024
1 parent b863afd commit f0091b1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
23 changes: 14 additions & 9 deletions tests/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from __future__ import annotations

from typing import ClassVar

from django.db import models
from django.db.models import Manager
from django.db.models.query_utils import DeferredAttribute
from django.utils.translation import gettext_lazy as _
from typing_extensions import Self

from model_utils import Choices
from model_utils.fields import MonitorField, SplitField, StatusField, UUIDField
Expand Down Expand Up @@ -32,7 +37,7 @@ class InheritanceManagerTestParent(models.Model):
related_self = models.OneToOneField(
"self", related_name="imtests_self", null=True,
on_delete=models.CASCADE)
objects = InheritanceManager()
objects: ClassVar[Manager[Self]] = InheritanceManager()

def __str__(self):
return "{}({})".format(
Expand All @@ -44,7 +49,7 @@ def __str__(self):
class InheritanceManagerTestChild1(InheritanceManagerTestParent):
non_related_field_using_descriptor_2 = models.FileField(upload_to="test")
normal_field_2 = models.TextField()
objects = InheritanceManager()
objects: ClassVar[Manager[Self]] = InheritanceManager()


class InheritanceManagerTestGrandChild1(InheritanceManagerTestChild1):
Expand Down Expand Up @@ -171,8 +176,8 @@ class Post(models.Model):
order = models.IntegerField()

objects = models.Manager()
public = QueryManager(published=True)
public_confirmed = QueryManager(
public: ClassVar[QueryManager[Self]] = QueryManager(published=True)
public_confirmed: ClassVar[QueryManager[Self]] = QueryManager(
models.Q(published=True) & models.Q(confirmed=True))
public_reversed = QueryManager(published=True).order_by("-order")

Expand All @@ -193,7 +198,7 @@ class Meta:


class AbstractTracked(models.Model):
number = 1
number: models.IntegerField

class Meta:
abstract = True
Expand Down Expand Up @@ -339,13 +344,13 @@ class SoftDeletable(SoftDeletableModel):
"""
name = models.CharField(max_length=20)

all_objects = models.Manager()
all_objects: ClassVar[Manager[SoftDeletable]] = models.Manager()


class CustomSoftDelete(SoftDeletableModel):
is_read = models.BooleanField(default=False)

objects = CustomSoftDeleteManager()
objects: ClassVar[CustomSoftDeleteManager[Self]] = CustomSoftDeleteManager()


class StringyDescriptor:
Expand Down Expand Up @@ -389,7 +394,7 @@ class ModelWithCustomDescriptor(models.Model):

class BoxJoinModel(models.Model):
name = models.CharField(max_length=32)
objects = JoinManager()
objects: ClassVar[JoinManager[Self]] = JoinManager()


class JoinItemForeignKey(models.Model):
Expand All @@ -399,7 +404,7 @@ class JoinItemForeignKey(models.Model):
null=True,
on_delete=models.CASCADE
)
objects = JoinManager()
objects: ClassVar[JoinManager[Self]] = JoinManager()


class CustomUUIDModel(UUIDModel):
Expand Down
15 changes: 9 additions & 6 deletions tests/test_fields/test_field_tracker.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
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
from django.db.models.fields.files import FieldFile
from django.test import TestCase

Expand Down Expand Up @@ -69,7 +72,7 @@ def test_pre_save_previous(self):

class FieldTrackerTests(FieldTrackerTestCase, FieldTrackerCommonTests):

tracked_class = Tracked
tracked_class: type[models.Model] = Tracked

def setUp(self):
self.instance = self.tracked_class()
Expand Down Expand Up @@ -276,7 +279,7 @@ def test_with_deferred_fields_access_multiple(self):
class FieldTrackedModelCustomTests(FieldTrackerTestCase,
FieldTrackerCommonTests):

tracked_class = TrackedNotDefault
tracked_class: type[models.Model] = TrackedNotDefault

def setUp(self):
self.instance = self.tracked_class()
Expand Down Expand Up @@ -407,7 +410,7 @@ def test_current(self):
class FieldTrackedModelMultiTests(FieldTrackerTestCase,
FieldTrackerCommonTests):

tracked_class = TrackedMultiple
tracked_class: type[models.Model] = TrackedMultiple

def setUp(self):
self.instance = self.tracked_class()
Expand Down Expand Up @@ -498,8 +501,8 @@ def test_current(self):

class FieldTrackerForeignKeyTests(FieldTrackerTestCase):

fk_class = Tracked
tracked_class = TrackedFK
fk_class: type[models.Model] = Tracked
tracked_class: type[models.Model] = TrackedFK

def setUp(self):
self.old_fk = self.fk_class.objects.create(number=8)
Expand Down Expand Up @@ -725,7 +728,7 @@ def test_current(self):

class ModelTrackerTests(FieldTrackerTests):

tracked_class = ModelTracked
tracked_class: type[models.Model] = ModelTracked

def test_cache_compatible(self):
cache.set('key', self.instance)
Expand Down

0 comments on commit f0091b1

Please sign in to comment.