-
Notifications
You must be signed in to change notification settings - Fork 669
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] Use Pure Dataclass In Example #5829
Changes from 4 commits
fa4a0d7
44d198f
6710b19
9a4078e
6f90ff4
389b097
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,11 +12,10 @@ Flytekit uses the [Mashumaro library](https://github.com/Fatal1ty/mashumaro) | |||||
to serialize and deserialize dataclasses. | ||||||
|
||||||
:::{important} | ||||||
If you're using Flytekit version below v1.10, you'll need to decorate with `@dataclass_json` using | ||||||
`from dataclasses_json import dataclass_json` instead of inheriting from Mashumaro's `DataClassJSONMixin`. | ||||||
|
||||||
If you're using Flytekit version >= v1.11.1, you don't need to decorate with `@dataclass_json` or | ||||||
If you're using Flytekit version below v1.11.1, your dataclass will need to decorate with `@dataclass_json` or | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me know if this is accurate! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's not, but I can update it |
||||||
inherit from Mashumaro's `DataClassJSONMixin`. | ||||||
|
||||||
If you're using Flytekit version less than v1.10, you'll need to add `from dataclasses_json import dataclass_json` to your imports and decorate your dataclass with `@dataclass_json`. | ||||||
::: | ||||||
|
||||||
```{note} | ||||||
|
@@ -25,15 +24,21 @@ To clone and run the example code on this page, see the [Flytesnacks repo][flyte | |||||
|
||||||
To begin, import the necessary dependencies: | ||||||
|
||||||
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/69dbe4840031a85d79d9ded25f80397c6834752d/examples/data_types_and_io/data_types_and_io/dataclass.py | ||||||
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/cfb5ea3b0d0502ef7df1f2e14f4a0d9b78250b6a/examples/data_types_and_io/data_types_and_io/dataclass.py | ||||||
:caption: data_types_and_io/dataclass.py | ||||||
:lines: 1-9 | ||||||
``` | ||||||
|
||||||
Build your custom image with ImageSpec: | ||||||
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/cfb5ea3b0d0502ef7df1f2e14f4a0d9b78250b6a/examples/data_types_and_io/data_types_and_io/dataclass.py | ||||||
:caption: data_types_and_io/dataclass.py | ||||||
:lines: 1-10 | ||||||
:lines: 16-19 | ||||||
``` | ||||||
|
||||||
## Python types | ||||||
We define a `dataclass` with `int`, `str` and `dict` as the data types. | ||||||
|
||||||
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/69dbe4840031a85d79d9ded25f80397c6834752d/examples/data_types_and_io/data_types_and_io/dataclass.py | ||||||
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/cfb5ea3b0d0502ef7df1f2e14f4a0d9b78250b6a/examples/data_types_and_io/data_types_and_io/dataclass.py | ||||||
:caption: data_types_and_io/dataclass.py | ||||||
:pyobject: Datum | ||||||
``` | ||||||
|
@@ -46,41 +51,41 @@ All variables in a data class should be **annotated with their type**. Failure t | |||||
|
||||||
Once declared, a dataclass can be returned as an output or accepted as an input. | ||||||
|
||||||
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/69dbe4840031a85d79d9ded25f80397c6834752d/examples/data_types_and_io/data_types_and_io/dataclass.py | ||||||
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/cfb5ea3b0d0502ef7df1f2e14f4a0d9b78250b6a/examples/data_types_and_io/data_types_and_io/dataclass.py | ||||||
:caption: data_types_and_io/dataclass.py | ||||||
:lines: 28-43 | ||||||
:lines: 32-47 | ||||||
``` | ||||||
|
||||||
## Flyte types | ||||||
We also define a data class that accepts {std:ref}`StructuredDataset <structured_dataset>`, | ||||||
{std:ref}`FlyteFile <files>` and {std:ref}`FlyteDirectory <folder>`. | ||||||
|
||||||
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/69dbe4840031a85d79d9ded25f80397c6834752d/examples/data_types_and_io/data_types_and_io/dataclass.py | ||||||
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/cfb5ea3b0d0502ef7df1f2e14f4a0d9b78250b6a/examples/data_types_and_io/data_types_and_io/dataclass.py | ||||||
:caption: data_types_and_io/dataclass.py | ||||||
:lines: 47-84 | ||||||
:lines: 51-88 | ||||||
``` | ||||||
|
||||||
A data class supports the usage of data associated with Python types, data classes, | ||||||
flyte file, flyte directory and structured dataset. | ||||||
|
||||||
We define a workflow that calls the tasks created above. | ||||||
|
||||||
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/69dbe4840031a85d79d9ded25f80397c6834752d/examples/data_types_and_io/data_types_and_io/dataclass.py | ||||||
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/cfb5ea3b0d0502ef7df1f2e14f4a0d9b78250b6a/examples/data_types_and_io/data_types_and_io/dataclass.py | ||||||
:caption: data_types_and_io/dataclass.py | ||||||
:pyobject: dataclass_wf | ||||||
``` | ||||||
|
||||||
You can run the workflow locally as follows: | ||||||
|
||||||
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/69dbe4840031a85d79d9ded25f80397c6834752d/examples/data_types_and_io/data_types_and_io/dataclass.py | ||||||
```{rli} https://raw.githubusercontent.com/flyteorg/flytesnacks/cfb5ea3b0d0502ef7df1f2e14f4a0d9b78250b6a/examples/data_types_and_io/data_types_and_io/dataclass.py | ||||||
:caption: data_types_and_io/dataclass.py | ||||||
:lines: 97-98 | ||||||
:lines: 101-102 | ||||||
``` | ||||||
|
||||||
To trigger a task that accepts a dataclass as an input with `pyflyte run`, you can provide a JSON file as an input: | ||||||
``` | ||||||
pyflyte run \ | ||||||
https://raw.githubusercontent.com/flyteorg/flytesnacks/69dbe4840031a85d79d9ded25f80397c6834752d/examples/data_types_and_io/data_types_and_io/dataclass.py \ | ||||||
https://raw.githubusercontent.com/flyteorg/flytesnacks/cfb5ea3b0d0502ef7df1f2e14f4a0d9b78250b6a/examples/data_types_and_io/data_types_and_io/dataclass.py \ | ||||||
add --x dataclass_input.json --y dataclass_input.json | ||||||
``` | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to clarify, this means that if you are using Flytekit < 1.11.1, you either need to decorate your dataclass with
@dataclass_json
or create a class that inheritsDataClassJSONMixin
, but that you don't need to do both? If you inherit fromDataClassJSONMixin
, do you need to dofrom mashumaro.mixins.json import DataClassJSONMixin
as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes!