Skip to content

Commit 9be771e

Browse files
authored
Update tests to pass Mypy strict mode (#941)
1 parent fb9322b commit 9be771e

13 files changed

+170
-166
lines changed

tests/testapp/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Any
66

77
import django
8+
from django.core import checks
89
from django.db import connection
910
from django.db.models import (
1011
CASCADE,
@@ -119,7 +120,7 @@ class DynamicModel(Model):
119120
)
120121

121122
@classmethod
122-
def check(cls, **kwargs):
123+
def check(cls, **kwargs: Any) -> list[checks.CheckMessage]:
123124
# Disable the checks on MySQL so that checks tests don't fail
124125
if not connection.mysql_is_mariadb:
125126
return []

tests/testapp/test_aggregates.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ def test_no_rows(self):
6666

6767

6868
class GroupConcatTests(TestCase):
69+
shakes: Author
70+
jk: Author
71+
grisham: Author
72+
str_tutee_ids: list[str]
73+
6974
@classmethod
7075
def setUpTestData(cls):
7176
super().setUpTestData()

tests/testapp/test_checks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
class CallCheckTest(TestCase):
1111

12-
databases = ["default", "other"]
12+
databases = {"default", "other"}
1313

1414
def test_check(self):
1515
call_command("check", "--database", "default", "--database", "other")
1616

1717

1818
class VariablesTests(TransactionTestCase):
1919

20-
databases = ["default", "other"]
20+
databases = {"default", "other"}
2121

2222
def test_passes(self):
2323
assert check_variables(app_configs=None, databases=["default", "other"]) == []

tests/testapp/test_dynamicfield.py

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -118,28 +118,31 @@ def as_sql(self, compiler, connection):
118118

119119

120120
class QueryTests(DynColTestCase):
121+
objs: list[DynamicModel]
122+
121123
@classmethod
122124
def setUpTestData(cls):
123125
super().setUpTestData()
124-
cls.objs = [
125-
DynamicModel(attrs={"a": "b"}),
126-
DynamicModel(attrs={"a": "b", "c": "d"}),
127-
DynamicModel(attrs={"c": "d"}),
128-
DynamicModel(attrs={}),
129-
DynamicModel(
130-
attrs={
131-
"datetimey": dt.datetime(2001, 1, 4, 14, 15, 16),
132-
"datey": dt.date(2001, 1, 4),
133-
"floaty": 128.5,
134-
"inty": 9001,
135-
"stry": "strvalue",
136-
"str_underscorey": "strvalue2",
137-
"timey": dt.time(14, 15, 16),
138-
"nesty": {"level2": "chirp"},
139-
}
140-
),
141-
]
142-
DynamicModel.objects.bulk_create(cls.objs)
126+
DynamicModel.objects.bulk_create(
127+
[
128+
DynamicModel(attrs={"a": "b"}),
129+
DynamicModel(attrs={"a": "b", "c": "d"}),
130+
DynamicModel(attrs={"c": "d"}),
131+
DynamicModel(attrs={}),
132+
DynamicModel(
133+
attrs={
134+
"datetimey": dt.datetime(2001, 1, 4, 14, 15, 16),
135+
"datey": dt.date(2001, 1, 4),
136+
"floaty": 128.5,
137+
"inty": 9001,
138+
"stry": "strvalue",
139+
"str_underscorey": "strvalue2",
140+
"timey": dt.time(14, 15, 16),
141+
"nesty": {"level2": "chirp"},
142+
}
143+
),
144+
]
145+
)
143146
cls.objs = list(DynamicModel.objects.order_by("id"))
144147

145148
def test_equal(self):
@@ -271,14 +274,17 @@ def test_key_transform_nesty__level2__startswith(self):
271274

272275

273276
class SpeclessQueryTests(DynColTestCase):
277+
objs: list[SpeclessDynamicModel]
278+
274279
@classmethod
275280
def setUpTestData(cls):
276281
super().setUpTestData()
277-
objs = [
278-
SpeclessDynamicModel(attrs={"a": "b"}),
279-
SpeclessDynamicModel(attrs={"a": "c"}),
280-
]
281-
SpeclessDynamicModel.objects.bulk_create(objs)
282+
SpeclessDynamicModel.objects.bulk_create(
283+
[
284+
SpeclessDynamicModel(attrs={"a": "b"}),
285+
SpeclessDynamicModel(attrs={"a": "c"}),
286+
]
287+
)
282288
cls.objs = list(SpeclessDynamicModel.objects.order_by("id"))
283289

284290
def test_simple(self):
@@ -290,7 +296,7 @@ def test_simple(self):
290296
@isolate_apps("tests.testapp")
291297
class TestCheck(DynColTestCase):
292298

293-
databases = ["default", "other"]
299+
databases = {"default", "other"}
294300

295301
def test_db_not_mariadb(self):
296302
class Valid(models.Model):
@@ -337,7 +343,9 @@ class Invalid(models.Model):
337343
assert len(errors) == 1
338344
assert errors[0].id == "django_mysql.E009"
339345
assert "'spec' must be a dict" in errors[0].msg
340-
assert "The value passed is of type list" in errors[0].hint
346+
hint = errors[0].hint
347+
assert hint is not None
348+
assert "The value passed is of type list" in hint
341349

342350
def test_spec_key_not_valid(self):
343351
class Invalid(models.Model):
@@ -347,8 +355,10 @@ class Invalid(models.Model):
347355
assert len(errors) == 1
348356
assert errors[0].id == "django_mysql.E010"
349357
assert "The key '2.0' in 'spec' is not a string" in errors[0].msg
350-
assert "'spec' keys must be of type " in errors[0].hint
351-
assert "'2.0' is of type float" in errors[0].hint
358+
hint = errors[0].hint
359+
assert hint is not None
360+
assert "'spec' keys must be of type " in hint
361+
assert "'2.0' is of type float" in hint
352362

353363
def test_spec_value_not_valid(self):
354364
class Invalid(models.Model):
@@ -358,9 +368,10 @@ class Invalid(models.Model):
358368
assert len(errors) == 1
359369
assert errors[0].id == "django_mysql.E011"
360370
assert "The value for 'bad' in 'spec' is not an allowed type" in errors[0].msg
371+
hint = errors[0].hint
372+
assert hint is not None
361373
assert (
362-
"'spec' values must be one of the following types: date, datetime"
363-
in errors[0].hint
374+
"'spec' values must be one of the following types: date, datetime" in hint
364375
)
365376

366377
def test_spec_nested_value_not_valid(self):
@@ -375,9 +386,10 @@ class Invalid(models.Model):
375386
assert (
376387
"The value for 'bad' in 'spec.l1' is not an allowed type" in errors[0].msg
377388
)
389+
hint = errors[0].hint
390+
assert hint is not None
378391
assert (
379-
"'spec' values must be one of the following types: date, datetime"
380-
in errors[0].hint
392+
"'spec' values must be one of the following types: date, datetime" in hint
381393
)
382394

383395

tests/testapp/test_enumfield.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def test_contains_lookup(self):
9898

9999
class TestCheck(TestCase):
100100

101-
databases = ["default", "other"]
101+
databases = {"default", "other"}
102102

103103
def test_check(self):
104104
errors = EnumModel.check()

0 commit comments

Comments
 (0)