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

Fix tuple handling in condition branches #279

Merged
merged 7 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
7 changes: 7 additions & 0 deletions flytekit/annotated/promise.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,13 @@ def with_overrides(self, *args, **kwargs):
val.with_overrides(*args, **kwargs)
return self

@property
def ref(self):
for p in promises:
if p.ref:
return p.ref
return None

return Output(*promises)


Expand Down
55 changes: 55 additions & 0 deletions tests/flytekit/unit/annotated/test_conditions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import typing
import pytest

from flytekit import task, workflow
Expand Down Expand Up @@ -47,3 +48,57 @@ def multiplier_2(my_input: float) -> float:

with pytest.raises(ValueError):
multiplier_2(my_input=10)


def test_condition_sub_workflows():
@task()
def sum_div_sub(a: int, b: int) -> typing.NamedTuple("Outputs", sum=int, div=int, sub=int):
return a + b, a / b, a - b

@task()
def sum_sub(a: int, b: int) -> typing.NamedTuple("Outputs", sum=int, sub=int):
return a + b, a - b

@workflow
def sub_wf(a: int, b: int) -> (int, int):
return sum_sub(a=a, b=b)

@workflow
def math_ops(a: int, b: int) -> (int, int):
# Flyte will only make `sum` and `sub` available as outputs because they are common between all branches
sum, sub = (
conditional("noDivByZero")
.if_(a > b)
.then(sub_wf(a=a, b=b))
.else_()
.fail("Only positive results are allowed")
)

return sum, sub

x, y = math_ops(a=3, b=2)
assert x == 5
assert y == 1


def test_condition_tuple_branches():
@task()
def sum_sub(a: int, b: int) -> typing.NamedTuple("Outputs", sum=int, sub=int):
return a + b, a - b

@workflow
def math_ops(a: int, b: int) -> (int, int):
# Flyte will only make `sum` and `sub` available as outputs because they are common between all branches
sum, sub = (
conditional("noDivByZero")
.if_(a > b)
.then(sum_sub(a=a, b=b))
.else_()
.fail("Only positive results are allowed")
)

return sum, sub

x, y = math_ops(a=3, b=2)
assert x == 5
assert y == 1