Hi, when I and my colleague <@U04DZ030Q1K> debug c...
# ask-community
g
Hi, when I and my colleague @Cristian Federiconi debug code with Pycharm the prefect flows (@flow) return the state (State object) instead of actual data, as, on the other hand, normally happens when I run the code with "Run" or in the cloud. This causes the code we wrote to stop working when debugging because the State object obviously does not behave as the data type we expect. Is there a particular reason why this happens? Is it possible to set a parameter on the flow decorator to stop this behaviour? Thank you in advance for your response.
n
hi @Giovanni Giacco - can you show a code snippet with what you're expecting and what you're getting instead? if you don't have an explicit
return
value from a flow context, then the flow will return
None
just like any other python function if you successfully return a python object, it will behave from the outside like any python function, and Prefect will give the flow run a completed state in the backend - for example
Copy code
from prefect import flow

@flow 
def foo() -> str:
   return "Hello Marvin!"

assert foo() == "Hello Marvin!"
Copy code
11:59:33.104 | INFO    | prefect.engine - Created flow run 'invaluable-limpet' for flow 'foo'
11:59:33.106 | INFO    | Flow run 'invaluable-limpet' - View at <https://app.prefect.cloud/account/12242a57-9f05-4bf5-8853-9bff595d4bab/workspace/cafa2ffa-f6cc-4ed6-ab76-eaa4ba1ad40e/flow-runs/flow-run/bcc2aaa7-b05e-4f41-ad5b-59299027f9f9>
11:59:34.692 | INFO    | Flow run 'invaluable-limpet' - Finished in state Completed()
c
Hi Nate, thank you for your response. The flow in question contains two explicit returns, one returns a compound object, while, the other, None object. After further testing we noticed that the error occurs only when the branch with the return None is traversed and trying to replace None with a string (nonsense), then the str object was returned without any problem. The conclusion we came to is that prefect does not check for the presence of an explicit return, but whether or not the flow returns an object that is other than None: if flow_ret is not None: return flow_ret else: return State(...). After all, the python interpreter treats a function without explicit return as returning None so I expect prefect to do the same. Are our conclusions correct? Should we avoid returning None from flows?
n
sorry, I misspoke in my message above what I should have said was: if you don't have an explicit
return
value from a flow context, then the flow will return
None
just like any other python function the link I sent refers more to the state assumed by the flow based on the return value, which doesn't seem to be your concern as far as I'm aware, Prefect doesn't care what the return value is, if your flow-decorated function returns a value, it will be returned just like python
Copy code
In [14]: @flow
    ...: def test():
    ...:     return None
    ...:

In [15]: assert test() is None

12:00:28.672 | INFO    | prefect.engine - Created flow run 'bright-toad' for flow 'test'
12:00:38.510 | INFO    | Flow run 'bright-toad' - Finished in state Completed()

In [22]: @flow
    ...: def test():
    ...:     pass
    ...:

In [23]: assert test() is None
12:07:44.222 | INFO    | prefect.engine - Created flow run 'spirited-jaybird' for flow 'test'
12:07:45.243 | INFO    | Flow run 'spirited-jaybird' - Finished in state Completed()
so if this doesn't answer the question, perhaps you could provide your code and explain what exactly you're running into
c
Hi Nate, thanks again for the support. Your answer perfectly answered our doubts. By returning an object instead of "None", the problem no longer occurs. Thanks again for the fast and very accurate answers. Have a nice day, see you soon. 🙂