Skip to content

Commit

Permalink
Add minimal type annotations to make mypy pass
Browse files Browse the repository at this point in the history
Avoid using `Self` as a type argument: for some reason this fails
when mypy has an empty cache, but passes when the cache has been
filled. Maybe it's a weird interaction between the mypy core and
the django-stubs plugin?
  • Loading branch information
mthuurne committed Apr 5, 2024
1 parent 441e3ad commit 2f58a11
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
22 changes: 13 additions & 9 deletions tests/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
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
Expand Down Expand Up @@ -32,7 +36,7 @@ class InheritanceManagerTestParent(models.Model):
related_self = models.OneToOneField(
"self", related_name="imtests_self", null=True,
on_delete=models.CASCADE)
objects = InheritanceManager()
objects: ClassVar[InheritanceManager[InheritanceManagerTestParent]] = InheritanceManager()

def __str__(self):
return "{}({})".format(
Expand All @@ -44,7 +48,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[InheritanceManager[InheritanceManagerTestParent]] = InheritanceManager()


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

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

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


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

class Meta:
abstract = True
Expand Down Expand Up @@ -340,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[SoftDeletableModel]] = CustomSoftDeleteManager()


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

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


class JoinItemForeignKey(models.Model):
Expand All @@ -400,7 +404,7 @@ class JoinItemForeignKey(models.Model):
null=True,
on_delete=models.CASCADE
)
objects = JoinManager()
objects: ClassVar[JoinManager[JoinItemForeignKey]] = 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 @@ -73,7 +76,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 @@ -280,7 +283,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 @@ -411,7 +414,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 @@ -502,8 +505,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 @@ -729,7 +732,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 2f58a11

Please sign in to comment.