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

[Docs] MessagePack IDL, Pydantic Support, and Attribute Access #1770

Merged
merged 9 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
1 change: 1 addition & 0 deletions dev-requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ coverage
pre-commit
codespell
mock
pydantic>2
pytest
mypy
mashumaro
Expand Down
2 changes: 1 addition & 1 deletion examples/data_types_and_io/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RUN python3 -m venv ${VENV}
ENV PATH="${VENV}/bin:$PATH"

RUN --mount=type=cache,sharing=locked,mode=0777,target=/root/.cache/pip,id=pip \
pip install flytekit pandas pyarrow
pip install flytekit pandas pyarrow pydantic>2
RUN --mount=type=cache,sharing=locked,mode=0777,target=/root/.cache/pip,id=pip \
pip install torch --index-url https://download.pytorch.org/whl/cpu

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from dataclasses import dataclass

from dataclasses_json import dataclass_json
from flytekit import task, workflow


Expand Down Expand Up @@ -36,7 +35,6 @@ def dict_wf():


# Directly access an attribute of a dataclass
@dataclass_json
@dataclass
class Fruit:
name: str
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from flytekit import ImageSpec, task, workflow
from pydantic import BaseModel

image_spec = ImageSpec(
registry="ghcr.io/flyteorg",
packages=["pydantic>2"],
)


@task(container_image=image_spec)
def print_message(message: str):
print(message)
return


# Access an output list using index notation
@task(container_image=image_spec)
def list_task() -> list[str]:
return ["apple", "banana"]


@workflow
def list_wf():
items = list_task()
first_item = items[0]
print_message(message=first_item)


# Access the output dictionary by specifying the key
@task(container_image=image_spec)
def dict_task() -> dict[str, str]:
return {"fruit": "banana"}


@workflow
def dict_wf():
fruit_dict = dict_task()
print_message(message=fruit_dict["fruit"])


# Directly access an attribute of a Pydantic BaseModel
class Fruit(BaseModel):
name: str


@task(container_image=image_spec)
def basemodel_task() -> Fruit:
return Fruit(name="banana")


@workflow
def basemodel_wf():
fruit_instance = basemodel_task()
print_message(message=fruit_instance.name)


# Combinations of list, dict, and BaseModel also work effectively
@task(container_image=image_spec)
def advance_task() -> (dict[str, list[str]], list[dict[str, str]], dict[str, Fruit]):
return (
{"fruits": ["banana"]},
[{"fruit": "banana"}],
{"fruit": Fruit(name="banana")},
)


@task(container_image=image_spec)
def print_list(fruits: list[str]):
print(fruits)


@task(container_image=image_spec)
def print_dict(fruit_dict: dict[str, str]):
print(fruit_dict)


@workflow
def advanced_workflow():
dictionary_list, list_dict, dict_basemodel = advance_task()
print_message(message=dictionary_list["fruits"][0])
print_message(message=list_dict[0]["fruit"])
print_message(message=dict_basemodel["fruit"].name)

print_list(fruits=dictionary_list["fruits"])
print_dict(fruit_dict=list_dict[0])


# Run the workflows locally
if __name__ == "__main__":
list_wf()
dict_wf()
basemodel_wf()
advanced_workflow()
1 change: 1 addition & 0 deletions examples/data_types_and_io/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ torch
tabulate
tensorflow
pyarrow
pydantic>2
Loading