Skip to content

Commit 351cfef

Browse files
author
Fabien Coelho
committed
simpler typing
1 parent 692817a commit 351cfef

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

CacheToolsUtils.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
CacheTools Utilities. This code is public domain.
33
"""
44

5-
from typing import Any, Callable, MutableMapping as MutMap
5+
from typing import Any, Callable, MutableMapping
66
import abc
77

88
import cachetools
@@ -25,7 +25,7 @@
2525
class _MutMapMix:
2626
"""Convenient MutableMapping Mixin, forward to _cache."""
2727

28-
_cache: MutMap
28+
_cache: MutableMapping
2929

3030
def __contains__(self, key):
3131
return self._cache.__contains__(key)
@@ -102,15 +102,15 @@ def dbsize(self, *args, **kwargs):
102102
# CACHETOOLS EXTENSIONS
103103
#
104104

105-
class DebugCache(MutMap):
105+
class DebugCache(MutableMapping):
106106
"""Debug class.
107107
108108
:param cache: actual cache
109109
:param log: logging instance
110110
:param name: name of instance for output
111111
"""
112112

113-
def __init__(self, cache: MutMap, log: logging.Logger, name="cache"):
113+
def __init__(self, cache: MutableMapping, log: logging.Logger, name="cache"):
114114
self._cache = cache
115115
self._log = log
116116
self._name = name
@@ -152,7 +152,7 @@ def reset(self): # pragma: no cover
152152
return self._cache.reset() # type: ignore
153153

154154

155-
class DictCache(_MutMapMix, MutMap):
155+
class DictCache(_MutMapMix, MutableMapping):
156156
"""Cache class based on dict."""
157157

158158
def __init__(self):
@@ -162,7 +162,7 @@ def clear(self):
162162
self._cache.clear()
163163

164164

165-
class LockedCache(_MutMapMix, _StatsMix, _RedisMix, MutMap):
165+
class LockedCache(_MutMapMix, _StatsMix, _RedisMix, MutableMapping):
166166
"""Cache class with a lock.
167167
168168
:param cache: actual cache.
@@ -178,7 +178,7 @@ class LockedCache(_MutMapMix, _StatsMix, _RedisMix, MutMap):
178178
cache = ctu.LockedCache(ct.LFUCache(), threading.RLock())
179179
"""
180180

181-
def __init__(self, cache: MutMap, lock):
181+
def __init__(self, cache: MutableMapping, lock):
182182
self._cache = cache
183183
self._lock = lock
184184

@@ -199,14 +199,14 @@ def __delitem__(self, key):
199199
return self._cache.__delitem__(key)
200200

201201

202-
class PrefixedCache(_KeyMutMapMix, _StatsMix, MutMap):
202+
class PrefixedCache(_KeyMutMapMix, _StatsMix, MutableMapping):
203203
"""Cache class to add a key prefix.
204204
205205
:param cache: actual cache.
206206
:param prefix: prefix to prepend to keys.
207207
"""
208208

209-
def __init__(self, cache: MutMap, prefix: str|bytes = ""):
209+
def __init__(self, cache: MutableMapping, prefix: str|bytes = ""):
210210
self._prefix = prefix
211211
self._cache = cache
212212
# dynamic cast
@@ -220,7 +220,7 @@ def _key(self, key: Any) -> Any:
220220
return self._prefix + self._cast(key) # type: ignore
221221

222222

223-
class StatsCache(_MutMapMix, MutMap):
223+
class StatsCache(_MutMapMix, MutableMapping):
224224
"""Cache class to keep stats.
225225
226226
:param cache: actual cache.
@@ -237,7 +237,7 @@ class StatsCache(_MutMapMix, MutMap):
237237
However, this only works for its own classes.
238238
"""
239239

240-
def __init__(self, cache: MutMap):
240+
def __init__(self, cache: MutableMapping):
241241
self._cache = cache
242242
self.reset()
243243

@@ -267,15 +267,15 @@ def clear(self):
267267
return self._cache.clear()
268268

269269

270-
class TwoLevelCache(_MutMapMix, MutMap):
270+
class TwoLevelCache(_MutMapMix, MutableMapping):
271271
"""Two-Level Cache class for CacheTools.
272272
273273
:param cache: first (smaller, shorter TTL) cache
274274
:param cache2: second (larger, longer TTL) cache
275275
:param resilient: whether to ignore cache2 failures
276276
"""
277277

278-
def __init__(self, cache: MutMap, cache2: MutMap, resilient=False):
278+
def __init__(self, cache: MutableMapping, cache2: MutableMapping, resilient=False):
279279
self._resilient = resilient
280280
self._cache = cache
281281
self._cache2 = cache2
@@ -370,10 +370,10 @@ def cache_del(*args, **kwargs):
370370

371371

372372
# short generator type name
373-
MapGen = Callable[[MutMap, str], MutMap]
373+
MapGen = Callable[[MutableMapping, str], MutableMapping]
374374

375375

376-
def cacheMethods(cache: MutMap, obj: Any, gen: MapGen = PrefixedCache, **funs):
376+
def cacheMethods(cache: MutableMapping, obj: Any, gen: MapGen = PrefixedCache, **funs):
377377
"""Cache some object methods.
378378
379379
:param cache: cache to use.
@@ -394,7 +394,7 @@ def cacheMethods(cache: MutMap, obj: Any, gen: MapGen = PrefixedCache, **funs):
394394

395395

396396
def cacheFunctions(
397-
cache: MutMap, globs: MutMap[str, Any], gen: MapGen = PrefixedCache, **funs
397+
cache: MutableMapping, globs: MutableMapping[str, Any], gen: MapGen = PrefixedCache, **funs
398398
):
399399
"""Cache some global functions, with a prefix.
400400
@@ -445,7 +445,7 @@ def deserialize(self, key, value, flag):
445445
raise Exception("Unknown serialization format")
446446

447447

448-
class MemCached(_KeyMutMapMix, MutMap):
448+
class MemCached(_KeyMutMapMix, MutableMapping):
449449
"""MemCached-compatible wrapper class for cachetools with key encoding.
450450
451451
:param cache: actual memcached cache.
@@ -528,7 +528,7 @@ class StatsMemCached(MemCached):
528528
#
529529

530530

531-
class RedisCache(MutMap):
531+
class RedisCache(MutableMapping):
532532
"""Redis TTL-ed wrapper for cachetools (``redis``).
533533
534534
:param cache: actual redis cache.

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ PYTEST = pytest --log-level=debug --capture=tee-sys
3535
PYTOPT =
3636

3737
.PHONY: check
38-
check: check.mypy check.flake8 check.pytest check.coverage check.docs
38+
check: check.mypy check.pyright check.flake8 check.pytest check.coverage check.docs
3939

4040
.PHONY: check.mypy
4141
check.mypy: venv

0 commit comments

Comments
 (0)