Charles Leung
05/31/2024, 5:55 PMasyncio.run( earnings_demo_03_2024(
env="qdevd",
injected_code=".wf.set[`INITIAL_CACHE_POSTED_77]; .wf.setS[`FUNDAMENTAL_EXTRACTED]; .wf.set[`IBES_Cycle6_Loaded_77];",
)
)
MyPy Returns an error Argument 1 to "run" has incompatible type "None"; expected "Coroutine[Any, Any, <nothing>]"Mypyarg-type
The function still runs properly but wondering why this error exists?Marvin
05/31/2024, 5:56 PMasyncio.run
in MyPy is a result of asyncio.run
expecting a coroutine, but possibly receiving an Awaitable
that resolves to None
. This seems to be a known issue.
### Explanation
In Python, asyncio.run
expects an argument that is of type Coroutine
, but sometimes you might accidentally pass an Awaitable
that doesn't exactly fit this type, causing MyPy to complain even though the actual runtime execution might work.
Here's a summary of the scenario with a similar problem reported:
- A user defined an asynchronous flow in Prefect (conforming to the example format in Prefect's issue 6054).
- Encountered MyPy's argument type incompatibility error, even though the flow runs without issues.
### Potential Solution
Ensure that the function you are passing to asyncio.run
is indeed a coroutine. Here's an illustrative example:
python
import asyncio
from prefect import flow
@flow
async def earnings_demo_03_2024(env, injected_code):
# your async code
pass
# Ensure the function call is awaited correctly
asyncio.run(earnings_demo_03_2024(
env="qdevd",
injected_code=".wf.set[`INITIAL_CACHE_POSTED_77]; .wf.setS[`FUNDAMENTAL_EXTRACTED]; .wf.set[`IBES_Cycle6_Loaded_77];"
))
### Referenced Sources
- Issue describing a similar problem
- Python asyncio.run documentation
If the issue persists, consider verifying the awaited function or checking for type inconsistencies specifically related to the type annotations in your function signature.Bring your towel and join one of the fastest growing data communities. Welcome to our second-generation open source orchestration platform, a completely rethought approach to dataflow automation.
Powered by