Skip to content

Commit 8758ed1

Browse files
committed
Improve function types
1 parent 7aeb183 commit 8758ed1

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/django_mysql/models/functions.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ def get(cls, using: str = DEFAULT_DB_ALIAS) -> int:
169169
# database connections in Django, and the reason was not clear
170170
with connections[using].cursor() as cursor:
171171
cursor.execute("SELECT LAST_INSERT_ID()")
172-
return cursor.fetchone()[0]
172+
id_: int = cursor.fetchone()[0]
173+
return id_
173174

174175

175176
# JSON Functions
@@ -258,7 +259,11 @@ def as_sql(
258259
if connection.vendor != "mysql": # pragma: no cover
259260
raise AssertionError("JSONValue only supports MySQL/MariaDB")
260261
json_string = json.dumps(self._data, allow_nan=False)
261-
if connection.vendor == "mysql" and connection.mysql_is_mariadb:
262+
if (
263+
connection.vendor == "mysql"
264+
# type narrowed by vendor check
265+
and connection.mysql_is_mariadb # type: ignore [attr-defined]
266+
):
262267
# MariaDB doesn't support explicit cast to JSON.
263268
return "JSON_EXTRACT(%s, '$')", (json_string,)
264269
else:
@@ -270,7 +275,7 @@ def __init__(
270275
self,
271276
expression: ExpressionArgument,
272277
data: dict[
273-
str,
278+
ExpressionArgument,
274279
(
275280
ExpressionArgument
276281
| None
@@ -288,12 +293,12 @@ def __init__(
288293
exprs = [expression]
289294

290295
for path, value in data.items():
291-
if not hasattr(path, "resolve_expression"):
296+
if not isinstance(path, Expression):
292297
path = Value(path)
293298

294299
exprs.append(path)
295300

296-
if not hasattr(value, "resolve_expression"):
301+
if not isinstance(value, Expression):
297302
value = JSONValue(value)
298303

299304
exprs.append(value)
@@ -392,20 +397,20 @@ def __init__(
392397
self,
393398
expression: ExpressionArgument,
394399
to_add: dict[
395-
str,
400+
ExpressionArgument,
396401
ExpressionArgument | float | int | dt.date | dt.time | dt.datetime,
397402
],
398403
) -> None:
399404
from django_mysql.models.fields import DynamicField
400405

401406
expressions = [expression]
402407
for name, value in to_add.items():
403-
if not hasattr(name, "resolve_expression"):
408+
if not isinstance(name, Expression):
404409
name = Value(name)
405410

406-
if isinstance(value, dict): # type: ignore [unreachable]
411+
if isinstance(value, dict):
407412
raise ValueError("ColumnAdd with nested values is not supported")
408-
if not hasattr(value, "resolve_expression"):
413+
if not isinstance(value, Expression):
409414
value = Value(value)
410415

411416
expressions.extend((name, value))

0 commit comments

Comments
 (0)