Closed onlyann closed 1 month ago
Click to see where and how coverage changed
File Statements Missing Coverage Coverage
(new stmts)Lines missing
procrastinate
blueprints.py
periodic.py
tasks.py
Project Total
This report was generated by python-coverage-comment-action
Closes #1056
Successful PR Checklist:
PR label(s):
This improves typing of the
@App.task
and@App.periodic
decorators.basic task
The
@task
decorator now returns a genericTask
that defines thefunc
anddefer
parameters.For example, given the task below:
sum
is:[(a: int, b: int)
describes the parameters of the wrapped func while(a: int, b: int)
describes the parameters of thedefer_async
anddefer
functions.⚠️ The Task signature doesn't enforce keyword arguments. Specifying positional arguments when calling
defer
will likely fail but won't be caught by type check. One way to improve type safety is to prevent positional arguments on the decorated function .e.g.async def sum(*, a: int, b: int)
task with context
Another example is the use of the
passContext
option:sum
is:This is because
JobContext
is injected by the worker but should not be part of thedefer
arguments. The type annotation also correctly complains if the decorated function is missing a first parameter of typeJobContext
.Periodic task
Yet another example is the periodic decorator:
cleanup
isThis is because
timestamp
is injected by the decorator but should not be part of thedefer
arguments.⚠️
timestamp
is actually set as a keyword parameter while the type annotation enforces a positional parameter. Unfortunately, concatenating keywords parameters is not supported.Examples demonstrating the limitations:
Other changes
Options supported by both the
task
andperiodic
decorators are fully defined.Other comments
The
task_kwargs
keyword parameter fromTask.configure
remains of typeJSONDict
. I am unsure if this can be accurately typed.I initially added another generic parameter for the task return type but eventually removed it as I didn't see much value.
I added type hints in some of the test files to demonstrate the type annotations. That said, given
pyright
is configured to ignore thetests
folder, breaking type hints won't be enforced during CI. Including type check for alltests
would have made this pull request much larger, if that is even something desirable. Maybe there is a middle ground where some test files are included. I lean on you to suggest the best course of action here.