Skip to content

Commit a8e867b

Browse files
authored
Fix some types (#1162)
Fix some issues discovered by running ty on the codebase.
1 parent d513ebd commit a8e867b

File tree

6 files changed

+28
-24
lines changed

6 files changed

+28
-24
lines changed

src/django_mysql/models/fields/dynamic.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828

2929
try:
3030
import mariadb_dyncol
31+
32+
HAVE_MARIADB_DYNCOL = True
3133
except ImportError: # pragma: no cover
32-
mariadb_dyncol = None
34+
HAVE_MARIADB_DYNCOL = False
3335

3436

3537
# Mypy doesn't support recursive types at time of writing, but we can at least
@@ -87,7 +89,7 @@ def check(self, **kwargs: Any) -> list[checks.CheckMessage]:
8789

8890
def _check_mariadb_dyncol(self) -> list[checks.CheckMessage]:
8991
errors = []
90-
if mariadb_dyncol is None:
92+
if not HAVE_MARIADB_DYNCOL:
9193
errors.append(
9294
checks.Error(
9395
"'mariadb_dyncol' is required to use DynamicField",

src/django_mysql/models/functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def __init__(
182182
self,
183183
expression: ExpressionArgument,
184184
*paths: ExpressionArgument,
185-
output_field: type[DjangoField] | None = None,
185+
output_field: DjangoField | None = None,
186186
) -> None:
187187
exprs = [expression]
188188
for path in paths:

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_state: ProjectState,
26+
to_state: 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_state: ProjectState,
38+
to_state: ProjectState,
3939
) -> None:
4040
if self.plugin_installed(schema_editor):
4141
schema_editor.execute(f"UNINSTALL PLUGIN {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_state: ProjectState,
74+
to_state: 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_state: ProjectState,
83+
to_state: 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/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ def collapse_spaces(string: str) -> str:
131131
return " ".join(filter(None, bits))
132132

133133

134-
def index_name(model: Model, *field_names: str, using: str = DEFAULT_DB_ALIAS) -> str:
134+
def index_name(
135+
model: type[Model], *field_names: str, using: str = DEFAULT_DB_ALIAS
136+
) -> str:
135137
"""
136138
Returns the name of the index existing on field_names, or raises KeyError
137139
if no such index exists.

tests/testapp/test_dynamicfield.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ class Valid(models.Model):
313313
assert errors[0].id == "django_mysql.E013"
314314
assert "MariaDB is required" in errors[0].msg
315315

316-
@mock.patch(DynamicField.__module__ + ".mariadb_dyncol", new=None)
316+
@mock.patch(DynamicField.__module__ + ".HAVE_MARIADB_DYNCOL", new=False)
317317
def test_mariadb_dyncol_missing(self):
318318
errors = DynamicModel.check()
319319
assert len(errors) == 1

tests/testapp/test_size_fields.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_binaryfield_default_length(self):
4646
# By default, SizedBinaryField should act like BinaryField
4747
field = SizedBinaryField()
4848
assert field.size_class == 4
49-
assert field.db_type(None) == "longblob"
49+
assert field.db_type(connection) == "longblob"
5050

5151
@atomic
5252
def test_binary_1_max_length(self):
@@ -149,7 +149,7 @@ def test_textfield_default_length(self):
149149
# By default, SizedTextField should act like TextField
150150
field = SizedTextField()
151151
assert field.size_class == 4
152-
assert field.db_type(None) == "longtext"
152+
assert field.db_type(connection) == "longtext"
153153

154154
def test_tinytext_max_length(self):
155155
# Okay

0 commit comments

Comments
 (0)