Skip to content

Commit 233ee5d

Browse files
committed
Use Mypy's strict mode
1 parent dd61c2b commit 233ee5d

File tree

8 files changed

+68
-34
lines changed

8 files changed

+68
-34
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ repos:
5757
rev: v1.0.0
5858
hooks:
5959
- id: mypy
60+
additional_dependencies:
61+
- django-stubs==1.12.0
62+
- mysqlclient
63+
- pytest==7.1.2

pyproject.toml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ 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.mypy]
9-
check_untyped_defs = true
10-
disallow_any_generics = true
11-
disallow_incomplete_defs = true
12-
disallow_untyped_defs = true
1312
mypy_path = "src/"
1413
namespace_packages = false
15-
no_implicit_optional = true
14+
plugins = ["mypy_django_plugin.main"]
1615
show_error_codes = true
16+
strict = true
1717
warn_unreachable = true
18-
warn_unused_ignores = true
1918

2019
[[tool.mypy.overrides]]
2120
module = "tests.*"

src/django_mysql/compat.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from typing import cast
77
from typing import TypeVar
88

9+
__all__ = ("cache",)
10+
911
if sys.version_info >= (3, 9):
1012
from functools import cache
1113
else:

src/django_mysql/models/__init__.py

Lines changed: 30 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
@@ -25,3 +25,32 @@
2525
from django_mysql.models.query import QuerySetMixin
2626
from django_mysql.models.query import SmartChunkedIterator
2727
from django_mysql.models.query import SmartIterator
28+
29+
30+
__all__ = (
31+
"add_QuerySetMixin",
32+
"ApproximateInt",
33+
"Bit1BooleanField",
34+
"BitAnd",
35+
"BitOr",
36+
"BitXor",
37+
"DynamicField",
38+
"EnumField",
39+
"FixedCharField",
40+
"GroupConcat",
41+
"ListCharField",
42+
"ListF",
43+
"ListTextField",
44+
"Model",
45+
"NullBit1BooleanField",
46+
"pt_visual_explain",
47+
"QuerySet",
48+
"QuerySetMixin",
49+
"SetCharField",
50+
"SetF",
51+
"SetTextField",
52+
"SizedBinaryField",
53+
"SizedTextField",
54+
"SmartChunkedIterator",
55+
"SmartIterator",
56+
)

src/django_mysql/models/fields/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from django_mysql.models.fields.sizes import SizedBinaryField
1313
from django_mysql.models.fields.sizes import SizedTextField
1414

15-
__all__ = [
15+
__all__ = (
1616
"Bit1BooleanField",
1717
"DynamicField",
1818
"EnumField",
@@ -24,4 +24,4 @@
2424
"SetTextField",
2525
"SizedBinaryField",
2626
"SizedTextField",
27-
]
27+
)

src/django_mysql/models/fields/dynamic.py

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

28-
from django_mysql.checks import mysql_connections
2928
from django_mysql.models.lookups import DynColHasKey
3029
from django_mysql.typing import DeconstructResult
30+
from django_mysql.utils import mysql_connections
3131

3232
try:
3333
import mariadb_dyncol
@@ -89,7 +89,7 @@ def check(self, **kwargs: Any) -> list[checks.CheckMessage]:
8989
return errors
9090

9191
def _check_mariadb_dyncol(self) -> list[checks.CheckMessage]:
92-
errors = []
92+
errors: list[checks.CheckMessage] = []
9393
if mariadb_dyncol is None:
9494
errors.append(
9595
checks.Error(
@@ -102,7 +102,7 @@ def _check_mariadb_dyncol(self) -> list[checks.CheckMessage]:
102102
return errors
103103

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

107107
any_conn_works = any(
108108
(conn.vendor == "mysql" and conn.mysql_is_mariadb)
@@ -121,7 +121,7 @@ def _check_mariadb_version(self) -> list[checks.CheckMessage]:
121121
return errors
122122

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

126126
conn = None
127127
for _alias, check_conn in mysql_connections():
@@ -153,7 +153,7 @@ def _check_character_set(self) -> list[checks.CheckMessage]:
153153
def _check_spec_recursively(
154154
self, spec: Any, path: str = ""
155155
) -> list[checks.CheckMessage]:
156-
errors = []
156+
errors: list[checks.CheckMessage] = []
157157

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