Skip to content

Commit 69e2772

Browse files
committed
🐛 aiida: suffix repeated input names
Engine-written files reuse one input name per stage with different defaults; keep the first, suffix the rest.
1 parent 4c55e5c commit 69e2772

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

‎src/python_workflow_definition/aiida.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,19 @@ def _emit_level(
261261
"""Emit wrapper tasks, nested graphs, and one ``@graph`` definition."""
262262
inputs: dict[int, str] = {}
263263
params: list[str] = []
264+
taken: set[str] = set()
264265
for node_id in sorted(nodes):
265266
node: dict[str, t.Any] = nodes[node_id]
266267
if node["type"] == "input":
267268
param: str = _identifier(node["name"], what="Input")
269+
# Distinct nodes may share a name with different defaults.
270+
# Keep the first name, suffix later ones deterministically.
271+
base: str = param
272+
counter: int = 2
273+
while param in taken:
274+
param = base + "_" + str(counter)
275+
counter += 1
276+
taken.add(param)
268277
inputs[node_id] = param
269278
params.append(param + "=" + repr(node["value"]))
270279
_emit_wrappers(lines, nodes, edges, prefix=prefix)

‎tests/test_aiida.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,53 @@ def test_workflow_node_rejected(self):
7575
with self.assertRaisesRegex(ValidationError, "workflow"):
7676
load_workflow_json(nested_file, module_name="pwd_test_nested")
7777

78+
def test_duplicate_input_names_suffixed(self):
79+
duplicate = {
80+
"version": "0.1.0",
81+
"nodes": [
82+
{"id": 0, "type": "input", "name": "x", "value": 1},
83+
{"id": 1, "type": "input", "name": "x", "value": 2},
84+
{
85+
"id": 2,
86+
"type": "function",
87+
"value": "python_workflow_definition.shared.get_dict",
88+
},
89+
{"id": 3, "type": "output", "name": "result"},
90+
],
91+
"edges": [
92+
{
93+
"target": 2,
94+
"targetPort": "a",
95+
"source": 0,
96+
"sourcePort": None,
97+
},
98+
{
99+
"target": 2,
100+
"targetPort": "b",
101+
"source": 1,
102+
"sourcePort": None,
103+
},
104+
{
105+
"target": 3,
106+
"targetPort": None,
107+
"source": 2,
108+
"sourcePort": None,
109+
},
110+
],
111+
}
112+
with tempfile.TemporaryDirectory() as tmp:
113+
duplicate_file = Path(tmp) / "duplicate.json"
114+
duplicate_file.write_text(json.dumps(duplicate))
115+
tasks = (
116+
load_workflow_json(duplicate_file, module_name="pwd_test_dup")
117+
.build()
118+
.to_dict()["tasks"]
119+
)
120+
self.assertEqual(
121+
sorted(task["name"] for task in tasks),
122+
["pwd_task_2"],
123+
)
124+
78125

79126
if __name__ == "__main__":
80127
unittest.main()

0 commit comments

Comments
 (0)