Hi! Quick question about the equality operator. No...
# ask-community
c
Hi! Quick question about the equality operator. Noticed that
prefect.core.task
understandably does not override
__eq__
__neq__
methods to be able to suppoort task comparisons as explained in
is_equal
method docstring: https://github.com/PrefectHQ/prefect/blob/1959eecf1bbbb8e3b194b288c197a6108deb8693/src/prefect/core/task.py#L924-L938 Just to confirm, this would be an appropriate way to resolve task result equality within a flow, right?
Copy code
with Flow('my-flow') as flow:
    task_a = get_task_a_result()
    task_a_is_value = task_a.is_equal('value')
Also, I couldn't find in docs anywhere that mentioned some operators may lead to silently lead to unexpected behavior (relative to other operators for which magic methods are overridden). Did I miss this somewhere? For example, this will always resolve
task_a_is_value
to
False
Copy code
with Flow('my-flow') as flow:
    task_a = get_task_a_result()
    task_a_is_value = task_a == 'value'
If not already documented somewhere else, it might be nice to have clarification in Operators docs that a few operators are not overridden when inlines (didn't look at whether there are any others that couldn't be overridden outside of
__eq__
  
__neq__
k
Hey @Cab Maddux, I don’t think this works. What are you trying to do with the comparison?
c
This works (which looks from
prefect.core.task.Task.is_equal
method docstring to be the correct usage as I can tell):
Copy code
with Flow('my-flow') as flow:
    task_a = get_task_a_result()
    task_a_is_value = task_a.is_equal('value')
This does not work:
Copy code
with Flow('my-flow') as flow:
    task_a = get_task_a_result()
    task_a_is_value = task_a == 'value'
Although was a bit confusing as other operators (
+
,
-
etc.) magic methods are overridden
Just wanted to 1. Confirm that the first code snippet above is the correct usage of
Task.is_equal(...)
2. If so, note that may be useful to update
Operator Tasks
docs (so others don't haven't to go into the source to sort out)
k
Ah ok. Let me confirm with the team if that is the right syntax. I was asking on the use case because normally you would compare as a logical check with the
case
task like an if-else, so that checks for equality.
Btw, when you tested if it worked, did you try values that weren’t equal? What did it return for you?
Yes you are right on the
is_equal
. That is correct usage.
m
Followup question, how would I used
is_equal
to check on length of a python list? (this is output of a task in a flow)
k
I would really use a lambda like:
Copy code
with Flow("test") as flow:
    a = abc(1)
    b = task(lambda x: len(x) == 3)(a)
because
is_equal
is just value comparison
m
Thank you, I’ll give this a try 🙂
c
Got it, thanks @Kevin Kho!