arq icon indicating copy to clipboard operation
arq copied to clipboard

Type hints for cron_jobs

Open ogenstad opened this issue 3 years ago • 3 comments

The documentation for cron jobs in docs/examples/cron.py use a coroutine as the argument to cron(), though that function expects coroutine to be a string or something that satisfies the WorkerCoroutine protocol.

Because of this the current example gives an error in with mypy.

> mypy docs/examples/cron.py                                                                                                                                                      
docs/examples/cron.py:10: error: Argument 1 to "cron" has incompatible type "Callable[[Any], Coroutine[Any, Any, None]]"; expected "Union[str, WorkerCoroutine]"

The cron function also has a check to verify that the value is a coroutine so specifying a class that matches to WorkerCoroutine doesn't seem to work: assert asyncio.iscoroutinefunction(coroutine_), f'{coroutine_} is not a coroutine function'

Was the original idea that this should have worked in a different way? Should the type hints in cron be changed to accept either a string or a coroutine?

For now I've just added a comment to ignore type checking for that line in my code.

ogenstad avatar May 17 '21 13:05 ogenstad

Been facing the same issue, worked around it by passing it as a string (package_name.function_name)

@samuelcolvin any reason it doesn't expect a coroutine directly?

akhilnarang avatar Aug 15 '22 07:08 akhilnarang

Same issue. This cannot be solved simply by passing the function name as a string.

FyZzyss avatar Oct 14 '22 09:10 FyZzyss

Same issue. This cannot be solved simply by passing the function name as a string.

@FyZzyss works for me like this: https://github.com/akhilnarang/vlrgg-scraper/blob/master/app/cron.py#L143-L147

akhilnarang avatar Oct 14 '22 13:10 akhilnarang