Do I need to call "result()" on a task call returning a bool

The first snippet results in the else statement; the second, is expected, but do I really need to call .result() for this? If so, I am confused about the rules when Orion automatically unravels the result without need for .result().

@task
def check_if_exists():
    return False


@flow
def test():
    if not check_if_exists():
        print("yes it doesnt exist")
    else:
        print("huh not supposed to happen")


test()

# outputs huh not supposed to happen

@task
def check_if_exists():
    return False


@flow
def test():
    if not check_if_exists().result():
        print("yes it doesnt exist")
    else:
        print("huh not supposed to happen")


test()
# outputs yes it doesnt exist

This is the right way of doing that.

You need to call .result() because in this line you want to check the actual underlying data (the value False) and Prefect will retrieve that data from the PrefectFuture by accessing its result.

Would it make sense to raise an error or a warning that user used if is_something_task() expecting it to unwrap? Right now I think it would always return True regardless of what was returned from is_something_task and I think that would trip up a lot of users (like me).

The other idea is if Orion can detect it’s checking logic and automatically call result() for the user (this was my misconception / expected behavior)

1 Like

I agree - this would be helpful. Would you open an issue for it in the Orion repo?

We thought about it but in the end, this was not desirable. I think @kvnkho can probably explain it better since he talked to Bill about that.

1 Like

Ah our discussions were just about turning .wait().result() into .result() and then inferring the wait. @ahuang11 suggests finding a way to raise a warning if a user is using the native Python if. I don’t know the specifics of how the futures are implemented, but I think it might be feasible since you can override the __bool__ method and put a warning there right? The Python if will call that method if my understanding is right. And then you can also explicitly call wait() or result() to evaluate the Future and compare the result.

I don’t know for sure. Worth opening an issue in the Orion repo :slight_smile:

2 Likes