Skip to content

Commit

Permalink
Improves the error message when and/or/not operators are used with Pa…
Browse files Browse the repository at this point in the history
…rgraph functions.
  • Loading branch information
rafa-be committed Feb 28, 2025
1 parent 2372078 commit d7f869c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pargraph/graph/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ def call(*args) -> Any:

return call(self, len(args), *args, *itertools.chain(*kwargs.items()))

def __bool__(self) -> bool:
# As Python does not allow the overloading of its and/or/not operators, we cannot implement
# our graph deduction mechanism on these.
# This implementation of __bool__ provides a more insightful error message when these
# operators are used on Pargraph functions.
# More info: https://peps.python.org/pep-0335/
raise TypeError("Pargraph does not support the use of boolean (and/or/not) operators.")


def external_input() -> GraphContext:
"""
Expand Down Expand Up @@ -345,8 +353,10 @@ def _generate_graph(func: Callable, /, *args, **kwargs):
# __hash__() is not included because since Python 3.3 hash randomization is enabled by default yielding impure hashes
# between isolated instances making it not suitable for out-of-core computations
# More info: https://docs.python.org/3/reference/datamodel.html#object.__hash__
#
# __bool__() has a specific implementation, as Python does not support the overloading of its and/or operators (see
# GraphContext.__bool__ here-above).
for op in {
bool,
int,
float,
complex,
Expand Down
15 changes: 15 additions & 0 deletions tests/test_graph_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ def sample_graph(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame:
),
)

def test_boolean(self):
@delayed
def is_even(value: int) -> bool:
return value % 2 == 0

@delayed
def is_positive(value: int) -> bool:
return value >= 0

@graph
def invalid_graph(value: int) -> bool:
return is_even(value) or is_positive(value)

self.assertRaises(TypeError, invalid_graph.to_graph)

def test_explode_subgraphs(self):
@graph
def sample_subgraph(x: int, y: int) -> int:
Expand Down

0 comments on commit d7f869c

Please sign in to comment.