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

Support optional inputs in lynxkite-graph-analytics.

Browse files
lynxkite-core/tests/test_simple.py CHANGED
@@ -19,7 +19,7 @@ async def test_optional_inputs():
19
  ws = workspace.Workspace(env="test", nodes=[], edges=[])
20
  a = ws.add_node(one)
21
  b = ws.add_node(maybe_add)
22
- outputs = await ws.execute()
23
  assert b.data.error == "Missing input: a"
24
  ws.add_edge(a, "output", b, "a")
25
  outputs = await ws.execute()
 
19
  ws = workspace.Workspace(env="test", nodes=[], edges=[])
20
  a = ws.add_node(one)
21
  b = ws.add_node(maybe_add)
22
+ await ws.execute()
23
  assert b.data.error == "Missing input: a"
24
  ws.add_edge(a, "output", b, "a")
25
  outputs = await ws.execute()
lynxkite-graph-analytics/src/lynxkite_graph_analytics/core.py CHANGED
@@ -187,6 +187,7 @@ async def execute(ws: workspace.Workspace):
187
  todo.remove(id)
188
  progress = True
189
  await _execute_node(node, ws, catalog, outputs)
 
190
 
191
 
192
  async def await_if_needed(obj):
@@ -213,10 +214,15 @@ async def _execute_node(
213
  # Convert inputs types to match operation signature.
214
  try:
215
  inputs = []
 
216
  for p in op.inputs:
217
  if p.name not in input_map:
218
- node.publish_error(f"Missing input: {p.name}")
219
- return
 
 
 
 
220
  x = input_map[p.name]
221
  if p.type == nx.Graph and isinstance(x, Bundle):
222
  x = x.to_nx()
@@ -230,6 +236,9 @@ async def _execute_node(
230
  traceback.print_exc()
231
  node.publish_error(e)
232
  return
 
 
 
233
  # Execute op.
234
  try:
235
  result = op(*inputs, **params)
 
187
  todo.remove(id)
188
  progress = True
189
  await _execute_node(node, ws, catalog, outputs)
190
+ return outputs
191
 
192
 
193
  async def await_if_needed(obj):
 
214
  # Convert inputs types to match operation signature.
215
  try:
216
  inputs = []
217
+ missing = []
218
  for p in op.inputs:
219
  if p.name not in input_map:
220
+ opt_type = ops.get_optional_type(p.type)
221
+ if opt_type is not None:
222
+ inputs.append(None)
223
+ else:
224
+ missing.append(p.name)
225
+ continue
226
  x = input_map[p.name]
227
  if p.type == nx.Graph and isinstance(x, Bundle):
228
  x = x.to_nx()
 
236
  traceback.print_exc()
237
  node.publish_error(e)
238
  return
239
+ if missing:
240
+ node.publish_error(f"Missing input: {', '.join(missing)}")
241
+ return
242
  # Execute op.
243
  try:
244
  result = op(*inputs, **params)
lynxkite-graph-analytics/tests/test_lynxkite_ops.py CHANGED
@@ -160,5 +160,28 @@ async def test_multiple_inputs():
160
  assert ws.nodes[-1].data.display is False
161
 
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  if __name__ == "__main__":
164
  pytest.main()
 
160
  assert ws.nodes[-1].data.display is False
161
 
162
 
163
+ async def test_optional_inputs():
164
+ @ops.op("test", "one")
165
+ def one():
166
+ return 1
167
+
168
+ @ops.op("test", "maybe add")
169
+ def maybe_add(a: int, b: int | None = None):
170
+ return a + (b or 0)
171
+
172
+ assert maybe_add.__op__.inputs == [
173
+ ops.Input(name="a", type=int, position="left"),
174
+ ops.Input(name="b", type=int | None, position="left"),
175
+ ]
176
+ ws = workspace.Workspace(env="test", nodes=[], edges=[])
177
+ a = ws.add_node(one)
178
+ b = ws.add_node(maybe_add)
179
+ await execute(ws)
180
+ assert b.data.error == "Missing input: a"
181
+ ws.add_edge(a, "output", b, "a")
182
+ outputs = await execute(ws)
183
+ assert outputs[b.id, "output"] == 1
184
+
185
+
186
  if __name__ == "__main__":
187
  pytest.main()