Skip to content

Commit 02adced

Browse files
committed
ENH(OP): explain missing-needs error when calling op
1 parent 3fc11e5 commit 02adced

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

graphtik/op.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import abc
66
import logging
77
from collections import abc as cabc
8+
from typing import Callable
89

910
from boltons.setutils import IndexedSet as iset
1011

@@ -110,10 +111,12 @@ class FunctionalOperation(Operation):
110111
"""
111112
An Operation performing a callable (ie function, method, lambda).
112113
113-
Use :class:`operation()` to build instances of this class instead.
114+
Use :class:`operation()` factory to build instances of this class instead.
114115
"""
115116

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+
):
117120
self.fn = fn
118121
self.returns_dict = returns_dict
119122
## 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:
178181

179182
def compute(self, named_inputs, outputs=None) -> dict:
180183
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+
)
188203

189204
args.extend(
190205
named_inputs[n]
@@ -286,7 +301,13 @@ class operation:
286301
"""
287302

288303
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,
290311
):
291312
self.fn = fn
292313
self.name = name

test/test_op.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,5 @@ def sumall(a, *args, b=0, **kwargs):
139139
assert op.compute(dict(a=1, arg1=2, arg2=3, args=[4, 5], b=6))["sum"] == exp - 7
140140
assert op.compute(dict(a=1, arg2=3, args=[4, 5], b=6, c=7))["sum"] == exp - 2
141141
assert op.compute(dict(a=1, arg1=2, arg2=3, b=6, c=7))["sum"] == exp - 4 - 5
142-
with pytest.raises(KeyError, match="'a'"):
142+
with pytest.raises(ValueError, match="Missing compulsory needs.+'a'"):
143143
assert op.compute(dict(arg1=2, arg2=3, b=6, c=7))

0 commit comments

Comments
 (0)