Skip to content

Commit 27fffbf

Browse files
committed
Use Mypy's strict mode
1 parent 8da03cd commit 27fffbf

File tree

11 files changed

+85
-53
lines changed

11 files changed

+85
-53
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ repos:
4545
rev: v0.971
4646
hooks:
4747
- id: mypy
48+
additional_dependencies:
49+
- django-stubs==1.12.0
50+
- mysqlclient

pyproject.toml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@ build-backend = "setuptools.build_meta"
55
[tool.black]
66
target-version = ['py37']
77

8+
[tool.django-stubs]
9+
django_settings_module = "tests.settings"
10+
811
[tool.isort]
912
profile = "black"
1013
add_imports = "from __future__ import annotations"
1114

1215
[tool.mypy]
13-
check_untyped_defs = true
14-
disallow_any_generics = true
15-
disallow_incomplete_defs = true
16-
disallow_untyped_defs = true
1716
mypy_path = "src/"
18-
no_implicit_optional = true
17+
plugins = ["mypy_django_plugin.main"]
1918
show_error_codes = true
19+
strict = true
2020
warn_unreachable = true
21-
warn_unused_ignores = true
2221

2322
[[tool.mypy.overrides]]
2423
module = "tests.*"

src/django_mysql/compat.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import sys
44
from typing import Any, Callable, TypeVar, cast
55

6+
__all__ = ("cache",)
7+
68
if sys.version_info >= (3, 9):
79
from functools import cache
810
else:

src/django_mysql/models/__init__.py

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
"""
2-
isort:skip_file
3-
"""
4-
from django_mysql.models.base import Model # noqa
5-
from django_mysql.models.aggregates import BitAnd, BitOr, BitXor, GroupConcat # noqa
6-
from django_mysql.models.expressions import ListF, SetF # noqa
7-
from django_mysql.models.query import ( # noqa
8-
add_QuerySetMixin,
9-
ApproximateInt,
10-
SmartChunkedIterator,
11-
SmartIterator,
12-
pt_visual_explain,
13-
QuerySet,
14-
QuerySetMixin,
15-
)
16-
from django_mysql.models.fields import ( # noqa
1+
from __future__ import annotations
2+
3+
from django_mysql.models.aggregates import BitAnd, BitOr, BitXor, GroupConcat
4+
from django_mysql.models.base import Model
5+
from django_mysql.models.expressions import ListF, SetF
6+
from django_mysql.models.fields import (
177
Bit1BooleanField,
188
DynamicField,
199
EnumField,
@@ -26,3 +16,40 @@
2616
SizedBinaryField,
2717
SizedTextField,
2818
)
19+
from django_mysql.models.query import (
20+
ApproximateInt,
21+
QuerySet,
22+
QuerySetMixin,
23+
SmartChunkedIterator,
24+
SmartIterator,
25+
add_QuerySetMixin,
26+
pt_visual_explain,
27+
)
28+
29+
__all__ = (
30+
"add_QuerySetMixin",
31+
"ApproximateInt",
32+
"Bit1BooleanField",
33+
"BitAnd",
34+
"BitOr",
35+
"BitXor",
36+
"DynamicField",
37+
"EnumField",
38+
"FixedCharField",
39+
"GroupConcat",
40+
"ListCharField",
41+
"ListF",
42+
"ListTextField",
43+
"Model",
44+
"NullBit1BooleanField",
45+
"pt_visual_explain",
46+
"QuerySet",
47+
"QuerySetMixin",
48+
"SetCharField",
49+
"SetF",
50+
"SetTextField",
51+
"SizedBinaryField",
52+
"SizedTextField",
53+
"SmartChunkedIterator",
54+
"SmartIterator",
55+
)

src/django_mysql/models/fields/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from django_mysql.models.fields.sets import SetCharField, SetTextField
99
from django_mysql.models.fields.sizes import SizedBinaryField, SizedTextField
1010

11-
__all__ = [
11+
__all__ = (
1212
"Bit1BooleanField",
1313
"DynamicField",
1414
"EnumField",
@@ -20,4 +20,4 @@
2020
"SetTextField",
2121
"SizedBinaryField",
2222
"SizedTextField",
23-
]
23+
)

src/django_mysql/models/fields/dynamic.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
from django.forms import Field as FormField
2222
from django.utils.translation import gettext_lazy as _
2323

24-
from django_mysql.checks import mysql_connections
2524
from django_mysql.models.lookups import DynColHasKey
2625
from django_mysql.typing import DeconstructResult
26+
from django_mysql.utils import mysql_connections
2727

2828
try:
2929
import mariadb_dyncol
@@ -85,7 +85,7 @@ def check(self, **kwargs: Any) -> list[checks.CheckMessage]:
8585
return errors
8686

8787
def _check_mariadb_dyncol(self) -> list[checks.CheckMessage]:
88-
errors = []
88+
errors: list[checks.CheckMessage] = []
8989
if mariadb_dyncol is None:
9090
errors.append(
9191
checks.Error(
@@ -98,7 +98,7 @@ def _check_mariadb_dyncol(self) -> list[checks.CheckMessage]:
9898
return errors
9999

100100
def _check_mariadb_version(self) -> list[checks.CheckMessage]:
101-
errors = []
101+
errors: list[checks.CheckMessage] = []
102102

103103
any_conn_works = any(
104104
(conn.vendor == "mysql" and conn.mysql_is_mariadb)
@@ -117,7 +117,7 @@ def _check_mariadb_version(self) -> list[checks.CheckMessage]:
117117
return errors
118118

119119
def _check_character_set(self) -> list[checks.CheckMessage]:
120-
errors = []
120+
errors: list[checks.CheckMessage] = []
121121

122122
conn = None
123123
for _alias, check_conn in mysql_connections():
@@ -149,7 +149,7 @@ def _check_character_set(self) -> list[checks.CheckMessage]:
149149
def _check_spec_recursively(
150150
self, spec: Any, path: str = ""
151151
) -> list[checks.CheckMessage]:
152-
errors = []
152+
errors: list[checks.CheckMessage] = []
153153

154154
if not isinstance(spec, dict):
155155
errors.append(

src/django_mysql/operations.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
44
from django.db.migrations.operations.base import Operation
5-
from django.db.migrations.state import ModelState
5+
from django.db.migrations.state import ProjectState
66
from django.utils.functional import cached_property
77

88

@@ -15,15 +15,15 @@ def __init__(self, name: str, soname: str) -> None:
1515
self.name = name
1616
self.soname = soname
1717

18-
def state_forwards(self, app_label: str, state: ModelState) -> None:
18+
def state_forwards(self, app_label: str, state: ProjectState) -> None:
1919
pass # pragma: no cover
2020

2121
def database_forwards(
2222
self,
2323
app_label: str,
2424
schema_editor: BaseDatabaseSchemaEditor,
25-
from_st: ModelState,
26-
to_st: ModelState,
25+
from_st: ProjectState,
26+
to_st: ProjectState,
2727
) -> None:
2828
if not self.plugin_installed(schema_editor):
2929
schema_editor.execute(
@@ -34,8 +34,8 @@ def database_backwards(
3434
self,
3535
app_label: str,
3636
schema_editor: BaseDatabaseSchemaEditor,
37-
from_st: ModelState,
38-
to_st: ModelState,
37+
from_st: ProjectState,
38+
to_st: ProjectState,
3939
) -> None:
4040
if self.plugin_installed(schema_editor):
4141
schema_editor.execute("UNINSTALL PLUGIN %s" % self.name)
@@ -63,24 +63,24 @@ class InstallSOName(Operation):
6363
def __init__(self, soname: str) -> None:
6464
self.soname = soname
6565

66-
def state_forwards(self, app_label: str, state: ModelState) -> None:
66+
def state_forwards(self, app_label: str, state: ProjectState) -> None:
6767
pass # pragma: no cover
6868

6969
def database_forwards(
7070
self,
7171
app_label: str,
7272
schema_editor: BaseDatabaseSchemaEditor,
73-
from_st: ModelState,
74-
to_st: ModelState,
73+
from_st: ProjectState,
74+
to_st: ProjectState,
7575
) -> None:
7676
schema_editor.execute("INSTALL SONAME %s", (self.soname,))
7777

7878
def database_backwards(
7979
self,
8080
app_label: str,
8181
schema_editor: BaseDatabaseSchemaEditor,
82-
from_st: ModelState,
83-
to_st: ModelState,
82+
from_st: ProjectState,
83+
to_st: ProjectState,
8484
) -> None:
8585
schema_editor.execute("UNINSTALL SONAME %s", (self.soname,))
8686

@@ -100,24 +100,24 @@ def __init__(
100100
def reversible(self) -> bool:
101101
return self.from_engine is not None
102102

103-
def state_forwards(self, app_label: str, state: ModelState) -> None:
103+
def state_forwards(self, app_label: str, state: ProjectState) -> None:
104104
pass
105105

106106
def database_forwards(
107107
self,
108108
app_label: str,
109109
schema_editor: BaseDatabaseSchemaEditor,
110-
from_state: ModelState,
111-
to_state: ModelState,
110+
from_state: ProjectState,
111+
to_state: ProjectState,
112112
) -> None:
113113
self._change_engine(app_label, schema_editor, to_state, engine=self.engine)
114114

115115
def database_backwards(
116116
self,
117117
app_label: str,
118118
schema_editor: BaseDatabaseSchemaEditor,
119-
from_state: ModelState,
120-
to_state: ModelState,
119+
from_state: ProjectState,
120+
to_state: ProjectState,
121121
) -> None:
122122
if self.from_engine is None:
123123
raise NotImplementedError("You cannot reverse this operation")
@@ -128,7 +128,7 @@ def _change_engine(
128128
self,
129129
app_label: str,
130130
schema_editor: BaseDatabaseSchemaEditor,
131-
to_state: ModelState,
131+
to_state: ProjectState,
132132
engine: str,
133133
) -> None:
134134
new_model = to_state.apps.get_model(app_label, self.name)

src/django_mysql/status.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from django.db import connections
77
from django.db.backends.utils import CursorWrapper
88
from django.db.utils import DEFAULT_DB_ALIAS
9-
from django.utils.functional import SimpleLazyObject
109

1110
from django_mysql.exceptions import TimeoutError
1211

@@ -16,6 +15,7 @@ class BaseStatus:
1615
Base class for the status classes
1716
"""
1817

18+
__slots__ = ("db",)
1919
query = ""
2020

2121
def __init__(self, using: str | None = None) -> None:
@@ -127,5 +127,5 @@ class SessionStatus(BaseStatus):
127127
query = "SHOW SESSION STATUS"
128128

129129

130-
global_status = SimpleLazyObject(GlobalStatus)
131-
session_status = SimpleLazyObject(SessionStatus)
130+
global_status = GlobalStatus()
131+
session_status = SessionStatus()

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_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ def test_sha2_bad_hash_len(self):
354354

355355
class InformationFunctionTests(TestCase):
356356

357-
databases = ["default", "other"]
357+
databases = {"default", "other"}
358358

359359
def test_last_insert_id(self):
360360
Alphabet.objects.create(a=7891)

0 commit comments

Comments
 (0)