Skip to content

Commit

Permalink
Support Passing Dataclass Values via Command Line (#2446)
Browse files Browse the repository at this point in the history
Signed-off-by: Future-Outlier <[email protected]>
  • Loading branch information
Future-Outlier authored Jun 4, 2024
1 parent c388a43 commit 9872e65
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion flytekit/interaction/click_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import cloudpickle
import rich_click as click
import yaml
from dataclasses_json import DataClassJsonMixin
from dataclasses_json import DataClassJsonMixin, dataclass_json
from pytimeparse import parse

from flytekit import BlobType, FlyteContext, FlyteContextManager, Literal, LiteralType, StructuredDataset
Expand Down Expand Up @@ -273,6 +273,11 @@ def convert(

if is_pydantic_basemodel(self._python_type):
return self._python_type.parse_raw(json.dumps(parsed_value)) # type: ignore

# Ensure that the python type has `from_json` function
if not hasattr(self._python_type, "from_json"):
self._python_type = dataclass_json(self._python_type)

return cast(DataClassJsonMixin, self._python_type).from_json(json.dumps(parsed_value))


Expand Down
20 changes: 20 additions & 0 deletions tests/flytekit/unit/interaction/test_click_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,23 @@ def test_query_passing(param_type: click.ParamType):
query = a.query()

assert param_type.convert(value=query, param=None, ctx=None) is query


def test_dataclass_type():
from dataclasses import dataclass

@dataclass
class Datum:
x: int
y: str
z: dict[int, str]
w: list[int]

t = JsonParamType(Datum)
value = '{ "x": 1, "y": "2", "z": { "1": "one", "2": "two" }, "w": [1, 2, 3] }'
v = t.convert(value=value, param=None, ctx=None)

assert v.x == 1
assert v.y == "2"
assert v.z == {1: "one", 2: "two"}
assert v.w == [1, 2, 3]

0 comments on commit 9872e65

Please sign in to comment.