Hi community, I have some problem when implement t...
# ask-the-community
h
Hi community, I have some problem when implement the new function in Promise. I want to add is_None to check if the value of Promise is None, no matter how I pass the input, as long as I call this function. It will show ups this exception (attachment), However, I have no clue to this. Here is my new function I’ve tried
Copy code
def is_None(self):
        return ComparisonExpression(self, ComparisonOps.EQ, None)

    def is_None(self):
        return self == None
Here is my tested workflow
Copy code
@task(limits=Resources(mem="1Gi",cpu="1"))
def none_func()->str:
    return f"the input is None"

@task(limits=Resources(mem="1Gi",cpu="1"))
def not_none_func(obj)->str:
    return f"the input {obj} is not None"

@task(limits=Resources(mem="1Gi",cpu="1"))
def get_none() -> None:
    return None

@task(limits=Resources(mem="1Gi",cpu="1"))
def get_int() -> int:
    return 100

@workflow
def none_wf2()->str:
    my_input = get_int()

    return (
        conditional("testing_none")
        .if_(my_input.is_None())
        .then(none_func())
        .else_()
        .then(not_none_func(obj=my_input))
    )
Thanks for the response!
j
hm i think it might be this
is_(None)
here is the source code
Copy code
def is_(self, v: bool) -> ComparisonExpression:
        return ComparisonExpression(self, ComparisonOps.EQ, v)

    def is_false(self) -> ComparisonExpression:
        return self.is_(False)

    def is_true(self):
        return self.is_(True)
h
Hi @Jay Ganbat, Thanks for the response! I am curious the reason behind this thought, can you share a bit more?
btw, I just tired the method you provide! thanks again. Unfortunately, it still shows the same error
j
can you try
Optional[int]
in your get_int method since it returns Int so im not sure it can be none according to type
oh i took the method signature from
Promise
object, the way its called before is you are calling a custom method so i dont think it can be called on Promise object
h
Hi @Jay Ganbat, Thanks for the response! Here is my updated workflow, but it still shows up the same exception.
image.png
j
can you try this
is_(None)
you are still calling
is_None()
method 🤔
so it should be called
my_input.is_(None)
since
my_input
will be Promise type during registration
h
yes, you’re correct. After the test result based on the following, it still shows the same exception
Copy code
@workflow
def none_wf2()->str:
    my_input = get_others()
    method_source = inspect.getsource(my_input.is_None)
    print(method_source)

    return (
        conditional("testing_none")
        .if_(my_input.is_(None))
        .then(none_func())
        .else_()
        .then(not_none_func(obj=my_input))
    )
j
can you comment out the inspect part
h
ok
Here is the result. looks like the same
j
is your
get_others
same as
get_int
h
My bad, they are different, but similar. However, I change it to get_int, it still the same
j
ohhh i know why i think its your
none_func()
its returning
None
but your workflow outputs
str
so make the output of your worklfow Optional[str]
h
I see, Thanks for the advice. However, even I change the return type with Optional[str], the error remain the same. Here are the whole implementation of the workflow.
Copy code
@task(limits=Resources(mem="1Gi",cpu="1"))
def none_func()->Optional[str]:
    return f"the input is None"

@task(limits=Resources(mem="1Gi",cpu="1"))
def not_none_func(obj)->Optional[str]:
    return f"the input {obj} is not None"

@task(limits=Resources(mem="1Gi",cpu="1"))
def get_none() -> None:
    return None

@task(limits=Resources(mem="1Gi",cpu="1"))
def get_int() -> Optional[int]:
    return 100

@workflow
def none_wf2()->str:
    my_input = get_int()

    return (
        conditional("testing_none")
        .if_(my_input.is_(True))
        .then(none_func())
        .else_()
        .then(not_none_func(obj=my_input))
    )
I think the problem is where I call
my_input.is_(None)
=> that the part cause the error.
j
hmm its the
none_wf2() -> Optional[str]
but i actually never tried to explicitly check for None i usually just return
get_int()
or any conditional check to return bool and use is_true() on it
h
Hi @Jay Ganbat Sorry I don’t understand your point, can you be more specific?
j
we have 1 conditioanl workflow in our repo and it has the following structure
Copy code
def is_none(myinput) -> bool:
    return some_conditional_method(myinput)

def my_wf()...
    result = is_none(in_spec_path=in_spec_path)
    out_aligned_bam_gs, out_metrics = (
        conditional("MYWF")
        .if_(result.is_true())
        .then(
            call_first_task()
        )
        .else_()
        .then(
            call_the_other_task()
        )
    )
h
I see. Thanks for sharing.
Here is the related issue. https://github.com/flyteorg/flyte/issues/3514 That’s why I want to implement a function to test if it’s a none
@Jay Ganbat Thanks for the advice again!
j
ohh i see 😬 thats why it wasnt working 😅
h
But I think I need to do more than just ass Is_None