<#1825 [flytekit] Support attribute access on prom...
# flyte-github
a
#1825 [flytekit] Support attribute access on promises Pull request opened by ByronHsu See flyteorg/flyteidl#439 Tracking Issue flyteorg/flyte#3864 Testing In the following workflow: •
basic_workflow
contains trivial examples to access output attributes •
failed_workflow
contains examples that causes exception (e.g. out of range) •
advanced_workflow
contains examples with more complex attribute access
Copy code
from dataclasses import dataclass
from typing import Dict, List, NamedTuple

from dataclasses_json import dataclass_json

from flytekit import WorkflowFailurePolicy, task, workflow


@dataclass_json
@dataclass
class foo:
    a: str


@task
def t1() -> (List[str], Dict[str, str], foo):
    return ["a", "b"], {"a": "b"}, foo(a="b")


@task
def t2(a: str) -> str:
    print("a", a)
    return a


@task
def t3() -> (Dict[str, List[str]], List[Dict[str, str]], Dict[str, foo]):
    return {"a": ["b"]}, [{"a": "b"}], {"a": foo(a="b")}


@task
def t4(a: List[str]):
    print("a", a)


@task
def t5(a: Dict[str, str]):
    print("a", a)


@workflow
def basic_workflow():
    l, d, f = t1()
    t2(a=l[0])
    t2(a=d["a"])
    t2(a=f.a)


@workflow(
    # The workflow doesn't fail when one of the nodes fails but other nodes are still executable
    failure_policy=WorkflowFailurePolicy.FAIL_AFTER_EXECUTABLE_NODES_COMPLETE
)
def failed_workflow():
    # This workflow is supposed to fail due to exceptions
    l, d, f = t1()
    t2(a=l[100])
    t2(a=d["b"])
    t2(a=f.b)


@workflow
def advanced_workflow():
    dl, ld, dd = t3()
    t2(a=dl["a"][0])
    t2(a=ld[0]["a"])
    t2(a=dd["a"].a)

    t4(a=dl["a"])
    t5(a=ld[0])
Local execution
Copy code
$ pyflyte run test_workflow.py basic_workflow   

a a
a b
a b

$ pyflyte run test_workflow.py failed_workflow

Failed with Exception Code: USER:PromiseAttributeResolveError
Underlying Exception: Failed to resolve attribute path [100] in promise Resolved(o0=<FlyteLiteral collection { literals { scalar { primitive { string_value: "a" } } } literals { scalar { primitive { string_value: "b" } } } }>), index 100 out of range 2
Encountered error while executing workflow 'test_workflow.failed_workflow':
  Error encountered while executing 'failed_workflow':
  Failed to resolve attribute path [100] in promise Resolved(o0=<FlyteLiteral collection { literals { scalar { primitive { string_value: "a" } } } literals { scalar { primitive { string_value: "b" } } } }>), index 100 out of range 2

$ pyflyte run test_workflow.py advanced_workflow
a b
a b
a b
a ['b']
a {'a': 'b'}
Remote Execution basic

https://user-images.githubusercontent.com/24364830/267101816-fd1b65cb-eb2e-49ef-b773-d2d621aff5b7.png▾

failed

https://user-images.githubusercontent.com/24364830/267101784-e686d575-a97e-494a-acb7-1fdb7e9b328d.png▾

advanced

https://user-images.githubusercontent.com/24364830/267101581-b6100bbd-2025-4813-b661-5f7f6c0e5649.png▾

flyteorg/flytekit Codecov: 54.62% (-0.10%) compared to 4d5e1b8 Codecov: 25.84% of diff hit (target 54.71%) ✅ 28 other checks have passed 28/30 successful checks