Skip to content

Commit a4cdc44

Browse files
committed
Finish hints for cache module
1 parent aa26296 commit a4cdc44

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
@@ -12,6 +12,7 @@
1212

1313
from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache, default_key_func
1414
from django.db import connections, router
15+
from django.db.models import Model
1516
from django.utils.encoding import force_bytes
1617
from django.utils.module_loading import import_string
1718

@@ -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
@@ -189,7 +192,7 @@ def get_many(
189192
self, keys: Iterable[str], version: int | None = None
190193
) -> dict[str, Any]:
191194
made_key_to_key = {self.make_key(key, version=version): key for key in keys}
192-
made_keys = list(made_key_to_key.keys())
195+
made_keys: list[Any] = list(made_key_to_key.keys())
193196
for key in made_keys:
194197
self.validate_key(key)
195198

@@ -273,7 +276,7 @@ def _base_set(
273276
return True
274277
else: # mode = 'add'
275278
# Use a special code in the add query for "did insert"
276-
insert_id = cursor.lastrowid
279+
insert_id: int = cursor.lastrowid
277280
return insert_id != 444
278281

279282
_set_many_query = collapse_spaces(
@@ -423,7 +426,8 @@ def _base_delta(
423426
raise ValueError("Key '%s' not found, or not an integer" % key)
424427

425428
# New value stored in insert_id
426-
return cursor.lastrowid
429+
result: int = cursor.lastrowid
430+
return result
427431

428432
# Looks a bit tangled to turn the blob back into an int for updating, but
429433
# it works. Stores the new value for insert_id() with LAST_INSERT_ID
@@ -455,7 +459,7 @@ def touch(
455459
db = router.db_for_write(self.cache_model_class)
456460
table = connections[db].ops.quote_name(self._table)
457461
with connections[db].cursor() as cursor:
458-
affected_rows = cursor.execute(
462+
affected_rows: int = cursor.execute(
459463
self._touch_query.format(table=table), [exp, key, self._now()]
460464
)
461465
return affected_rows > 0
@@ -619,18 +623,20 @@ def delete_with_prefix(self, prefix: str, version: int | None = None) -> int:
619623
prefix = self.make_key(prefix + "%", version=version)
620624

621625
with connections[db].cursor() as cursor:
622-
return cursor.execute(
626+
result: int = cursor.execute(
623627
"""DELETE FROM {table}
624628
WHERE cache_key LIKE %s""".format(
625629
table=table
626630
),
627631
(prefix,),
628632
)
633+
return result
629634

630635
def cull(self) -> int:
631636
db = router.db_for_write(self.cache_model_class)
632637
table = connections[db].ops.quote_name(self._table)
633638

639+
num_deleted: int
634640
with connections[db].cursor() as cursor:
635641
# First, try just deleting expired keys
636642
num_deleted = cursor.execute(

0 commit comments

Comments
 (0)