Skip to content

Commit 148d29c

Browse files
committed
Use Mypy's strict mode
1 parent f53978a commit 148d29c

File tree

7 files changed

+65
-34
lines changed

7 files changed

+65
-34
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,7 @@ repos:
7373
rev: v1.15.0
7474
hooks:
7575
- id: mypy
76+
additional_dependencies:
77+
- django-stubs==5.0.4
78+
- mysqlclient
79+
- pytest==8.3.2

pyproject.toml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,19 @@ enable_error_code = [
108108
"redundant-expr",
109109
"truthy-bool",
110110
]
111-
check_untyped_defs = true
112-
disallow_any_generics = true
113-
disallow_incomplete_defs = true
114-
disallow_untyped_defs = true
115111
mypy_path = "src/"
116112
namespace_packages = false
117-
no_implicit_optional = true
113+
plugins = [ "mypy_django_plugin.main" ]
114+
strict = true
118115
warn_unreachable = true
119-
warn_unused_ignores = true
120116

121117
[[tool.mypy.overrides]]
122118
module = "tests.*"
123119
allow_untyped_defs = true
124120

121+
[tool.django-stubs]
122+
django_settings_module = "tests.settings"
123+
125124
[tool.rstcheck]
126125
ignore_directives = [
127126
"automodule",

src/django_mysql/models/__init__.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from django_mysql.models.aggregates import BitOr
55
from django_mysql.models.aggregates import BitXor
66
from django_mysql.models.aggregates import GroupConcat
7-
from django_mysql.models.base import Model # noqa
7+
from django_mysql.models.base import Model
88
from django_mysql.models.expressions import ListF
99
from django_mysql.models.expressions import SetF
1010
from django_mysql.models.fields import Bit1BooleanField
@@ -27,3 +27,31 @@
2727
from django_mysql.models.query import SmartIterator
2828
from django_mysql.models.query import add_QuerySetMixin
2929
from django_mysql.models.query import pt_visual_explain
30+
31+
__all__ = (
32+
"add_QuerySetMixin",
33+
"ApproximateInt",
34+
"Bit1BooleanField",
35+
"BitAnd",
36+
"BitOr",
37+
"BitXor",
38+
"DynamicField",
39+
"EnumField",
40+
"FixedCharField",
41+
"GroupConcat",
42+
"ListCharField",
43+
"ListF",
44+
"ListTextField",
45+
"Model",
46+
"NullBit1BooleanField",
47+
"pt_visual_explain",
48+
"QuerySet",
49+
"QuerySetMixin",
50+
"SetCharField",
51+
"SetF",
52+
"SetTextField",
53+
"SizedBinaryField",
54+
"SizedTextField",
55+
"SmartChunkedIterator",
56+
"SmartIterator",
57+
)

src/django_mysql/models/fields/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from django_mysql.models.fields.tiny_integer import PositiveTinyIntegerField
1515
from django_mysql.models.fields.tiny_integer import TinyIntegerField
1616

17-
__all__ = [
17+
__all__ = (
1818
"Bit1BooleanField",
1919
"DynamicField",
2020
"EnumField",
@@ -28,4 +28,4 @@
2828
"SizedBinaryField",
2929
"SizedTextField",
3030
"TinyIntegerField",
31-
]
31+
)

src/django_mysql/models/fields/dynamic.py

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

26-
from django_mysql.checks import mysql_connections
2726
from django_mysql.models.lookups import DynColHasKey
2827
from django_mysql.typing import DeconstructResult
28+
from django_mysql.utils import mysql_connections
2929

3030
try:
3131
import mariadb_dyncol
@@ -87,7 +87,7 @@ def check(self, **kwargs: Any) -> list[checks.CheckMessage]:
8787
return errors
8888

8989
def _check_mariadb_dyncol(self) -> list[checks.CheckMessage]:
90-
errors = []
90+
errors: list[checks.CheckMessage] = []
9191
if mariadb_dyncol is None:
9292
errors.append(
9393
checks.Error(
@@ -100,7 +100,7 @@ def _check_mariadb_dyncol(self) -> list[checks.CheckMessage]:
100100
return errors
101101

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

105105
any_conn_works = any(
106106
(conn.vendor == "mysql" and conn.mysql_is_mariadb)
@@ -119,7 +119,7 @@ def _check_mariadb_version(self) -> list[checks.CheckMessage]:
119119
return errors
120120

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

124124
conn = None
125125
for _alias, check_conn in mysql_connections():
@@ -151,7 +151,7 @@ def _check_character_set(self) -> list[checks.CheckMessage]:
151151
def _check_spec_recursively(
152152
self, spec: Any, path: str = ""
153153
) -> list[checks.CheckMessage]:
154-
errors = []
154+
errors: list[checks.CheckMessage] = []
155155

156156
if not isinstance(spec, dict):
157157
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()

0 commit comments

Comments
 (0)