Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PortNamespace: Make dynamic apply recursively #263

Merged
merged 1 commit into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/plumpy/ports.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,20 @@ def get_port(self, name: str) -> Union[Port, 'PortNamespace']:
namespace = name.split(self.NAMESPACE_SEPARATOR)
port_name = namespace.pop(0)

if port_name not in self:
if port_name not in self and not self.dynamic:
raise ValueError(f"port '{port_name}' does not exist in port namespace '{self.name}'")

if port_name not in self and self.dynamic:
self[port_name] = self.__class__(
name=port_name,
required=self.required,
validator=self.validator,
valid_type=self.valid_type,
default=self.default,
dynamic=self.dynamic,
populate_defaults=self.populate_defaults
)

if namespace:
portnamespace = cast(PortNamespace, self[port_name])
return portnamespace.get_port(self.NAMESPACE_SEPARATOR.join(namespace))
Expand Down
21 changes: 21 additions & 0 deletions test/test_port.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,27 @@ def test_port_namespace_get_port(self):
port = self.port_namespace.get_port('sub.name.space.' + self.BASE_PORT_NAME)
self.assertEqual(port, self.port)

def test_port_namespace_get_port_dynamic(self):
"""Test that ``get_port`` does not raise if a port does not exist as long as the namespace is dynamic.

In this case, the method should create the subnamespace on-the-fly with the same stats as the host namespace.
"""
port_namespace = PortNamespace(self.BASE_PORT_NAMESPACE_NAME, dynamic=True)

name = 'undefined'
sub_namespace = port_namespace.get_port(name)

assert isinstance(sub_namespace, PortNamespace)
assert sub_namespace.dynamic
assert sub_namespace.name == name

name = 'nested.undefined'
sub_namespace = port_namespace.get_port(name)

assert isinstance(sub_namespace, PortNamespace)
assert sub_namespace.dynamic
assert sub_namespace.name == 'undefined'

def test_port_namespace_create_port_namespace(self):
"""
Test the create_port_namespace function of the PortNamespace class
Expand Down