|
6 | 6 | */ |
7 | 7 |
|
8 | 8 | use crate::django_testcase; |
| 9 | +use crate::test::django::util::django_env; |
| 10 | +use crate::test::util::TestEnv; |
| 11 | +use crate::testcase; |
| 12 | + |
| 13 | +fn django_model_utils_env() -> TestEnv { |
| 14 | + let mut env = django_env(); |
| 15 | + env.add( |
| 16 | + "model_utils.managers", |
| 17 | + r#" |
| 18 | +from typing import TypeVar |
| 19 | +
|
| 20 | +from django.db import models |
| 21 | +
|
| 22 | +ModelT = TypeVar("ModelT", bound=models.Model, covariant=True) |
| 23 | +
|
| 24 | +class InheritanceQuerySet(models.QuerySet[ModelT]): ... |
| 25 | +
|
| 26 | +class InheritanceManager(models.Manager[ModelT]): |
| 27 | + def select_subclasses(self) -> InheritanceQuerySet[ModelT]: ... |
| 28 | +"#, |
| 29 | + ); |
| 30 | + env |
| 31 | +} |
| 32 | + |
| 33 | +testcase!( |
| 34 | + test_django_model_utils_custom_manager, |
| 35 | + django_model_utils_env(), |
| 36 | + r#" |
| 37 | +from typing import assert_type |
| 38 | +
|
| 39 | +from django.db import models |
| 40 | +from model_utils.managers import InheritanceManager, InheritanceQuerySet |
| 41 | +
|
| 42 | +class Place(models.Model): |
| 43 | + objects = InheritanceManager() |
| 44 | +
|
| 45 | +assert_type(Place.objects, InheritanceManager[Place]) |
| 46 | +assert_type(Place.objects.select_subclasses(), InheritanceQuerySet[Place]) |
| 47 | +assert_type(Place.objects.get(), Place) |
| 48 | +assert_type(Place.objects.create(), Place) |
| 49 | +assert_type(Place.objects.filter(), models.QuerySet[Place]) |
| 50 | +
|
| 51 | +class Child(Place): |
| 52 | + objects = models.Manager() |
| 53 | +
|
| 54 | +assert_type(Child.objects, models.Manager[Child]) |
| 55 | +assert_type(Child.objects.get(), Child) |
| 56 | +
|
| 57 | +class SiblingManager(models.Manager): |
| 58 | + def custom(self) -> int: |
| 59 | + return 1 |
| 60 | +
|
| 61 | +class OtherChild(Place): |
| 62 | + objects = SiblingManager() |
| 63 | +
|
| 64 | +assert_type(OtherChild.objects, SiblingManager) |
| 65 | +assert_type(OtherChild.objects.get(), OtherChild) |
| 66 | +assert_type(OtherChild.objects.custom(), int) |
| 67 | +"#, |
| 68 | +); |
| 69 | + |
| 70 | +django_testcase!( |
| 71 | + test_bare_custom_manager_model_binding, |
| 72 | + r#" |
| 73 | +from typing import assert_type |
| 74 | +from django.db import models |
| 75 | +
|
| 76 | +class PersonManager(models.Manager): |
| 77 | + def custom(self) -> int: |
| 78 | + return 1 |
| 79 | +
|
| 80 | +class Person(models.Model): |
| 81 | + objects = PersonManager() |
| 82 | +
|
| 83 | +class Child(Person): ... |
| 84 | +
|
| 85 | +assert_type(Person.objects, PersonManager) |
| 86 | +assert_type(Person.objects.get(), Person) |
| 87 | +assert_type(Person.objects.create(), Person) |
| 88 | +assert_type(Person.objects.filter(), models.QuerySet[Person]) |
| 89 | +assert_type(Person.objects.model, type[Person]) |
| 90 | +assert_type(Person.objects.custom(), int) |
| 91 | +assert_type(Person.objects.db_manager().get(), Person) |
| 92 | +assert_type(Child.objects.get(), Child) |
| 93 | +assert_type(Child.objects.filter(), models.QuerySet[Child]) |
| 94 | +
|
| 95 | +def accept_manager(manager: models.Manager[Person]) -> None: ... |
| 96 | +accept_manager(Person.objects) |
| 97 | +"#, |
| 98 | +); |
| 99 | + |
| 100 | +django_testcase!( |
| 101 | + test_custom_manager_inherited_qualifiers, |
| 102 | + r#" |
| 103 | +from typing import Any, ClassVar, Final, Protocol, assert_type |
| 104 | +from django.db import models |
| 105 | +
|
| 106 | +class CustomManager(models.Manager): ... |
| 107 | +
|
| 108 | +class Base(models.Model): |
| 109 | + records: ClassVar[models.Manager["Base"]] = models.Manager() |
| 110 | + locked: Final[models.Manager["Base"]] = models.Manager() |
| 111 | +
|
| 112 | +class Person(Base): |
| 113 | + objects = CustomManager() |
| 114 | + records = CustomManager() |
| 115 | + locked = CustomManager() # E: `locked` is declared as final in parent class `Base` |
| 116 | +
|
| 117 | +class HasManager(Protocol): |
| 118 | + objects: models.Manager[Any] |
| 119 | +
|
| 120 | +def accept_model(model: HasManager) -> None: ... |
| 121 | +
|
| 122 | +class HasCustomManager(Protocol): |
| 123 | + objects: CustomManager |
| 124 | +
|
| 125 | +def accept_custom_model(model: HasCustomManager) -> None: ... |
| 126 | +accept_custom_model(Person) |
| 127 | +
|
| 128 | +person = Person() |
| 129 | +person.objects = Person.objects # E: Cannot set field `objects` |
| 130 | +person.records = Person.records # E: Cannot set field `records` |
| 131 | +Person.locked = Person.locked # E: Cannot set field `locked` |
| 132 | +
|
| 133 | +class Child(Person): |
| 134 | + objects = models.Manager() |
| 135 | + records = models.Manager() |
| 136 | +
|
| 137 | +assert_type(Child.objects.get(), Child) |
| 138 | +assert_type(Child.records.get(), Child) |
| 139 | +accept_model(Child) |
| 140 | +child = Child() |
| 141 | +child.objects = Child.objects # E: Cannot set field `objects` |
| 142 | +child.records = Child.records # E: Cannot set field `records` |
| 143 | +
|
| 144 | +class Plain(models.Model): |
| 145 | + objects = models.Manager() |
| 146 | +
|
| 147 | +accept_model(Plain) |
| 148 | +plain = Plain() |
| 149 | +plain.objects = Plain.objects # E: Cannot set field `objects` |
| 150 | +"#, |
| 151 | +); |
| 152 | + |
| 153 | +django_testcase!( |
| 154 | + test_custom_manager_constructor_arguments, |
| 155 | + r#" |
| 156 | +from typing import Generic, TypeVar, assert_type |
| 157 | +from django.db import models |
| 158 | +
|
| 159 | +T = TypeVar("T") |
| 160 | +M = TypeVar("M", bound=models.Model) |
| 161 | +
|
| 162 | +class CustomManager(models.Manager[M], Generic[T, M]): |
| 163 | + def __init__(self, value: T) -> None: |
| 164 | + self.value = value |
| 165 | +
|
| 166 | + def custom(self) -> T: |
| 167 | + return self.value |
| 168 | +
|
| 169 | +class Person(models.Model): |
| 170 | + objects = CustomManager("label") |
| 171 | +
|
| 172 | +assert_type(Person.objects, CustomManager[str, Person]) |
| 173 | +assert_type(Person.objects.custom(), str) |
| 174 | +assert_type(Person.objects.get(), Person) |
| 175 | +
|
| 176 | +class Ordinary: |
| 177 | + objects = models.Manager[Person]() |
| 178 | +
|
| 179 | +assert_type(Ordinary.objects.get(), Person) |
| 180 | +"#, |
| 181 | +); |
9 | 182 |
|
10 | 183 | django_testcase!( |
11 | 184 | test_model, |
|
0 commit comments