Hey, I don't get how validators work for results. ...
# prefect-community
a
Hey, I don't get how validators work for results. for example, in the following task
Copy code
task(get_data,
        result=LocalResult(serializer=JSONSerializer(), validators=lambda : False),
        target=join(p.data_path, "data.json"),
        checkpoint=True,
    )
If a target file already exist here, is the task run anyway because the validator returns False? What I want to achieve is to use a kind of "force_run" variable in a result validator (eg. defined in a user config) that forces to evaluate a task, even if there is already a result at the target location.
e
Going through something similar, wanting to validate a cache. Not sure if
validator
argument in the
Result
constructor is what you want. I don't think it is about cache. Have it worked for you?
a
I also don't think it is the way. If I remember well, somebody mentioned in the past to me that using Results and targets is prefered over the cache functionality (not sure) I guess I could make another (helper)task upstream that (conditionally) removes the target file. But I was hoping that it was somehow possible within the main task to check for this (to keep my code tidy 🙂 )
k
Hey @as, The Result validator passes the output of your task to the function you provide for validity, the Task will execute regardless of the resulting output. The target determines whether to write based on the existence of the file. I think if you want to write your result no matter what, using the
location
kwarg might be better. Additionally, this article goes into result targeting.
Apologies if I’m misunderstanding your use case here!
a
Hey, I want optionaly skip the check for a target file at the target location. So the task does not use the cache and gets executed and overwrites the old target file. (eg. sort of an "ignore_target" parameter in the task function) What would be the suggest prefect way to achieve this? I don't think there are clues towards this in the medium article you posted above. Thanks
k
Hmm, I’m not sure about a kwarg to optionally configure the target - you may need to create a custom task to check for that. For overwriting the file however, location kwargs are definitely the way to go, it will write at that pwd no matter what and can be templated.