Skip to content

Commit 13fc802

Browse files
committed
Fix some mypy errors, improve type-hint and fix the FIFOCache.get docsting
!test
1 parent 6552bfe commit 13fc802

File tree

5 files changed

+75
-69
lines changed

5 files changed

+75
-69
lines changed

pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,6 @@ asyncio_default_fixture_loop_scope = "function"
5050
python-source = "python"
5151
features = ["pyo3/extension-module"]
5252
module-name = "cachebox._core"
53+
54+
[tool.mypy]
55+
disable_error_code = "attr-defined, type-arg, no-untyped-def, no-any-return"

python/cachebox/_cachebox.py

+26-26
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
DT = typing.TypeVar("DT")
1111

1212

13-
def _items_to_str(items, length):
13+
def _items_to_str(items: typing.Iterable[typing.Any], length) -> str:
1414
if length <= 50:
1515
return "{" + ", ".join(f"{k!r}: {v!r}" for k, v in items) + "}"
1616

1717
c = 0
1818
left = []
1919

2020
while c < length:
21-
k, v = next(items)
21+
k, v = next(items) # type: ignore[call-overload]
2222

2323
if c <= 50:
2424
left.append(f"{k!r}: {v!r}")
@@ -72,7 +72,7 @@ class Cache(BaseCacheImpl[KT, VT]):
7272
def __init__(
7373
self,
7474
maxsize: int,
75-
iterable: typing.Union[dict, typing.Iterable[tuple]] = None,
75+
iterable: typing.Union[dict, typing.Iterable[tuple], None] = None,
7676
*,
7777
capacity: int = 0,
7878
) -> None:
@@ -148,7 +148,7 @@ def get(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT,
148148
try:
149149
return self._raw.get(key)
150150
except _core.CoreKeyError:
151-
return default
151+
return default # type: ignore[return-value]
152152

153153
def pop(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT, DT]:
154154
"""
@@ -157,7 +157,7 @@ def pop(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT,
157157
try:
158158
return self._raw.remove(key)
159159
except _core.CoreKeyError:
160-
return default
160+
return default # type: ignore[return-value]
161161

162162
def setdefault(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT, DT]:
163163
"""
@@ -169,7 +169,7 @@ def setdefault(self, key: KT, default: typing.Optional[DT] = None) -> typing.Uni
169169
def popitem(self) -> typing.NoReturn: # pragma: no cover
170170
raise NotImplementedError()
171171

172-
def drain(self) -> typing.NoReturn: # pragma: no cover
172+
def drain(self, n: int) -> typing.NoReturn: # pragma: no cover
173173
raise NotImplementedError()
174174

175175
def update(self, iterable: typing.Union[dict, typing.Iterable[tuple]]) -> None:
@@ -370,7 +370,7 @@ def insert(self, key: KT, value: VT) -> typing.Optional[VT]:
370370
return self._raw.insert(key, value)
371371

372372
def get(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT, DT]:
373-
""" "
373+
"""
374374
Retrieves the value for a given key from the cache.
375375
376376
Returns the value associated with the key if present, otherwise returns the specified default value.
@@ -386,7 +386,7 @@ def get(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT,
386386
try:
387387
return self._raw.get(key)
388388
except _core.CoreKeyError:
389-
return default
389+
return default # type: ignore[return-value]
390390

391391
def pop(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT, DT]:
392392
"""
@@ -395,7 +395,7 @@ def pop(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT,
395395
try:
396396
return self._raw.remove(key)
397397
except _core.CoreKeyError:
398-
return default
398+
return default # type: ignore[return-value] # type: ignore[return-value]
399399

400400
def setdefault(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT, DT]:
401401
"""
@@ -647,7 +647,7 @@ def get(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT,
647647
try:
648648
return self._raw.get(key)
649649
except _core.CoreKeyError:
650-
return default
650+
return default # type: ignore[return-value]
651651

652652
def pop(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT, DT]:
653653
"""
@@ -656,7 +656,7 @@ def pop(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT,
656656
try:
657657
return self._raw.remove(key)
658658
except _core.CoreKeyError:
659-
return default
659+
return default # type: ignore[return-value]
660660

661661
def setdefault(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT, DT]:
662662
"""
@@ -898,7 +898,7 @@ def peek(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT,
898898
try:
899899
return self._raw.peek(key)
900900
except _core.CoreKeyError:
901-
return default
901+
return default # type: ignore[return-value]
902902

903903
def get(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT, DT]:
904904
"""
@@ -917,7 +917,7 @@ def get(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT,
917917
try:
918918
return self._raw.get(key)
919919
except _core.CoreKeyError:
920-
return default
920+
return default # type: ignore[return-value]
921921

922922
def pop(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT, DT]:
923923
"""
@@ -926,7 +926,7 @@ def pop(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT,
926926
try:
927927
return self._raw.remove(key)
928928
except _core.CoreKeyError:
929-
return default
929+
return default # type: ignore[return-value]
930930

931931
def setdefault(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT, DT]:
932932
"""
@@ -1168,7 +1168,7 @@ def peek(
11681168
try:
11691169
return self._raw.peek(key)
11701170
except _core.CoreKeyError:
1171-
return default
1171+
return default # type: ignore[return-value]
11721172

11731173
def get(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT, DT]:
11741174
"""
@@ -1187,7 +1187,7 @@ def get(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT,
11871187
try:
11881188
return self._raw.get(key)
11891189
except _core.CoreKeyError:
1190-
return default
1190+
return default # type: ignore[return-value]
11911191

11921192
def pop(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT, DT]:
11931193
"""
@@ -1196,7 +1196,7 @@ def pop(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT,
11961196
try:
11971197
return self._raw.remove(key)
11981198
except _core.CoreKeyError:
1199-
return default
1199+
return default # type: ignore[return-value]
12001200

12011201
def setdefault(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT, DT]:
12021202
"""
@@ -1468,7 +1468,7 @@ def get(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT,
14681468
try:
14691469
return self._raw.get(key).value()
14701470
except _core.CoreKeyError:
1471-
return default
1471+
return default # type: ignore[return-value]
14721472

14731473
def get_with_expire(
14741474
self, key: KT, default: typing.Optional[DT] = None
@@ -1490,7 +1490,7 @@ def get_with_expire(
14901490
try:
14911491
pair = self._raw.get(key)
14921492
except _core.CoreKeyError:
1493-
return default, 0.0
1493+
return default, 0.0 # type: ignore[return-value]
14941494
else:
14951495
return (pair.value(), pair.duration())
14961496

@@ -1501,7 +1501,7 @@ def pop(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT,
15011501
try:
15021502
return self._raw.remove(key).value()
15031503
except _core.CoreKeyError:
1504-
return default
1504+
return default # type: ignore[return-value]
15051505

15061506
def pop_with_expire(
15071507
self, key: KT, default: typing.Optional[DT] = None
@@ -1522,7 +1522,7 @@ def pop_with_expire(
15221522
try:
15231523
pair = self._raw.remove(key)
15241524
except _core.CoreKeyError:
1525-
return default, 0.0
1525+
return default, 0.0 # type: ignore[return-value]
15261526
else:
15271527
return (pair.value(), pair.duration())
15281528

@@ -1833,7 +1833,7 @@ def get(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT,
18331833
try:
18341834
return self._raw.get(key).value()
18351835
except _core.CoreKeyError:
1836-
return default
1836+
return default # type: ignore[return-value]
18371837

18381838
def get_with_expire(
18391839
self, key: KT, default: typing.Optional[DT] = None
@@ -1855,7 +1855,7 @@ def get_with_expire(
18551855
try:
18561856
pair = self._raw.get(key)
18571857
except _core.CoreKeyError:
1858-
return default, 0.0
1858+
return default, 0.0 # type: ignore[return-value]
18591859
else:
18601860
return (pair.value(), pair.duration())
18611861

@@ -1866,7 +1866,7 @@ def pop(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT,
18661866
try:
18671867
return self._raw.remove(key).value()
18681868
except _core.CoreKeyError:
1869-
return default
1869+
return default # type: ignore[return-value]
18701870

18711871
def pop_with_expire(
18721872
self, key: KT, default: typing.Optional[DT] = None
@@ -1887,7 +1887,7 @@ def pop_with_expire(
18871887
try:
18881888
pair = self._raw.remove(key)
18891889
except _core.CoreKeyError:
1890-
return default, 0.0
1890+
return default, 0.0 # type: ignore[return-value]
18911891
else:
18921892
return (pair.value(), pair.duration())
18931893

@@ -1939,7 +1939,7 @@ def popitem(self) -> typing.Tuple[KT, VT]:
19391939
"""
19401940
try:
19411941
val = self._raw.popitem()
1942-
except _core.CoreKeyError: # pragma: no cover
1942+
except _core.CoreKeyError: # pragma: no cover
19431943
raise KeyError() from None
19441944
else:
19451945
return val.pack2()

python/cachebox/_core.pyi

+13-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class BaseCacheImpl(typing.Generic[KT, VT]):
3232
capacity: int = ...,
3333
) -> None: ...
3434
@staticmethod
35-
def __class_getitem__(*args) -> None: ...
35+
def __class_getitem__(*args: typing.Any) -> None: ...
3636
@property
3737
def maxsize(self) -> int: ...
3838
def __len__(self) -> int: ...
@@ -41,19 +41,21 @@ class BaseCacheImpl(typing.Generic[KT, VT]):
4141
def __contains__(self, key: KT) -> bool: ...
4242
def __setitem__(self, key: KT, value: VT) -> None: ...
4343
def __getitem__(self, key: KT) -> VT: ...
44-
def __delitem__(self, key: KT) -> VT: ...
44+
def __delitem__(self, key: KT) -> None: ...
4545
def __str__(self) -> str: ...
4646
def __iter__(self) -> typing.Iterator[KT]: ...
47-
def __eq__(self, other) -> bool: ...
48-
def __ne__(self, other) -> bool: ...
47+
def __eq__(self, other: typing.Any) -> bool: ...
48+
def __ne__(self, other: typing.Any) -> bool: ...
4949
def capacity(self) -> int: ...
5050
def is_full(self) -> bool: ...
5151
def is_empty(self) -> bool: ...
52-
def insert(self, key: KT, value: VT, *args, **kwargs) -> typing.Optional[VT]: ...
53-
def get(self, key: KT, default: DT = None) -> typing.Union[VT, DT]: ...
54-
def pop(self, key: KT, default: DT = None) -> typing.Union[VT, DT]: ...
52+
def insert(
53+
self, key: KT, value: VT, *args: typing.Any, **kwargs: typing.Any
54+
) -> typing.Optional[VT]: ...
55+
def get(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT, DT]: ...
56+
def pop(self, key: KT, default: typing.Optional[DT] = None) -> typing.Union[VT, DT]: ...
5557
def setdefault(
56-
self, key: KT, default: typing.Optional[DT] = None, *args, **kwargs
58+
self, key: KT, default: typing.Optional[DT] = None, *args: typing.Any, **kwargs: typing.Any
5759
) -> typing.Optional[VT | DT]: ...
5860
def popitem(self) -> typing.Tuple[KT, VT]: ...
5961
def drain(self, n: int) -> int: ...
@@ -62,12 +64,12 @@ class BaseCacheImpl(typing.Generic[KT, VT]):
6264
def update(
6365
self,
6466
iterable: typing.Union[typing.Iterable[typing.Tuple[KT, VT]], typing.Dict[KT, VT]],
65-
*args,
66-
**kwargs,
67+
*args: typing.Any,
68+
**kwargs: typing.Any,
6769
) -> None: ...
6870
def keys(self) -> typing.Iterable[KT]: ...
6971
def values(self) -> typing.Iterable[VT]: ...
7072
def items(self) -> typing.Iterable[typing.Tuple[KT, VT]]: ...
7173
def __copy__(self) -> "BaseCacheImpl[KT, VT]": ...
72-
def __deepcopy__(self, memo) -> "BaseCacheImpl[KT, VT]": ...
74+
def __deepcopy__(self, memo: typing.Dict[str, object]) -> "BaseCacheImpl[KT, VT]": ...
7375
def copy(self) -> "BaseCacheImpl[KT, VT]": ...

0 commit comments

Comments
 (0)