Skip to content

[Core feature] Add support Literal Transformer - #3304

Merged
pingsutw merged 11 commits into
flyteorg:masterfrom
BarryWu0812:#6503
Sep 27, 2025
Merged

[Core feature] Add support Literal Transformer#3304
pingsutw merged 11 commits into
flyteorg:masterfrom
BarryWu0812:#6503

Conversation

@BarryWu0812

@BarryWu0812 BarryWu0812 commented Jul 25, 2025

Copy link
Copy Markdown
Contributor

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.

Signed-off-by: Barry Wu <a0987818905@gmail.com>
@welcome

welcome Bot commented Jul 25, 2025

Copy link
Copy Markdown

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
Barry Wu and others added 2 commits August 4, 2025 19:10
@BarryWu0812
BarryWu0812 marked this pull request as ready for review August 5, 2025 02:21
@kumare3

kumare3 commented Aug 5, 2025

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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

Signed-off-by: Barry Wu <a0987818905@gmail.com>
@machichima

machichima commented Aug 6, 2025

Copy link
Copy Markdown
Member

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
Copy Markdown
Member

Hi @BarryWu0812 ,

Could you please fix the CI error?

Thanks!

@codecov

codecov Bot commented Aug 6, 2025

Copy link
Copy Markdown

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.

Signed-off-by: Barry Wu <a0987818905@gmail.com>
@BarryWu0812

Copy link
Copy Markdown
Contributor Author

Hi @BarryWu0812 ,

Could you please fix the CI error?

Thanks!

Hi @machichima , I fixed it, Thanks!

Comment thread flytekit/core/type_engine.py Outdated
Comment thread flytekit/core/type_engine.py Outdated
Comment thread flytekit/core/type_engine.py Outdated
Comment thread flytekit/core/type_engine.py
Comment thread flytekit/core/type_engine.py Outdated
Signed-off-by: Barry Wu <a0987818905@gmail.com>
@BarryWu0812
BarryWu0812 requested a review from machichima August 22, 2025 17:51
@machichima

machichima commented Aug 25, 2025

Copy link
Copy Markdown
Member

@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

BarryWu0812 commented Aug 26, 2025

Copy link
Copy Markdown
Contributor Author

@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

@pingsutw pingsutw left a comment

Copy link
Copy Markdown
Member

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

Comment thread flytekit/core/type_engine.py Outdated
Comment thread flytekit/core/type_engine.py Outdated
…t tests

Signed-off-by: Barry Wu <a0987818905@gmail.com>
@BarryWu0812
BarryWu0812 requested a review from pingsutw August 27, 2025 15:29
Comment thread flytekit/core/type_engine.py Outdated
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
Copy Markdown
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

Comment thread tests/flytekit/unit/core/test_type_engine.py
Signed-off-by: Barry Wu <a0987818905@gmail.com>
Signed-off-by: Barry Wu <a0987818905@gmail.com>
Signed-off-by: Barry Wu <a0987818905@gmail.com>
@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 <a0987818905@gmail.com>
Co-authored-by: Barry Wu <barry@wukanglideMacBook-Pro.local>
Signed-off-by: Atharva <atharvakulkarni172003@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.

[Core feature] Add support Literal Transformer

4 participants