Hey, can Flyte handle `field` defaults on dataclas...
# ask-the-community
r
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
I think i may be able to handle constants, definitely not
lambda
as the dataclasses are converted to a json schema
r
it should be possible to execute that default_factory to get the constant it returns though no?
s
cc: @Kevin Su
k
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
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
I’m using the latest version of sandbox
s
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
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
Interesting though that in this case Flyteconsole showing the list entries only seems to work with a default parameter on task and workflow 🤔
r
nice to know, i still hadn't figured it out until you mentioned this
k
Wdym, default parameter on the task?
s
@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
This is odd, I think this is because of json schema
103 Views