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

raise error on append and extend connections #128

Merged
merged 6 commits into from
Dec 16, 2024
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
60 changes: 60 additions & 0 deletions tests/test_add_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,63 @@ def test_combine_error():
znflow.combine([1, 2, 3], attribute="outs", only_getattr_on_nodes=False)

assert znflow.combine([1, 2, 3], attribute="outs") == [1, 2, 3]


def test_append_connection():
with znflow.DiGraph():
a = CreateList(size=5)
b = CreateList(size=4)

assert isinstance(a.outs, znflow.Connection)

with pytest.raises(TypeError):
a.outs.append(b.outs)


def test_extend_connection():
with znflow.DiGraph():
a = CreateList(size=5)
b = CreateList(size=4)

with pytest.raises(TypeError):
a.outs.extend(b.outs)


def test_append_function_future():
with znflow.DiGraph():
a = create_list(5)
b = create_list(5)

assert isinstance(a, znflow.FunctionFuture)

with pytest.raises(TypeError):
a.append(b)


def test_extend_function_future():
with znflow.DiGraph():
a = create_list(5)
b = create_list(5)

with pytest.raises(TypeError):
a.extend(b)


def test_append_combined_connection():
with znflow.DiGraph():
a = create_list(5)
b = create_list(5)

assert isinstance(a + b, znflow.CombinedConnections)

with pytest.raises(TypeError):
a.append(a + b)


def test_extend_combined_connection():
with znflow.DiGraph():
a = create_list(5)
b = create_list(5)

with pytest.raises(TypeError):
a.extend(a + b)
58 changes: 58 additions & 0 deletions znflow/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,24 @@ def __iter__(self):
except AttributeError:
raise TypeError(f"'{self}' object is not iterable")

def extend(self, *args) -> None:
"""
Raises
------
TypeError
If the method is called.
"""
raise TypeError("Connections can not be extended. Use 'self += other' instead.")

def append(self, *args) -> None:
"""
Raises
------
TypeError
If the method is called.
"""
raise TypeError("Connections can not be appended.")


@dataclasses.dataclass(frozen=True)
class CombinedConnections:
Expand Down Expand Up @@ -350,6 +368,26 @@ def result(self):
f" only supported type is list. Please change {connection}"
) from err

def extend(self, *args) -> None:
"""
Raises
------
TypeError
If the method is called.
"""
raise TypeError(
"CombinedConnections can not be extended. Use 'self += other' instead."
)

def append(self, *args) -> None:
"""
Raises
------
TypeError
If the method is called.
"""
raise TypeError("CombinedConnections can not be appended.")


@dataclasses.dataclass
class FunctionFuture(NodeBaseMixin):
Expand Down Expand Up @@ -436,3 +474,23 @@ def __iter__(self):
return resolve(self).__iter__()
except AttributeError:
raise TypeError(f"'{self}' object is not iterable")

def extend(self, *args) -> None:
"""
Raises
------
TypeError
If the method is called.
"""
raise TypeError(
"FunctionFuture can not be extended. Use 'self += other' instead."
)

def append(self, *args) -> None:
"""
Raises
------
TypeError
If the method is called.
"""
raise TypeError("FunctionFuture can not be appended.")
Loading