Prefect and dataclasses: I want to refactor my tas...
# prefect-community
a
Prefect and dataclasses: I want to refactor my tasks to pass around dataclasses instead of dicts and stuff. now, if I have a
dict
, prefect handles it by planting the
getItem
task in the DAG:
Copy code
dict_output = generate_dict()
a = task_a(dict_output['a'])
b = task_b(dict_output['b'])
How would you recommend working with dataclasses? The following piece of code won’t work, naturally (since a task doesn’t have these attributes)
Copy code
@dataclass
def MyClass:
    a: int
    b: int
...
...
dataclass_output = generate_dataclass()
a = task_a(dataclass_output.a)
b = task_b(dataclass_output.b)
c = another_task(dataclass_output)
j
Hey @Avi A - behind the scenes indexing is just creating one of these
GetItem
tasks (see here) - you could replicate this with a
GetAttribute
task that basically runs
lambda x: x.attr
. Adding sugar for that might be tricky, because we don’t want to allow magic attribute access to the task class. Maybe something like
dataclass_output.output.a
or something? Just idle thoughts.
a
Thanks @Jeremiah. So
dataclass_output.output
allows direct interaction with a `Task`’s result?
j
No, sorry for the confusion - I was trying to think of how we might implement something like what you’re looking for. Today that isn’t implemented.
You’d need to create a
GetAttribute
task like:
Copy code
@task
def get_attribute(task, attr):
    return getattr(task, attr)

a = get_attribute(dataclass_output, 'a')
P 1
a
yeah I got that part 🙂
10x!
j
👌 👌
a
follow-up: from what I could tell I see that
GetItem
doesn’t persist even though
Result
is defined on the flow, which makes a lot of sense b/c it just extracts data from existing data. How can I do the same for this task? would it help to just set
target=None
?
j
I think so - there’s nothing special about the
GetItem
task AFAIK
Except that it’s created automatically when you use
[]