darabos commited on
Commit
0e6dfc5
·
1 Parent(s): d29f839

Support optional inputs in one-by-one executor.

Browse files
lynxkite-core/src/lynxkite/core/executors/one_by_one.py CHANGED
@@ -131,12 +131,22 @@ async def execute(ws: workspace.Workspace, catalog: ops.Catalog, cache=None):
131
  for task in ts:
132
  try:
133
  inputs = []
 
134
  for i in op.inputs:
135
  if i.position.is_vertical():
136
- assert (n, i.name) in batch_inputs, f"{i.name} is missing"
137
- inputs.append(batch_inputs[(n, i.name)])
 
 
 
 
 
 
138
  else:
139
  inputs.append(task)
 
 
 
140
  if cache is not None:
141
  key = make_cache_key((inputs, params))
142
  if key not in cache:
 
131
  for task in ts:
132
  try:
133
  inputs = []
134
+ missing = []
135
  for i in op.inputs:
136
  if i.position.is_vertical():
137
+ if (n, i.name) in batch_inputs:
138
+ inputs.append(batch_inputs[(n, i.name)])
139
+ else:
140
+ opt_type = ops.get_optional_type(i.type)
141
+ if opt_type is not None:
142
+ inputs.append(None)
143
+ else:
144
+ missing.append(i.name)
145
  else:
146
  inputs.append(task)
147
+ if missing:
148
+ node.publish_error(f"Missing input: {', '.join(missing)}")
149
+ break
150
  if cache is not None:
151
  key = make_cache_key((inputs, params))
152
  if key not in cache:
lynxkite-core/tests/test_one_by_one.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from lynxkite.core import ops, workspace
2
+ from lynxkite.core.executors import one_by_one
3
+
4
+
5
+ async def test_optional_inputs():
6
+ @ops.op("test", "one")
7
+ def one():
8
+ return 1
9
+
10
+ @ops.input_position(a="bottom", b="bottom")
11
+ @ops.op("test", "maybe add")
12
+ def maybe_add(a: list[int], b: list[int] | None = None):
13
+ return [a + b for a, b in zip(a, b)] if b else a
14
+
15
+ assert maybe_add.__op__.inputs == [
16
+ ops.Input(name="a", type=list[int], position="bottom"),
17
+ ops.Input(name="b", type=list[int] | None, position="bottom"),
18
+ ]
19
+ one_by_one.register("test")
20
+ ws = workspace.Workspace(env="test", nodes=[], edges=[])
21
+ a = ws.add_node(one)
22
+ b = ws.add_node(maybe_add)
23
+ outputs = await ws.execute()
24
+ assert b.data.error == "Missing input: a"
25
+ ws.add_edge(a, "output", b, "a")
26
+ outputs = await ws.execute()
27
+ assert outputs[b.id].last_result == [1]