Skip to content

Conversation

BarryWu0812
Copy link
Contributor

@BarryWu0812 BarryWu0812 commented Jul 25, 2025

Tracking issue

Closes flyteorg/flyte#6503

Why are the changes needed?

Support to serialize Literal to a literal primitive

What changes were proposed in this pull request?

Add a Literal transformer

How was this patch tested?

Unit test
and
Run the below workflow

from flytekit import task, workflow, ImageSpec
import flytekit
from typing import Literal

@task
def my_task(feature: Literal["outcome", "income"]) -> str:
    return f"feature: {feature}"

@workflow
def wf(feature: Literal["outcome", "income"]) -> str:
    return my_task(feature=feature)

Screenshots?

image

Check all the applicable boxes

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

Summary by Bito

This pull request introduces a new Literal transformer in the Flyte framework, enhancing the type engine's ability to serialize Literal types into primitive types. It improves data type handling and usability in type management, and includes methods for converting between Literal and Python values. Unit tests have been added to verify the new functionality.

Copy link

welcome bot commented Jul 25, 2025

Thank you for opening this pull request! 🙌

These tips will help get your PR across the finish line:

  • Most of the repos have a PR template; if not, fill it out to the best of your knowledge.
  • Sign off your commits (Reference: DCO Guide).

@BarryWu0812 BarryWu0812 marked this pull request as draft July 25, 2025 17:05
@BarryWu0812 BarryWu0812 reopened this Jul 25, 2025
@BarryWu0812 BarryWu0812 marked this pull request as ready for review August 5, 2025 02:21
@kumare3
Copy link
Contributor

kumare3 commented Aug 5, 2025

What is the goal of a literal type? Is it to provide an enum like behavior or just a weird way of passing primitives

@BarryWu0812
Copy link
Contributor Author

#3304 (comment)

Currently, the Literal will be serialized to a pickle file instead of proto, and it would be nice to have the typing.Literal parameter also render as string in the UI.

Before, the input in UI is shown as like feature: "single (PythonPickle) blob" if the input type is Literal.
After, the input in UI will be shown as like feature: "income" if the input type is Literal.

I think the goal is to support the Literal can be serialized to primitive

@machichima
Copy link
Member

machichima commented Aug 6, 2025

What is the goal of a literal type? Is it to provide an enum like behavior or just a weird way of passing primitives

Based on my understanding, Literal type in Python is to set the acceptable value for a variable. Probably a bit like Union but also restrict the value

For example, if def func(a: Literal("a", 1), then we can only call func with func("a") or func(1)

@machichima
Copy link
Member

Hi @BarryWu0812 ,

Could you please fix the CI error?

Thanks!

Copy link

codecov bot commented Aug 6, 2025

Codecov Report

❌ Patch coverage is 22.44898% with 38 lines in your changes missing coverage. Please review.
✅ Project coverage is 47.62%. Comparing base (9effe91) to head (bcd136f).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
flytekit/core/type_engine.py 22.44% 37 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3304      +/-   ##
==========================================
+ Coverage   44.53%   47.62%   +3.09%     
==========================================
  Files         295      215      -80     
  Lines       26792    22634    -4158     
  Branches     2955     2965      +10     
==========================================
- Hits        11931    10779    -1152     
+ Misses      14764    11278    -3486     
- Partials       97      577     +480     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@BarryWu0812
Copy link
Contributor Author

Hi @BarryWu0812 ,

Could you please fix the CI error?

Thanks!

Hi @machichima , I fixed it, Thanks!

@BarryWu0812 BarryWu0812 requested a review from machichima August 22, 2025 17:51
@machichima
Copy link
Member

machichima commented Aug 25, 2025

@BarryWu0812

Could you please run an example using Literal as the task argument, and provide a screenshot to ensure the "input" tab in the UI shows correctly instead of single (PythonPickle) blob? As mentioned in issue: #6410

Thanks

@BarryWu0812
Copy link
Contributor Author

BarryWu0812 commented Aug 26, 2025

@BarryWu0812

Could you please run an example using Literal as the task argument, and provide a screenshot to ensure the "input" tab in the UI shows correctly instead of single (PythonPickle) blob? As mentioned in issue: #6410

Thanks

Hi @machichima , I have uploaded the result. Thanks

Copy link
Member

@pingsutw pingsutw left a comment

Choose a reason for hiding this comment

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

Thanks for working on it. Could you also add some unit tests

@BarryWu0812 BarryWu0812 requested a review from pingsutw August 27, 2025 15:29
return base_transformer.to_python_value(ctx, lv, base_type)

def guess_python_type(self, literal_type: LiteralType) -> Type:
ann = getattr(literal_type, "annotation", None)
Copy link
Member

Choose a reason for hiding this comment

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

literal_type will always have an annotation attribute, right? I think we can just do if literal_type.annotation and literal_type.annotation.annotations

@pingsutw pingsutw merged commit 23de1ab into flyteorg:master Sep 27, 2025
228 of 230 checks passed
Atharva1723 pushed a commit to Atharva1723/flytekit that referenced this pull request Oct 5, 2025
Signed-off-by: Barry Wu <[email protected]>
Co-authored-by: Barry Wu <[email protected]>
Signed-off-by: Atharva <[email protected]>
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.

[Core feature] Add support Literal Transformer

4 participants