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

Fix Get Literal Type Error for Attribute Access Compile in Flytepropeller #2749

Merged
merged 1 commit into from
Sep 12, 2024

Conversation

Future-Outlier
Copy link
Member

@Future-Outlier Future-Outlier commented Sep 12, 2024

Tracking issue

flyteorg/flyte#5427

Why are the changes needed?

This will make the propeller get the attribute access's type correct.

  • Before
Error 0: Code: MismatchingTypes, Node Id: n4, Description: Variable [o0] (type [simple:STRING]) doesn't match expected type [simple:STRUCT  metadata:{fields:{key:"additionalProperties"  value:{bool_value:false}}  fields:{key:"properties"  value:{struct_value:{fields:{key:"name"  value:{struct_value:{fields:{key:"type"  value:{string_value:"string"}}}}}}}}  fields:{key:"required"  value:{list_value:{values:{string_value:"name"}}}}  fields:{key:"title"  value:{string_value:"Fruit"}}  fields:{key:"type"  value:{string_value:"object"}}}  structure:{dataclass_type:{key:"name"  value:{simple:STRING}}}].
Error 1: Code: ParameterNotBound, Node Id: n4, Description: Parameter not bound [fruit_instance]
  • After
image

What changes were proposed in this pull request?

Make the provided literal type's dataclass type correct, this will be used when resolving the attribute type in the propeller.

Setup process

from __future__ import annotations

from dataclasses import dataclass

from flytekit import task, workflow
from mashumaro.mixins.json import DataClassJSONMixin


@dataclass
class Fruit(DataClassJSONMixin):
    name: str


@dataclass
class NestedFruit(DataClassJSONMixin):
    sub_fruit: Fruit
    name: str


@task
def dataclass_task() -> Fruit:
    return Fruit(name="banana")


@task
def dataclass_task_nested() -> NestedFruit:
    return NestedFruit(sub_fruit=Fruit(name="banana"), name="nested_name")


@task
def print_message(message: str):
    print(message)
    return


@task
def print_message_fruit(fruit_instance: Fruit):
    print(fruit_instance.name)
    print(fruit_instance)
    return


@task
def print_message_nested(nested_fruit: NestedFruit):
    print(nested_fruit.sub_fruit.name)
    print(nested_fruit.name)
    return


@workflow
def dataclass_wf():
    fruit_instance = dataclass_task()
    nested_fruit_instance = dataclass_task_nested()

    # ✅ accessing attribute of type=str
    print_message(message=fruit_instance.name)
    # ✅ non attribute access
    print_message_nested(nested_fruit=nested_fruit_instance)
    # ✅ accessing attribute of type=str on a nested dataclass
    print_message(message=nested_fruit_instance.sub_fruit.name)
    # ❌ accessing attribute of type=dataclass
    print_message_fruit(fruit_instance=nested_fruit_instance.sub_fruit)

if __name__ == "__main__":
    from flytekit.clis.sdk_in_container import pyflyte
    from click.testing import CliRunner

    runner = CliRunner()
    path = "/Users/future-outlier/code/dev/flytekit/build/PR/dataclass-compiler-literal-type-error/example.py"
    result = runner.invoke(pyflyte.main,
                           ["run", path, "dataclass_wf"])

    print("Local Execution: ", result.output)

    result = runner.invoke(pyflyte.main,
                           ["run", "--remote", path, "dataclass_wf"])

    print("Remote Execution: ", result.output)

Screenshots

image image

Check all the applicable boxes

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

Related PRs

Docs link

…ller

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

literal_type = de.get_literal_type(NestedFruit)
dataclass_type = literal_type.structure.dataclass_type
assert dataclass_type["sub_fruit"].simple == SimpleType.STRUCT
Copy link
Member Author

Choose a reason for hiding this comment

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

before this will be STRING because we didn't use get_type_hints.

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.

LGTM, could you update the PR description

@Future-Outlier Future-Outlier merged commit c06ef30 into master Sep 12, 2024
101 checks passed
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.

2 participants