Skip to content

Commit bd1be12

Browse files
committed
Finish hints for cache module
1 parent f0cbce0 commit bd1be12

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

@@ -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)