Skip to content

Commit f6f3424

Browse files
committed
Add mypy to dircache
1 parent f248672 commit f6f3424

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

fsspec/dircache.py

+24-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
from __future__ import annotations
12
import time
2-
from collections.abc import MutableMapping
3+
from typing import MutableMapping, List, TypedDict, Iterator, Any
4+
35
from functools import lru_cache
46

57

6-
class DirCache(MutableMapping):
8+
class DirEntry(TypedDict):
9+
name: str
10+
size: int
11+
type: str
12+
13+
14+
class DirCache(MutableMapping[str, List[DirEntry]]):
715
"""
816
Caching of directory listings, in a structure like::
917
@@ -26,10 +34,10 @@ class DirCache(MutableMapping):
2634

2735
def __init__(
2836
self,
29-
use_listings_cache=True,
30-
listings_expiry_time=None,
31-
max_paths=None,
32-
**kwargs,
37+
use_listings_cache: bool = True,
38+
listings_expiry_time: float | None = None,
39+
max_paths: int | None = None,
40+
**kwargs: Any,
3341
):
3442
"""
3543
@@ -45,36 +53,36 @@ def __init__(
4553
The number of most recent listings that are considered valid; 'recent'
4654
refers to when the entry was set.
4755
"""
48-
self._cache = {}
49-
self._times = {}
56+
self._cache: dict[str, list[DirEntry]] = {}
57+
self._times: dict[str, float] = {}
5058
if max_paths:
5159
self._q = lru_cache(max_paths + 1)(lambda key: self._cache.pop(key, None))
5260
self.use_listings_cache = use_listings_cache
5361
self.listings_expiry_time = listings_expiry_time
5462
self.max_paths = max_paths
5563

56-
def __getitem__(self, item):
64+
def __getitem__(self, item: str) -> list[DirEntry]:
5765
if self.listings_expiry_time is not None:
5866
if self._times.get(item, 0) - time.time() < -self.listings_expiry_time:
5967
del self._cache[item]
6068
if self.max_paths:
6169
self._q(item)
6270
return self._cache[item] # maybe raises KeyError
6371

64-
def clear(self):
72+
def clear(self) -> None:
6573
self._cache.clear()
6674

67-
def __len__(self):
75+
def __len__(self) -> int:
6876
return len(self._cache)
6977

70-
def __contains__(self, item):
78+
def __contains__(self, item: object) -> bool:
7179
try:
72-
self[item]
80+
self[item] # type: ignore[index]
7381
return True
7482
except KeyError:
7583
return False
7684

77-
def __setitem__(self, key, value):
85+
def __setitem__(self, key: str, value: List[DirEntry]) -> None:
7886
if not self.use_listings_cache:
7987
return
8088
if self.max_paths:
@@ -83,10 +91,10 @@ def __setitem__(self, key, value):
8391
if self.listings_expiry_time is not None:
8492
self._times[key] = time.time()
8593

86-
def __delitem__(self, key):
94+
def __delitem__(self, key) -> None:
8795
del self._cache[key]
8896

89-
def __iter__(self):
97+
def __iter__(self) -> Iterator[str]:
9098
entries = list(self._cache)
9199

92100
return (k for k in entries if k in self)

setup.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ exclude = (test.*|conftest)\.py$
5757
[mypy-fsspec.caching]
5858
check_untyped_defs = True
5959

60+
[mypy-fsspec.dircache]
61+
check_untyped_defs = True
62+
6063
[mypy-fsspec.mapping]
6164
check_untyped_defs = True
6265

0 commit comments

Comments
 (0)