Skip to content

Commit 22fc94a

Browse files
committed
Finish hints for cache module
1 parent 148d29c commit 22fc94a

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
@@ -18,6 +18,7 @@
1818
from django.core.cache.backends.base import default_key_func
1919
from django.db import connections
2020
from django.db import router
21+
from django.db.models import Model
2122
from django.utils.encoding import force_bytes
2223
from django.utils.module_loading import import_string
2324

@@ -61,7 +62,9 @@ def __init__(self, table: str, params: dict[str, Any]) -> None:
6162
super().__init__(params)
6263
self._table = table
6364

64-
class CacheEntry:
65+
CacheEntry: type[Model] # force Mypy to accept duck typing
66+
67+
class CacheEntry: # type: ignore [no-redef]
6568
_meta = Options(table)
6669

6770
self.cache_model_class = CacheEntry
@@ -182,7 +185,7 @@ def get_many(
182185
self, keys: Iterable[str], version: int | None = None
183186
) -> dict[str, Any]:
184187
made_key_to_key = {self.make_key(key, version=version): key for key in keys}
185-
made_keys = list(made_key_to_key.keys())
188+
made_keys: list[Any] = list(made_key_to_key.keys())
186189
for key in made_keys:
187190
self.validate_key(key)
188191

@@ -265,7 +268,7 @@ def _base_set(
265268
return True
266269
else: # mode = 'add'
267270
# Use a special code in the add query for "did insert"
268-
insert_id = cursor.lastrowid
271+
insert_id: int = cursor.lastrowid
269272
return insert_id != 444
270273

271274
_set_many_query = collapse_spaces(
@@ -415,7 +418,8 @@ def _base_delta(
415418
raise ValueError("Key '%s' not found, or not an integer" % key)
416419

417420
# New value stored in insert_id
418-
return cursor.lastrowid
421+
result: int = cursor.lastrowid
422+
return result
419423

420424
# Looks a bit tangled to turn the blob back into an int for updating, but
421425
# it works. Stores the new value for insert_id() with LAST_INSERT_ID
@@ -447,7 +451,7 @@ def touch(
447451
db = router.db_for_write(self.cache_model_class)
448452
table = connections[db].ops.quote_name(self._table)
449453
with connections[db].cursor() as cursor:
450-
affected_rows = cursor.execute(
454+
affected_rows: int = cursor.execute(
451455
self._touch_query.format(table=table), [exp, key, self._now()]
452456
)
453457
return affected_rows > 0
@@ -611,18 +615,20 @@ def delete_with_prefix(self, prefix: str, version: int | None = None) -> int:
611615
prefix = self.make_key(prefix + "%", version=version)
612616

613617
with connections[db].cursor() as cursor:
614-
return cursor.execute(
618+
result: int = cursor.execute(
615619
"""DELETE FROM {table}
616620
WHERE cache_key LIKE %s""".format(
617621
table=table
618622
),
619623
(prefix,),
620624
)
625+
return result
621626

622627
def cull(self) -> int:
623628
db = router.db_for_write(self.cache_model_class)
624629
table = connections[db].ops.quote_name(self._table)
625630

631+
num_deleted: int
626632
with connections[db].cursor() as cursor:
627633
# First, try just deleting expired keys
628634
num_deleted = cursor.execute(

0 commit comments

Comments
 (0)