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

Merged
merged 3 commits into from
Feb 4, 2025

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 in Flytekit for ambiguous union type transformations, providing more detailed type information and context. The update includes improved TypeError messages in type_engine.py with comprehensive lists of potential matching types and structurally similar types. These changes aim to improve developer debugging experience.

Unit tests added: True

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

Signed-off-by: Future-Outlier <eric901201@gmail.com>
@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 Messaging

type_engine.py - Added potential types tracking and improved error message for ambiguous union types

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

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

Attention: Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.

Project coverage is 51.96%. Comparing base (a86048a) to head (3fe9801).
Report is 18 commits behind head on master.

Files with missing lines Patch % Lines
flytekit/core/type_engine.py 0.00% 3 Missing ⚠️

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

HEAD has 18 uploads less than BASE
Flag BASE (a86048a) HEAD (3fe9801)
20 2
Additional details and impacted files
@@             Coverage Diff             @@
##           master    #3076       +/-   ##
===========================================
- Coverage   77.44%   51.96%   -25.49%     
===========================================
  Files         238      202       -36     
  Lines       23158    21474     -1684     
  Branches     2760     2768        +8     
===========================================
- Hits        17935    11158     -6777     
- Misses       4412     9696     +5284     
+ Partials      811      620      -191     

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

Signed-off-by: Future-Outlier <eric901201@gmail.com>
@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.

Copy link
Member Author

Choose a reason for hiding this comment

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

love u thomas, it's upadted

Signed-off-by: Future-Outlier <eric901201@gmail.com>
@Future-Outlier Future-Outlier merged commit 6bdf411 into master Feb 4, 2025
107 of 112 checks passed
@flyte-bot
Copy link
Contributor

flyte-bot commented Feb 4, 2025

Code Review Agent Run #cc92f6

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: b9298cd..3fe9801
    • 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

UmerAhmad pushed a commit to UmerAhmad/flytekit that referenced this pull request Feb 8, 2025
* Improve Union Transformer Ambiguous Error Message

Signed-off-by: Future-Outlier <eric901201@gmail.com>

* better error msg assertion

Signed-off-by: Future-Outlier <eric901201@gmail.com>

---------

Signed-off-by: Future-Outlier <eric901201@gmail.com>
Signed-off-by: Umer Ahmad <umer2ahmad@gmail.com>
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.

4 participants