From bfc2b15a655319589849d23cca108d58457019b5 Mon Sep 17 00:00:00 2001 From: Kevin Su Date: Wed, 28 Dec 2022 23:30:03 +0800 Subject: [PATCH] Convert default dict to json string in pyflyte run (#1399) Signed-off-by: Kevin Su Signed-off-by: Kevin Su Co-authored-by: Eduardo Apolinario <653394+eapolinario@users.noreply.github.com> --- flytekit/clis/sdk_in_container/run.py | 3 +++ tests/flytekit/unit/cli/pyflyte/workflow.py | 6 ++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/flytekit/clis/sdk_in_container/run.py b/flytekit/clis/sdk_in_container/run.py index 27aae9750e..615be3ba14 100644 --- a/flytekit/clis/sdk_in_container/run.py +++ b/flytekit/clis/sdk_in_container/run.py @@ -303,6 +303,9 @@ def convert_to_literal( if self._literal_type.simple or self._literal_type.enum_type: if self._literal_type.simple and self._literal_type.simple == SimpleType.STRUCT: if self._python_type == dict: + if type(value) != str: + # The type of default value is dict, so we have to convert it to json string + value = json.dumps(value) o = json.loads(value) elif type(value) != self._python_type: o = cast(DataClassJsonMixin, self._python_type).from_json(value) diff --git a/tests/flytekit/unit/cli/pyflyte/workflow.py b/tests/flytekit/unit/cli/pyflyte/workflow.py index 71d4e29a7c..85438eb00d 100644 --- a/tests/flytekit/unit/cli/pyflyte/workflow.py +++ b/tests/flytekit/unit/cli/pyflyte/workflow.py @@ -55,8 +55,9 @@ def print_all( j: datetime.timedelta, k: Color, l: dict, + m: dict, ): - print(f"{a}, {b}, {c}, {d}, {e}, {f}, {g}, {h}, {i}, {j}, {k}, {l}") + print(f"{a}, {b}, {c}, {d}, {e}, {f}, {g}, {h}, {i}, {j}, {k}, {l}, {m}") @task @@ -85,9 +86,10 @@ def my_wf( l: dict, remote: pd.DataFrame, image: StructuredDataset, + m: dict = {"hello": "world"}, ) -> Annotated[StructuredDataset, subset_cols]: x = get_subset_df(df=remote) # noqa: shown for demonstration; users should use the same types between tasks show_sd(in_sd=x) show_sd(in_sd=image) - print_all(a=a, b=b, c=c, d=d, e=e, f=f, g=g, h=h, i=i, j=j, k=k, l=l) + print_all(a=a, b=b, c=c, d=d, e=e, f=f, g=g, h=h, i=i, j=j, k=k, l=l, m=m) return x