Skip to content

Commit

Permalink
fix: 🐛 typing with py310 Union |
Browse files Browse the repository at this point in the history
  • Loading branch information
bashirmindee committed Nov 12, 2024
1 parent a0a8184 commit b2cfc37
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tawazi/node/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def _validate_tuple(func: Callable[..., Any], unpack_to: int) -> Optional[bool]:
if isinstance(r_type, str):
# eval is safe to use because the user is providing the typing and evaluating it locally
r_type = eval(r_type, func.__globals__) # noqa: PGH001,S307 # nosec B307
if not hasattr(r_type, "__origin__"):
return None
is_tuple = r_type.__origin__ is tuple
if not is_tuple:
return None
Expand Down
33 changes: 33 additions & 0 deletions tests/test_multiple_return_value_for_exec_node.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from typing import Dict, List, Tuple

import pytest
Expand Down Expand Up @@ -219,3 +220,35 @@ def pipe() -> Tuple[int, int, int, List[int]]:
return r1, r2, r3, list_v

assert pipe() == (1, 2, 3, [1, 2, 3])


# if python version is 3.10 or higher, include this function
if sys.version_info >= (3, 10):
from typing import Union

from tawazi import xn

union_type = Union[tuple[float, int], tuple[float, float]]
py311_union_type = tuple[float, int] | tuple[float, float]

@xn(unpack_to=2)
def union_py39(x: float, integer: bool = False) -> union_type:
return x, x / 2 if not integer else int(x / 2)

@dag
def dag_union_py39(x):
return union_py39(x)

def test_unpacking_union() -> None:
assert 1, 1 / 2 == dag_union_py39(1)

@xn(unpack_to=2)
def union_py310(x: float, integer: bool = False) -> py311_union_type:
return x, x / 2 if not integer else int(x / 2)

@dag
def dag_union_py310(x):
return union_py310(x)

def test_unpacking_union_py310() -> None:
assert 2, 1 == dag_union_py310(2)

0 comments on commit b2cfc37

Please sign in to comment.