Skip to content

Commit 6d7e6ad

Browse files
committed
Finish hints for cache module
1 parent 86cd00a commit 6d7e6ad

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/django_mysql/cache.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from django.core.cache.backends.base import DEFAULT_TIMEOUT
2020
from django.db import connections
2121
from django.db import router
22+
from django.db.models import Model
2223
from django.utils.encoding import force_bytes
2324
from django.utils.module_loading import import_string
2425

@@ -69,7 +70,9 @@ def __init__(self, table: str, params: dict[str, Any]) -> None:
6970
super().__init__(params)
7071
self._table = table
7172

72-
class CacheEntry:
73+
CacheEntry: type[Model] # force Mypy to accept duck typing
74+
75+
class CacheEntry: # type: ignore [no-redef]
7376
_meta = Options(table)
7477

7578
self.cache_model_class = CacheEntry
@@ -197,7 +200,7 @@ def get_many(
197200
self, keys: Iterable[str], version: int | None = None
198201
) -> dict[str, Any]:
199202
made_key_to_key = {self.make_key(key, version=version): key for key in keys}
200-
made_keys = list(made_key_to_key.keys())
203+
made_keys: list[Any] = list(made_key_to_key.keys())
201204
for key in made_keys:
202205
self.validate_key(key)
203206

@@ -281,7 +284,7 @@ def _base_set(
281284
return True
282285
else: # mode = 'add'
283286
# Use a special code in the add query for "did insert"
284-
insert_id = cursor.lastrowid
287+
insert_id: int = cursor.lastrowid
285288
return insert_id != 444
286289

287290
_set_many_query = collapse_spaces(
@@ -431,7 +434,8 @@ def _base_delta(
431434
raise ValueError("Key '%s' not found, or not an integer" % key)
432435

433436
# New value stored in insert_id
434-
return cursor.lastrowid
437+
result: int = cursor.lastrowid
438+
return result
435439

436440
# Looks a bit tangled to turn the blob back into an int for updating, but
437441
# it works. Stores the new value for insert_id() with LAST_INSERT_ID
@@ -463,7 +467,7 @@ def touch(
463467
db = router.db_for_write(self.cache_model_class)
464468
table = connections[db].ops.quote_name(self._table)
465469
with connections[db].cursor() as cursor:
466-
affected_rows = cursor.execute(
470+
affected_rows: int = cursor.execute(
467471
self._touch_query.format(table=table), [exp, key, self._now()]
468472
)
469473
return affected_rows > 0
@@ -627,18 +631,20 @@ def delete_with_prefix(self, prefix: str, version: int | None = None) -> int:
627631
prefix = self.make_key(prefix + "%", version=version)
628632

629633
with connections[db].cursor() as cursor:
630-
return cursor.execute(
634+
result: int = cursor.execute(
631635
"""DELETE FROM {table}
632636
WHERE cache_key LIKE %s""".format(
633637
table=table
634638
),
635639
(prefix,),
636640
)
641+
return result
637642

638643
def cull(self) -> int:
639644
db = router.db_for_write(self.cache_model_class)
640645
table = connections[db].ops.quote_name(self._table)
641646

647+
num_deleted: int
642648
with connections[db].cursor() as cursor:
643649
# First, try just deleting expired keys
644650
num_deleted = cursor.execute(

0 commit comments

Comments
 (0)