Skip to content

Commit

Permalink
zict type annotations (#5905)
Browse files Browse the repository at this point in the history
  • Loading branch information
crusaderky authored Mar 10, 2022
1 parent 30f0b60 commit f9d2f91
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ repos:
- numpy
- dask
- tornado
- zict
10 changes: 6 additions & 4 deletions distributed/spill.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections.abc import Mapping
from contextlib import contextmanager
from functools import partial
from typing import Any, Literal, NamedTuple
from typing import Any, Literal, NamedTuple, cast

import zict
from packaging.version import parse as parse_version
Expand All @@ -32,6 +32,7 @@ def __sub__(self, other: SpilledSize) -> SpilledSize: # type: ignore
return SpilledSize(self.memory - other.memory, self.disk - other.disk)


# zict.Buffer[str, Any] requires zict >= 2.2.0
class SpillBuffer(zict.Buffer):
"""MutableMapping that automatically spills out dask key/value pairs to disk when
the total size of the stored data exceeds the target. If max_spill is provided the
Expand Down Expand Up @@ -171,7 +172,7 @@ def evict(self) -> int:
try:
with self.handle_errors(None):
_, _, weight = self.fast.evict()
return weight
return cast(int, weight)
except HandledError:
return -1

Expand Down Expand Up @@ -203,7 +204,7 @@ def spilled_total(self) -> SpilledSize:
The two may differ substantially, e.g. if sizeof() is inaccurate or in case of
compression.
"""
return self.slow.total_weight
return cast(Slow, self.slow).total_weight


def _in_memory_weight(key: str, value: Any) -> int:
Expand Down Expand Up @@ -240,7 +241,8 @@ def __init__(self, spill_directory: str, max_weight: int | Literal[False] = Fals

def __setitem__(self, key: str, value: Any) -> None:
try:
pickled = self.dump(value)
# FIXME https://github.com/python/mypy/issues/708
pickled = self.dump(value) # type: ignore
except Exception as e:
# zict.LRU ensures that the key remains in fast if we raise.
# Wrap the exception so that it's recognizable by SpillBuffer,
Expand Down
2 changes: 1 addition & 1 deletion distributed/tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
try:
import zict
except ImportError:
zict = None
zict = None # type: ignore

requires_zict = pytest.mark.skipif(not zict, reason="requires zict")
requires_zict_210 = pytest.mark.skipif(
Expand Down

0 comments on commit f9d2f91

Please sign in to comment.