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

Improve Union Transformer Ambiguous Error Message #3076

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion flytekit/core/type_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,7 @@ async def async_to_literal(
) -> typing.Union[Literal, asyncio.Future]:
python_type = get_underlying_type(python_type)

potential_types = []
found_res = False
is_ambiguous = False
res = None
Expand All @@ -1906,14 +1907,15 @@ async def async_to_literal(
is_ambiguous = True
res_type = _add_tag_to_type(trans.get_literal_type(t), trans.name)
found_res = True
potential_types.append(t)
except Exception as e:
logger.warning(
f"UnionTransformer failed attempt to convert from {python_val} to {t} error: {e}",
)
continue

if is_ambiguous:
raise TypeError("Ambiguous choice of variant for union type")
raise TypeError(f"Ambiguous choice of variant for union type.\n" f"Potential types: {potential_types}\n")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add more to this error message stating why they are ambiguous? Something like:

These types are structurally the same, because it's attributes have the same names and associated types.

Otherwise, it's hard to tell from the error what is wrong.


if found_res:
return Literal(scalar=Scalar(union=Union(value=res, stored_type=res_type)))
Expand Down
20 changes: 20 additions & 0 deletions tests/flytekit/unit/core/test_unions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import pytest
import mock
from flytekit import task

from flytekit.core.context_manager import FlyteContextManager
from flytekit.core.type_engine import TypeEngine, TypeTransformerFailedError
Expand Down Expand Up @@ -140,3 +141,22 @@ class TestDataclass(DataClassJsonMixin):
assert o.b.remote_path == ot.b.remote_source
assert o.c["a"] == ot.c["a"]
assert o.d == FlyteFile(remote_path)

def test_ambiguous_union_transformer_to_literal():
@dataclass
class A:
a: int

@dataclass
class B:
a: int

@task
def t1() -> typing.Union[A, B]:
return A(a=1)

with pytest.raises(
TypeError,
match=("Potential types:")
):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider more specific error message assertion

The error message assertion match=("Potential types:") seems too generic. Consider making it more specific to match the actual error message that would be raised when there are ambiguous union types.

Code suggestion
Check the AI-generated fix before applying
Suggested change
match=("Potential types:")
):
match="Cannot determine which type to choose from Union[A, B] for value of type A. Potential types:"
):

Code Review Run #da8f08


Is this a valid issue, or was it incorrectly flagged by the Agent?

  • it was incorrectly flagged

t1()
Loading