|
5 | 5 | import abc |
6 | 6 | import logging |
7 | 7 | from collections import abc as cabc |
| 8 | +from typing import Callable |
8 | 9 |
|
9 | 10 | from boltons.setutils import IndexedSet as iset |
10 | 11 |
|
@@ -110,10 +111,12 @@ class FunctionalOperation(Operation): |
110 | 111 | """ |
111 | 112 | An Operation performing a callable (ie function, method, lambda). |
112 | 113 |
|
113 | | - Use :class:`operation()` to build instances of this class instead. |
| 114 | + Use :class:`operation()` factory to build instances of this class instead. |
114 | 115 | """ |
115 | 116 |
|
116 | | - def __init__(self, fn, name, needs=None, provides=None, *, returns_dict=None): |
| 117 | + def __init__( |
| 118 | + self, fn: Callable, name, needs=None, provides=None, *, returns_dict=None |
| 119 | + ): |
117 | 120 | self.fn = fn |
118 | 121 | self.returns_dict = returns_dict |
119 | 122 | ## Set op-data early, for repr() to work on errors. |
@@ -178,13 +181,25 @@ def _zip_results_with_provides(self, results, real_provides: iset) -> dict: |
178 | 181 |
|
179 | 182 | def compute(self, named_inputs, outputs=None) -> dict: |
180 | 183 | try: |
181 | | - args = [ |
182 | | - ## Network expected to ensure all compulsory inputs exist, |
183 | | - # so no special handling for key errors here. |
184 | | - named_inputs[n] |
185 | | - for n in self.needs |
186 | | - if not isinstance(n, optional) and not isinstance(n, sideffect) |
187 | | - ] |
| 184 | + try: |
| 185 | + args = [ |
| 186 | + ## Network expected to ensure all compulsory inputs exist, |
| 187 | + # so no special handling for key errors here. |
| 188 | + # |
| 189 | + named_inputs[ |
| 190 | + n |
| 191 | + ] # Key-error here means `inputs` < compulsory `needs`. |
| 192 | + for n in self.needs |
| 193 | + if not isinstance(n, (optional, sideffect)) |
| 194 | + ] |
| 195 | + except KeyError: |
| 196 | + # FIXME: |
| 197 | + compulsory = iset( |
| 198 | + n for n in self.needs if not isinstance(n, (optional, sideffect)) |
| 199 | + ) |
| 200 | + raise ValueError( |
| 201 | + f"Missing compulsory needs{list(compulsory)}!\n inputs: {list(named_inputs)}\n {self}" |
| 202 | + ) |
188 | 203 |
|
189 | 204 | args.extend( |
190 | 205 | named_inputs[n] |
@@ -286,7 +301,13 @@ class operation: |
286 | 301 | """ |
287 | 302 |
|
288 | 303 | def __init__( |
289 | | - self, fn=None, *, name=None, needs=None, provides=None, returns_dict=None |
| 304 | + self, |
| 305 | + fn: Callable = None, |
| 306 | + *, |
| 307 | + name=None, |
| 308 | + needs=None, |
| 309 | + provides=None, |
| 310 | + returns_dict=None, |
290 | 311 | ): |
291 | 312 | self.fn = fn |
292 | 313 | self.name = name |
|
0 commit comments