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

Conversation

Future-Outlier
Copy link
Member

@Future-Outlier Future-Outlier commented Jan 22, 2025

Tracking issue

image

https://flyte-org.slack.com/archives/CP2HDHKE1/p1737474492222339

Why are the changes needed?

Users can debug more effectively if having potential types.

What changes were proposed in this pull request?

  1. add potential types variables when doing type transform
  2. print potential types if there's an ambiguous case.

How was this patch tested?

unit test and local execution.

from flytekit import task
from dataclasses import dataclass
from typing import Union

@dataclass
class A:
    a: int

@dataclass
class B:
    a: int

@task
def t1() -> Union[A, B]:
    return A(3)

if __name__ == "__main__":
    print(t1())

Setup process

Screenshots

image

Check all the applicable boxes

  • I updated the documentation accordingly.
  • All new and existing tests passed.
  • All commits are signed-off.

Related PRs

Summary by Bito

Enhanced error messaging system for ambiguous union type transformations, providing more detailed type information. The update includes improved error messages that now display both the ambiguous choice notification and a comprehensive list of potential matching types. These changes aim to improve developer debugging experience.

Unit tests added: True

Estimated effort to review (1-5, lower is better): 1

@flyte-bot
Copy link
Contributor

flyte-bot commented Jan 22, 2025

Code Review Agent Run #da8f08

Actionable Suggestions - 1
  • tests/flytekit/unit/core/test_unions.py - 1
    • Consider more specific error message assertion · Line 160-161
Review Details
  • Files reviewed - 2 · Commit Range: 8052d77..8052d77
    • flytekit/core/type_engine.py
    • tests/flytekit/unit/core/test_unions.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

AI Code Review powered by Bito Logo

@flyte-bot
Copy link
Contributor

flyte-bot commented Jan 22, 2025

Changelist by Bito

This pull request implements the following key changes.

Key Change Files Impacted
Feature Improvement - Enhanced Union Type Error Handling

type_engine.py - Improved error message for ambiguous union type transformations by adding potential types information

test_unions.py - Added test cases to verify enhanced error messages for ambiguous union transformations

Comment on lines 160 to 161
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

Copy link

codecov bot commented Jan 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.22%. Comparing base (a86048a) to head (8052d77).

❗ There is a different number of reports uploaded between BASE (a86048a) and HEAD (8052d77). Click for more details.

HEAD has 7 uploads less than BASE
Flag BASE (a86048a) HEAD (8052d77)
8 1
Additional details and impacted files
@@             Coverage Diff             @@
##           master    #3076       +/-   ##
===========================================
- Coverage   93.28%   73.22%   -20.07%     
===========================================
  Files          36      202      +166     
  Lines        1728    21432    +19704     
  Branches        0     2760     +2760     
===========================================
+ Hits         1612    15693    +14081     
- Misses        116     4956     +4840     
- Partials        0      783      +783     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: Future-Outlier <[email protected]>
@flyte-bot
Copy link
Contributor

flyte-bot commented Jan 22, 2025

Code Review Agent Run #5172b2

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: 8052d77..b9298cd
    • tests/flytekit/unit/core/test_unions.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

AI Code Review powered by Bito Logo

@Future-Outlier Future-Outlier enabled auto-merge (squash) January 24, 2025 06:41
Copy link
Member

@thomasjpfan thomasjpfan left a comment

Choose a reason for hiding this comment

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

For your example:

@dataclass
class A:
    a: int

@dataclass
class B:
    a: int

what should we recommend the user to make it work?

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants