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

Fixes guess type bug in UnionTransformer #1426

Merged
merged 2 commits into from
Jan 31, 2023
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
2 changes: 1 addition & 1 deletion flytekit/core/type_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ def to_python_value(self, ctx: FlyteContext, lv: Literal, expected_python_type:

def guess_python_type(self, literal_type: LiteralType) -> type:
if literal_type.union_type is not None:
return typing.Union[tuple(TypeEngine.guess_python_type(v.type) for v in literal_type.union_type.variants)] # type: ignore
return typing.Union[tuple(TypeEngine.guess_python_type(v) for v in literal_type.union_type.variants)]

raise ValueError(f"Union transformer cannot reverse {literal_type}")

Expand Down
14 changes: 13 additions & 1 deletion tests/flytekit/unit/core/test_type_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
from flytekit.models.annotation import TypeAnnotation
from flytekit.models.core.types import BlobType
from flytekit.models.literals import Blob, BlobMetadata, Literal, LiteralCollection, LiteralMap, Primitive, Scalar, Void
from flytekit.models.types import LiteralType, SimpleType, TypeStructure
from flytekit.models.types import LiteralType, SimpleType, TypeStructure, UnionType
from flytekit.types.directory import TensorboardLogs
from flytekit.types.directory.types import FlyteDirectory
from flytekit.types.file import FileExt, JPEGImageFile
Expand Down Expand Up @@ -941,6 +941,18 @@ def test_union_transformer():
assert UnionTransformer.get_sub_type_in_optional(typing.Optional[int]) == int


def test_union_guess_type():
ut = UnionTransformer()
t = ut.guess_python_type(
LiteralType(
union_type=UnionType(
variants=[LiteralType(simple=SimpleType.STRING), LiteralType(simple=SimpleType.INTEGER)]
)
)
)
assert t == typing.Union[str, int]


def test_union_type_with_annotated():
pt = typing.Union[
Annotated[str, FlyteAnnotation({"hello": "world"})], Annotated[int, FlyteAnnotation({"test": 123})]
Expand Down