https://prefect.io logo
j

Jasono

10/19/2020, 4:08 PM
Hi, I’m getting
Context object has no attribute 'today'
error when I try to get
prefect.context.today
in the Flow block. According to the documentation,
today
is one of those variables always available during task runs. What am I missing?
c

Chris White

10/19/2020, 4:16 PM
Are you referencing this attribute outside of a task? If so, remember that the Flow block builds your Flow but does not run it. This attribute is only populated in the context of a run
j

Jasono

10/19/2020, 4:31 PM
This is what I actually wanted to do.
Copy code
with Flow("prem_trend") as flow:

    task = RenameFlowRunTask()

    task(flow_run_id=prefect.context.flow_run_id), flow_run_name=f'run_{helpers.pstime()}')
Trying to pick up
flow_run_id
and replace the run name for it.
I get
Context object has no attribute flow_run_id
.
c

Chris White

10/19/2020, 4:50 PM
Yes, these variables are not populated until runtime but you are using them at build time in your example. To use a non-prefect specific example from your code:
Copy code
flow_run_name=f'run_{helpers.pstime()}'
This code is called once and the string is hardened when you first build this flow, it will not change each time you run the flow. If you want something dynamic, you should place this logic within your tasks or create new tasks that run this at runtime. To be very precise in your example, I would do:
Copy code
class MyRenameTask(RenameFlowRunTask):
    def run(self):
        return super.run(flow_run_id=prefect.context.flow_run_id), flow_run_name=f'run_{helpers.pstime()}')
j

Jasono

10/19/2020, 4:52 PM
Wow.. thanks. Let me try.
👍 1
sorry, it’s complaining
super
has no attribute
run
c

Chris White

10/19/2020, 5:03 PM
sorry, that should have been:
Copy code
return super().run(flow_run_id=prefect.context.flow_run_id), flow_run_name=f'run_{helpers.pstime()}')
note the parentheses
j

Jasono

10/19/2020, 5:04 PM
cool… it’s working 🙂