Skip to content

Commit

Permalink
raise error on append and extend connections (#128)
Browse files Browse the repository at this point in the history
* add __len__

* raise error on append/extend

* update functionfuture as well

* test connections

* assert type
  • Loading branch information
PythonFZ authored Dec 16, 2024
1 parent 899bf94 commit 9e5e58e
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
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.")

0 comments on commit 9e5e58e

Please sign in to comment.