Skip to content

Commit 3928c3d

Browse files
committed
Add type-hints to adaptive/utils.py
1 parent b4ba66e commit 3928c3d

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

adaptive/utils.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1+
from __future__ import annotations
2+
13
import abc
24
import functools
35
import gzip
46
import inspect
57
import os
68
import pickle
79
import warnings
8-
from contextlib import contextmanager
10+
from contextlib import _GeneratorContextManager, contextmanager
911
from itertools import product
12+
from typing import Any, Callable, Mapping, Sequence
1013

1114
import cloudpickle
1215

1316

14-
def named_product(**items):
17+
def named_product(**items: Mapping[str, Sequence[Any]]):
1518
names = items.keys()
1619
vals = items.values()
1720
return [dict(zip(names, res)) for res in product(*vals)]
1821

1922

2023
@contextmanager
21-
def restore(*learners):
24+
def restore(*learners) -> _GeneratorContextManager:
2225
states = [learner.__getstate__() for learner in learners]
2326
try:
2427
yield
@@ -27,7 +30,7 @@ def restore(*learners):
2730
learner.__setstate__(state)
2831

2932

30-
def cache_latest(f):
33+
def cache_latest(f: Callable) -> Callable:
3134
"""Cache the latest return value of the function and add it
3235
as 'self._cache[f.__name__]'."""
3336

@@ -42,7 +45,7 @@ def wrapper(*args, **kwargs):
4245
return wrapper
4346

4447

45-
def save(fname, data, compress=True):
48+
def save(fname: str, data: Any, compress: bool = True) -> None:
4649
fname = os.path.expanduser(fname)
4750
dirname = os.path.dirname(fname)
4851
if dirname:
@@ -71,14 +74,14 @@ def save(fname, data, compress=True):
7174
return True
7275

7376

74-
def load(fname, compress=True):
77+
def load(fname: str, compress: bool = True):
7578
fname = os.path.expanduser(fname)
7679
_open = gzip.open if compress else open
7780
with _open(fname, "rb") as f:
7881
return cloudpickle.load(f)
7982

8083

81-
def copy_docstring_from(other):
84+
def copy_docstring_from(other: Callable) -> Callable:
8285
def decorator(method):
8386
return functools.wraps(other)(method)
8487

0 commit comments

Comments
 (0)