Skip to content

Commit 5210874

Browse files
committed
More type hints
1 parent 1b30f4b commit 5210874

21 files changed

Lines changed: 143 additions & 83 deletions

File tree

cubed/core/plan.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,8 @@ def execute(
591591

592592
if callbacks is not None:
593593
event = ComputeStartEventWithPlan(compute_id, dag, self)
594-
[callback.on_compute_start(event) for callback in callbacks]
594+
for callback in callbacks:
595+
callback.on_compute_start(event)
595596
executor.execute_dag(
596597
dag,
597598
compute_id=compute_id,
@@ -601,7 +602,8 @@ def execute(
601602
)
602603
if callbacks is not None:
603604
event = ComputeEndEvent(compute_id, dag)
604-
[callback.on_compute_end(event) for callback in callbacks]
605+
for callback in callbacks:
606+
callback.on_compute_end(event)
605607

606608
def visualize(
607609
self,

cubed/primitive/blockwise.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections.abc import Iterator
66
from dataclasses import dataclass
77
from functools import partial
8-
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union
8+
from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union
99

1010
import toolz
1111
import zarr
@@ -355,7 +355,7 @@ def general_blockwise(
355355
output_chunk_memory = 0
356356
target_arrays = []
357357

358-
numblocks0 = None
358+
numblocks0: Optional[Tuple[int, ...]] = None
359359
for i, target_store in enumerate(target_stores):
360360
chunks_normal = normalize_chunks(chunkss[i], shape=shapes[i], dtype=dtypes[i])
361361
chunksize = to_chunksize(chunks_normal)
@@ -465,7 +465,7 @@ def can_fuse_primitive_ops(
465465
def can_fuse_multiple_primitive_ops(
466466
name: str,
467467
primitive_op: PrimitiveOperation,
468-
predecessor_primitive_ops: List[PrimitiveOperation],
468+
predecessor_primitive_ops: List[Optional[PrimitiveOperation]],
469469
*,
470470
max_total_num_input_blocks: Optional[int] = None,
471471
) -> bool:
@@ -531,7 +531,7 @@ def can_fuse_multiple_primitive_ops(
531531
return False
532532

533533

534-
def peak_projected_mem(primitive_ops):
534+
def peak_projected_mem(primitive_ops: Iterable[Optional[PrimitiveOperation]]) -> int:
535535
"""Calculate the peak projected memory for running a series of primitive ops
536536
and retaining their return values in memory."""
537537
memory_modeller = MemoryModeller()
@@ -612,7 +612,8 @@ def fused_func(*args):
612612

613613

614614
def fuse_multiple(
615-
primitive_op: PrimitiveOperation, *predecessor_primitive_ops: PrimitiveOperation
615+
primitive_op: PrimitiveOperation,
616+
*predecessor_primitive_ops: Optional[PrimitiveOperation],
616617
) -> PrimitiveOperation:
617618
"""
618619
Fuse a blockwise operation and its predecessors into a single operation, avoiding writing to (or reading from) the targets of the predecessor operations.

cubed/primitive/rechunk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def _setup_array_rechunk(
131131

132132
read_chunks, int_chunks, write_chunks = rechunking_plan(
133133
shape,
134-
source_chunks,
134+
source_chunks, # type: ignore
135135
target_chunks,
136136
itemsize(dtype),
137137
max_mem,

cubed/runtime/asyncio.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async def async_map_unordered(
3939
batch_size: Optional[int] = None,
4040
return_stats: bool = False,
4141
name: Optional[str] = None,
42-
**kwargs,
42+
**kwargs: Any,
4343
) -> AsyncIterator[Any]:
4444
"""
4545
Asynchronous parallel map over an iterable input, with support for backups and batching.
@@ -128,7 +128,7 @@ async def async_map_dag(
128128
dag: MultiDiGraph,
129129
callbacks: Optional[Sequence[Callback]] = None,
130130
compute_arrays_in_parallel: Optional[bool] = None,
131-
**kwargs,
131+
**kwargs: Any,
132132
) -> None:
133133
"""
134134
Asynchronous parallel map over multiple pipelines from a DAG, with support for backups and batching.
@@ -170,7 +170,7 @@ def pipeline_to_stream(
170170
create_futures_func: Callable,
171171
name: str,
172172
pipeline: CubedPipeline,
173-
**kwargs,
173+
**kwargs: Any,
174174
) -> Stream:
175175
"""
176176
Turn a pipeline into an asynchronous stream of results.

cubed/runtime/create.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
from typing import Optional
1+
from typing import Any, Dict, Optional
22

33
from cubed.runtime.types import Executor
44

55

6-
def create_executor(name: str, executor_options: Optional[dict] = None) -> Executor:
6+
def create_executor(
7+
name: str, executor_options: Optional[Dict[Any, Any]] = None
8+
) -> Executor:
79
"""Create an executor from an executor name."""
810
executor_options = executor_options or {}
911
if name == "beam":

cubed/runtime/executors/coiled.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def make_coiled_function(func, name, coiled_kwargs):
2121
class CoiledExecutor(DagExecutor):
2222
"""An execution engine that uses Coiled Functions."""
2323

24-
def __init__(self, **kwargs):
24+
def __init__(self, **kwargs: Any) -> None:
2525
super().__init__(**kwargs)
2626

2727
@property

cubed/runtime/executors/dask.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def create_futures_func(input, **kwargs):
5858
class DaskExecutor(DagExecutor):
5959
"""An execution engine that uses Dask Distributed's async API."""
6060

61-
def __init__(self, **kwargs):
61+
def __init__(self, **kwargs: Any) -> None:
6262
super().__init__(**kwargs)
6363

6464
@property

cubed/runtime/executors/lithops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def standardise_lithops_stats(name: str, future: RetryingFuture) -> Dict[str, An
256256
class LithopsExecutor(DagExecutor):
257257
"""An execution engine that uses Lithops."""
258258

259-
def __init__(self, **kwargs):
259+
def __init__(self, **kwargs: Any) -> None:
260260
super().__init__(**kwargs)
261261

262262
@property

cubed/runtime/executors/local.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def execute_dag(
4242
callbacks: Optional[Sequence[Callback]] = None,
4343
spec: Optional[Spec] = None,
4444
compute_id: Optional[str] = None,
45-
**kwargs,
45+
**kwargs: Any,
4646
) -> None:
4747
for name, node in visit_nodes(dag):
4848
handle_operation_start_callbacks(callbacks, name)
@@ -57,7 +57,8 @@ def execute_dag(
5757
)
5858
if callbacks is not None:
5959
event = TaskEndEvent(name=name, result=result)
60-
[callback.on_task_end(event) for callback in callbacks]
60+
for callback in callbacks:
61+
callback.on_task_end(event)
6162
handle_operation_end_callbacks(callbacks, name)
6263

6364

@@ -81,7 +82,7 @@ def unpickle_and_call(f, inp, **kwargs):
8182
return f(inp, **kwargs)
8283

8384

84-
def check_runtime_memory(spec, max_workers):
85+
def check_runtime_memory(spec: Optional[Spec], max_workers: int) -> None:
8586
allowed_mem = spec.allowed_mem if spec is not None else None
8687
total_mem = psutil.virtual_memory().total
8788
if allowed_mem is not None:
@@ -113,7 +114,7 @@ def create_futures_func(input, **kwargs):
113114
class ThreadsExecutor(DagExecutor):
114115
"""An execution engine that uses Python asyncio."""
115116

116-
def __init__(self, **kwargs):
117+
def __init__(self, **kwargs: Any) -> None:
117118
super().__init__(**kwargs)
118119

119120
# Tell NumPy to use a single thread
@@ -133,7 +134,7 @@ def execute_dag(
133134
callbacks: Optional[Sequence[Callback]] = None,
134135
spec: Optional[Spec] = None,
135136
compute_id: Optional[str] = None,
136-
**kwargs,
137+
**kwargs: Any,
137138
) -> None:
138139
merged_kwargs = {**self.kwargs, **kwargs}
139140
asyncio_run(
@@ -152,7 +153,7 @@ async def _async_execute_dag(
152153
callbacks: Optional[Sequence[Callback]] = None,
153154
spec: Optional[Spec] = None,
154155
compute_arrays_in_parallel: Optional[bool] = None,
155-
**kwargs,
156+
**kwargs: Any,
156157
) -> None:
157158
max_workers = kwargs.pop("max_workers", os.cpu_count())
158159
if spec is not None:
@@ -203,7 +204,7 @@ def create_futures_func(input, **kwargs):
203204
class ProcessesExecutor(DagExecutor):
204205
"""An execution engine that uses local processes."""
205206

206-
def __init__(self, **kwargs):
207+
def __init__(self, **kwargs: Any) -> None:
207208
super().__init__(**kwargs)
208209

209210
# Tell NumPy to use a single thread
@@ -223,7 +224,7 @@ def execute_dag(
223224
callbacks: Optional[Sequence[Callback]] = None,
224225
spec: Optional[Spec] = None,
225226
compute_id: Optional[str] = None,
226-
**kwargs,
227+
**kwargs: Any,
227228
) -> None:
228229
merged_kwargs = {**self.kwargs, **kwargs}
229230
asyncio_run(
@@ -242,7 +243,7 @@ async def _async_execute_dag(
242243
callbacks: Optional[Sequence[Callback]] = None,
243244
spec: Optional[Spec] = None,
244245
compute_arrays_in_parallel: Optional[bool] = None,
245-
**kwargs,
246+
**kwargs: Any,
246247
) -> None:
247248
max_workers = kwargs.pop("max_workers", os.cpu_count())
248249
if spec is not None:

cubed/runtime/executors/modal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def create_futures_func(input, **kwargs):
140140
class ModalExecutor(DagExecutor):
141141
"""An execution engine that uses Modal's async API."""
142142

143-
def __init__(self, **kwargs):
143+
def __init__(self, **kwargs: Any) -> None:
144144
super().__init__(**kwargs)
145145

146146
@property

0 commit comments

Comments
 (0)