Hi everyone, I have a task that I want to dynamica...
# ask-community
c
Hi everyone, I have a task that I want to dynamically assign tags for when invoking the task (there is an argument my_tags), and i want to add it to tags in this manner. Im not sure why there's an error, can anyone explain why this doesnt work?
n
hey @Charles Leung - i think its just because we don't template in parameters for tag values there instead you can use
with_options
like this
Copy code
In [1]: from prefect import flow, task

In [2]: @task
   ...: def foo():
   ...:     pass
   ...:

In [3]: @flow
   ...: def bar():
   ...:     foo.with_options(tags={"special-tag"})()
c
ah okay interesting. I was reading the code and thought it meant we could pass in a list in the decorator, but I could be mistaken
Thank you so much for your help!!
n
you can pass tags into the decorator to define the tags statically!
with_options
is just a
@classmethod
that allows you the pass those decorator kwargs in again (after defining it), which is espcially helpful when you want to customize a task definition based on runtime data (e.g. set tags for a task based on inputs for a flow) for certain decorator kwargs (result_storage_key, task_run_name etc) , we make the parameters of the task templatable like you tried first but we don't do that for
tags
as far as I know
c
oh I see - understood. One new issue I found when inserting
._with_options()
is that it now does not rename my task name and keeps it as the name of the function (async_query). This is how I'm invoking the task within my flow:
n
hmm if you're just using
task_name
to name your prefect task, you could just remove
task_name
as a kwarg of our task and just pass
name="1_data_wait..."
to
with_options
along with
tags
c
yeah i was thinking that too - i changed it and its all working now
thank you for your input 🙂
n
catjam