Hi — when is the `today` template filled in a resu...
# ask-community
d
Hi — when is the
today
template filled in a result
location
or task
target
string? I keep getting a `KeyError`:
Copy code
from prefect.engine.results import LocalResult

# Create a new local result pointing to current working directory.
result = LocalResult(dir=".", location="{today}.txt")
assert result.location == "{today}.txt"

# This raises `KeyError: 'today'`.
result.write(value_="hello!")
I’m following this doc: https://docs.prefect.io/core/concepts/results.html#choose-a-serializer (under “Templating
Result
locations”)
k
It is filled in during the Flow run after context is populated. If this is a test, maybe with
with context...
d
Hmm so I’m testing using
TaskRunner
and it doesn’t seem to populate the context: is that just a limitation of
TaskRunner
? Here’s a reproducible example:
Copy code
from prefect import task
from prefect.engine.results import LocalResult
from prefect.engine.task_runner import TaskRunner


@task(result=LocalResult(location="{today}.prefect"))
def root_task():
    return [1, 2, 3]


task_runner: TaskRunner = TaskRunner(task=root_task)
task_runner.run()
Produces:
Copy code
[2021-12-17 13:55:05-0800] INFO - prefect.TaskRunner | Task 'root_task': Starting task run...
[2021-12-17 13:55:05-0800] ERROR - prefect.TaskRunner | Unexpected error: KeyError('today')
Traceback (most recent call last):
  File "/home/smds/.cache/pypoetry/virtualenvs/prefectx-yDPAt8fm-py3.7/lib/python3.7/site-packages/prefect/engine/runner.py", line 48, in inner
    new_state = method(self, state, *args, **kwargs)
  File "/home/smds/.cache/pypoetry/virtualenvs/prefectx-yDPAt8fm-py3.7/lib/python3.7/site-packages/prefect/engine/task_runner.py", line 926, in get_task_run_state
    result = self.result.write(value, **formatting_kwargs)
  File "/home/smds/.cache/pypoetry/virtualenvs/prefectx-yDPAt8fm-py3.7/lib/python3.7/site-packages/prefect/engine/results/local_result.py", line 106, in write
    new = self.format(**kwargs)
  File "/home/smds/.cache/pypoetry/virtualenvs/prefectx-yDPAt8fm-py3.7/lib/python3.7/site-packages/prefect/engine/result/base.py", line 133, in format
    new.location = new.location.format(**kwargs)
KeyError: 'today'
[2021-12-17 13:55:05-0800] INFO - prefect.TaskRunner | Task 'root_task': Finished task run for task with final state: 'Failed'
Out[1]: <Failed: "Unexpected error: KeyError('today')">
k
I guess so. It might be at the context. I’ll ping someone but a lot of people are out so I dunno if we’ll get a response
d
No worries! I can test using the proper
Flow
for now. Thanks @Kevin Kho
k
Oh I meant try something with defining the context before you execute the task runner like this: https://github.com/PrefectHQ/prefect/blob/master/tests/engine/test_task_runner.py#L225-L236
Actually this specific test might work for you
d
Ah yeah this works as expected:
Copy code
import pendulum
import prefect
from prefect import task
from prefect.engine.results import LocalResult
from prefect.engine.task_runner import TaskRunner


@task(result=LocalResult(dir="."), checkpoint=True, target="{today}-{date:%Y}.prefect")
def root_task():
    return [1, 2, 3]


with prefect.context(date=pendulum.today(), today=pendulum.today().strftime("%Y-%m-%d")):
    task_runner: TaskRunner = TaskRunner(task=root_task)
    task_runner.run()
Thanks @Kevin Kho!
k
ah nice!