Skip to content

Commit 7ee3a90

Browse files
committed
Finish hints for cache module
1 parent 1f3f315 commit 7ee3a90

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_key_func
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

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

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

6871
self.cache_model_class = CacheEntry
@@ -183,7 +186,7 @@ def get_many(
183186
self, keys: Iterable[str], version: int | None = None
184187
) -> dict[str, Any]:
185188
made_key_to_key = {self.make_key(key, version=version): key for key in keys}
186-
made_keys = list(made_key_to_key.keys())
189+
made_keys: list[Any] = list(made_key_to_key.keys())
187190
for key in made_keys:
188191
self.validate_key(key)
189192

@@ -266,7 +269,7 @@ def _base_set(
266269
return True
267270
else: # mode = 'add'
268271
# Use a special code in the add query for "did insert"
269-
insert_id = cursor.lastrowid
272+
insert_id: int = cursor.lastrowid
270273
return insert_id != 444
271274

272275
_set_many_query = collapse_spaces(
@@ -416,7 +419,8 @@ def _base_delta(
416419
raise ValueError("Key '%s' not found, or not an integer" % key)
417420

418421
# New value stored in insert_id
419-
return cursor.lastrowid
422+
result: int = cursor.lastrowid
423+
return result
420424

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

614618
with connections[db].cursor() as cursor:
615-
return cursor.execute(
619+
result: int = cursor.execute(
616620
"""DELETE FROM {table}
617621
WHERE cache_key LIKE %s""".format(
618622
table=table
619623
),
620624
(prefix,),
621625
)
626+
return result
622627

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

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

0 commit comments

Comments
 (0)