Selecting a namespace together with its child namespace aborts the run with a duplicate node names error
Description
Selecting two namespaces where one is the parent of the other, for example kedro run --namespaces a,a.b, aborts the run with ValueError: Pipeline nodes must have unique names. Each namespace is selected fine on its own.
Pipeline.only_nodes_with_namespaces collects the matching nodes in a loop, one namespace at a time, and extends the same list without deduplicating:
for node_namespace in node_namespaces:
matching_nodes = []
for n in self._nodes:
if n.namespace and (
n.namespace == node_namespace
or n.namespace.startswith(f"{node_namespace}.")
):
matching_nodes.append(n)
...
nodes.extend(matching_nodes)The startswith branch is what makes a select the nodes of a.b too, which is the documented behaviour for nested namespaces. So when both a and a.b are requested, every node under a.b is appended twice and the resulting Pipeline([...]) rejects the duplicates.
Context
I hit this while filtering a run down to a parent namespace plus one of its children, which is a natural thing to type when you are not sure whether the parent already covers the child.
The error message makes it worse: it says the node names appear more than once and suggests naming the nodes with the last argument of node(), but the nodes already have unique names in the pipeline. There is nothing the user can change in their project to fix it. The only way out is to figure out that the two namespaces overlap and drop one.
Steps to Reproduce
- Create a pipeline with a node in namespace
aand a node in namespacea.b:
from kedro.pipeline import Pipeline, node
def make_value() -> int:
return 1
def double(x: int) -> int:
return x * 2
def create_pipeline(**kwargs) -> Pipeline:
return Pipeline([
node(make_value, None, "a.out", name="first", namespace="a"),
node(double, "a.out", "a.b.out", name="second", namespace="a.b"),
])kedro run --namespaces aruns both nodes.kedro run --namespaces a.bruns one node.kedro run --namespaces a,a.bfails.
The same is visible without a project:
p.only_nodes_with_namespaces(["a"]) # ['a.first', 'a.b.second']
p.only_nodes_with_namespaces(["a.b"]) # ['a.b.second']
p.only_nodes_with_namespaces(["a", "a.b"]) # ValueErrorExpected Result
--namespaces a,a.b selects the union of both namespaces, which in this case is the same set of nodes as --namespaces a.
Actual Result
ValueError: Pipeline nodes must have unique names. The following node names appear more than once:
Free nodes:
- a.b.second
You can name your nodes using the last argument of 'node()'.Your Environment
- Kedro version used: 1.6.0 (installed from source at
8d9abdf) - Python version used: 3.12.13
- Operating system and version: macOS 15
Possible fix
Deduplicate while preserving order before building the pipeline, keeping the unmatched namespace check as it is today. I have a fix and a regression test ready and I am happy to open a PR if you agree with the approach.
Source: kedro-org/kedro