https://prefect.io logo
Title
j

José Duarte

11/03/2022, 12:39 PM
How can I set a state with no data? Prefect throws an error if there’s no data set, but I have nothing to return
k

Khuyen Tran

11/03/2022, 4:07 PM
What do you mean by setting a state without data? Can you show example code for this?
j

José Duarte

11/03/2022, 4:07 PM
Completed()
That is a state without data
I found the issue and worked around it regardless. I saw how a flow is called behind the scenes and replicated it with
return_type='state'
instead of
return_type='result'
k

Khuyen Tran

11/03/2022, 4:10 PM
This works for me:
from prefect import flow 
from prefect.states import Completed

@flow()
def my_flow():
    return Completed() 

if __name__=="__main__":
    my_flow()
j

José Duarte

11/03/2022, 4:11 PM
It doesn’t for me:
In [24]: from prefect import flow
    ...: from prefect.states import Completed
    ...: 
    ...: @flow()
    ...: def my_flow():
    ...:     return Completed()
    ...: 
    ...: if __name__=="__main__":
    ...:     my_flow()
    ...: 
16:10:44.761 | INFO    | prefect.engine - Created flow run 'psychedelic-degu' for flow 'my-flow'
16:10:45.358 | INFO    | Flow run 'psychedelic-degu' - Finished in state Completed()
> /Users/duarte/Documents/data/pdbt/.venv/lib/python3.8/site-packages/prefect/states.py(87)_get_state_result()
-> raise MissingResult(
(Pdb)
[16:11:15] Φ prefect version    
Version:             2.6.4
API version:         0.8.2
Python version:      3.8.9
Git commit:          51e92dda
Built:               Thu, Oct 20, 2022 3:11 PM
OS/Arch:             darwin/x86_64
Profile:             default
Server type:         ephemeral
Server:
  Database:          sqlite
  SQLite version:    3.32.3
k

Khuyen Tran

11/03/2022, 4:13 PM
This is what I saw:
11:13:05.135 | INFO    | prefect.engine - Created flow run 'acrid-viper' for flow 'my-flow'               
11:13:08.721 | INFO    | prefect.flow_runs - Finished in state Completed()
and I tested with 2.6.4
It seems like your flow run completed
but you got the error because of something else
It seems like you are using a debugger tool. Where did you put the breakpoint?
j

José Duarte

11/03/2022, 4:17 PM
without the breakpoint
In [1]: from prefect import flow
   ...: from prefect.states import Completed
   ...: 
   ...: @flow()
   ...: def my_flow():
   ...:     return Completed()
   ...: 

In [2]: my_flow()
16:16:44.507 | INFO    | prefect.engine - Created flow run 'brilliant-dugong' for flow 'my-flow'
16:16:44.674 | INFO    | Flow run 'brilliant-dugong' - Finished in state Completed()
------------------------------------------------------------------------
MissingResult                          Traceback (most recent call last)
Cell In [2], line 1
----> 1 my_flow()

File ~/Documents/data/pdbt/.venv/lib/python3.8/site-packages/prefect/flows.py:439, in Flow.__call__(self, return_state, *args, **kwargs)
    435 parameters = get_call_parameters(self.fn, args, kwargs)
    437 return_type = "state" if return_state else "result"
--> 439 return enter_flow_run_engine_from_flow_call(
    440     self, parameters, return_type=return_type
    441 )

File ~/Documents/data/pdbt/.venv/lib/python3.8/site-packages/prefect/engine.py:155, in enter_flow_run_engine_from_flow_call(flow, parameters, return_type)
    152             return portal.call(begin_run)
    153     else:
    154         # An event loop is not running so we will create one
--> 155         return anyio.run(begin_run)
    157 if not parent_flow_run_context.flow.isasync:
    158     # Async subflow run in sync flow run
    159     return run_async_from_worker_thread(begin_run)

File ~/Documents/data/pdbt/.venv/lib/python3.8/site-packages/anyio/_core/_eventloop.py:70, in run(func, backend, backend_options, *args)
     68 try:
     69     backend_options = backend_options or {}
---> 70     return asynclib.run(func, *args, **backend_options)
     71 finally:
     72     if token:

File ~/Documents/data/pdbt/.venv/lib/python3.8/site-packages/anyio/_backends/_asyncio.py:292, in run(func, debug, use_uvloop, policy, *args)
    289         del _task_states[task]
    291 _maybe_set_event_loop_policy(policy, use_uvloop)
--> 292 return native_run(wrapper(), debug=debug)

File ~/.pyenv/versions/3.8.9/lib/python3.8/asyncio/runners.py:44, in run(main, debug)
     42     if debug is not None:
     43         loop.set_debug(debug)
---> 44     return loop.run_until_complete(main)
     45 finally:
     46     try:

File ~/.pyenv/versions/3.8.9/lib/python3.8/asyncio/base_events.py:616, in BaseEventLoop.run_until_complete(self, future)
    613 if not future.done():
    614     raise RuntimeError('Event loop stopped before Future completed.')
--> 616 return future.result()

File ~/Documents/data/pdbt/.venv/lib/python3.8/site-packages/anyio/_backends/_asyncio.py:287, in run.<locals>.wrapper()
    284     task.set_name(task_state.name)
    286 try:
--> 287     return await func(*args)
    288 finally:
    289     del _task_states[task]

File ~/Documents/data/pdbt/.venv/lib/python3.8/site-packages/prefect/client/utilities.py:47, in inject_client.<locals>.with_injected_client(*args, **kwargs)
     45 async with client_context as new_client:
     46     kwargs.setdefault("client", new_client or client)
---> 47     return await fn(*args, **kwargs)

File ~/Documents/data/pdbt/.venv/lib/python3.8/site-packages/prefect/engine.py:226, in create_then_begin_flow_run(flow, parameters, return_type, client)
    224     return state
    225 elif return_type == "result":
--> 226     return await state.result(fetch=True)
    227 else:
    228     raise ValueError(f"Invalid return type for flow engine {return_type!r}.")

File ~/Documents/data/pdbt/.venv/lib/python3.8/site-packages/prefect/states.py:86, in _get_state_result(state, raise_on_failure)
     84         return await get_state_exception(state)
     85     else:
---> 86         raise MissingResult(
     87             "State data is missing. "
     88             "Typically, this occurs when result persistence is disabled and the "
     89             "state has been retrieved from the API."
     90         )
     92 else:
     93     # The result is attached directly
     94     result = state.data

MissingResult: State data is missing. Typically, this occurs when result persistence is disabled and the state has been retrieved from the API.

In [3]: