https://flyte.org logo
#ask-the-community
Title
# ask-the-community
r

Robin Kahlow

10/04/2022, 5:44 PM
Hey, can Flyte handle
field
defaults on dataclasses? Dataclasses can't have list / dict / set defaults (https://stackoverflow.com/q/52063759) and you have to resort to something like
str_list: List[str] = field(default_factory=lambda: ["a", "b"])
instead, but on Flyte console when launching a workflow it just shows an empty list for me.
k

Ketan (kumare3)

10/04/2022, 8:27 PM
I think i may be able to handle constants, definitely not
lambda
as the dataclasses are converted to a json schema
r

Robin Kahlow

10/05/2022, 11:32 AM
it should be possible to execute that default_factory to get the constant it returns though no?
s

Samhita Alla

10/06/2022, 6:45 AM
cc: @Kevin Su
k

Kevin Su

10/06/2022, 7:33 PM
My task
Copy code
@dataclass_json
@dataclass
class Args(object):
    max_iter: int = 0
    str_list: List[str] = field(default_factory=lambda: ["a", "b"])


@task
def t1(schema: Args = Args(1)):
    print(schema)
r

Robin Kahlow

10/07/2022, 10:41 AM
I was using 1.1.1, but updated to 1.2.1 and have the same problem there. Was it filled in the launch workflow form for you too? // will try to make a repro later
k

Kevin Su

10/07/2022, 4:21 PM
I’m using the latest version of sandbox
s

Sören Brunk

11/23/2022, 2:11 PM
I just ran into this as well. I my case, the list is only filled if I actually have a default parameter that creates an instance of the dataclass.
Copy code
@task
def t1(schema: Args = Args(1)):
    print(schema)
If, on the other hand there is no default parameter (it is only set in a launchplan in our case), it is empty like in @Robin Kahlow's case:
Copy code
@task
def t1(schema: Args):
    print(schema)
@Kevin Su what happens if you leave out the default parameter?
k

Ketan (kumare3)

11/23/2022, 3:41 PM
So tasks do not support default parameters
This default is working on the python side
Only default supported is none
Cc @Yee maybe we should have an error of this case
s

Sören Brunk

11/23/2022, 5:40 PM
Interesting though that in this case Flyteconsole showing the list entries only seems to work with a default parameter on task and workflow 🤔
r

Robin Kahlow

11/24/2022, 6:02 PM
nice to know, i still hadn't figured it out until you mentioned this
k

Ketan (kumare3)

11/24/2022, 6:09 PM
Wdym, default parameter on the task?
s

Sören Brunk

11/24/2022, 9:41 PM
@Ketan (kumare3) like this:
Copy code
@task
def t1(schema: Args = Args(1)):
    print(schema)
You need to add the dataclass constructor call
Args()
as a default parameter to the task. It seems only then Flyte will fill in lists defined as default in the
Args
dataclass.
k

Ketan (kumare3)

11/24/2022, 10:58 PM
This is odd, I think this is because of json schema