Danny Vilela
02/12/2021, 7:20 PM@task
decorator is lacking a positional argument. For example:
@task
def add_one(x: int) -> int:
return x + 1
@task()
def add_two(x: int) -> int:
return x + 2
The @task
decorator over add_one
is how I’ve seen it in the docs, but it seems like the decorator for add_two
is how PyCharm likes it. Am I missing something?Jim Crist-Harif
02/12/2021, 7:24 PM()
. Use whichever one you want.Danny Vilela
02/12/2021, 7:27 PMfrom dataclasses import dataclass
@dataclass
class PointA:
x: float
y: float
@dataclass()
class PointB:
x: float
y: float
No worries though. Thanks @Jim Crist-Harif!Jim Crist-Harif
02/12/2021, 7:28 PMDanny Vilela
02/12/2021, 7:33 PM@dataclass
decorator and tried adopting it for @task
. This seems to work, and doesn’t look too different from the current @task
.
def task(
fn: Callable = None, **task_init_kwargs: Any
) -> Union[
"prefect.tasks.core.function.FunctionTask",
Callable[[Callable], "prefect.tasks.core.function.FunctionTask"],
]:
def wrap(fn):
return prefect.tasks.core.function.FunctionTask(
fn=fn,
**task_init_kwargs,
)
# See if we're being called as @task or @task().
if fn is None:
# We're called with parens.
return wrap
# We're called as @task without parens.
return wrap(fn)
And no more complaints from PyCharm on either @task
or @task()
! Happy to test it some more and/or open a PR, if it’s of interest.def task(
fn: Callable = None, **task_init_kwargs: Any
) -> Union[
"prefect.tasks.core.function.FunctionTask",
Callable[[Callable], "prefect.tasks.core.function.FunctionTask"],
]:
def wrap(fn):
if fn is None:
return lambda void_fn: prefect.tasks.core.function.FunctionTask(
fn=void_fn,
**task_init_kwargs,
)
return prefect.tasks.core.function.FunctionTask(
fn=fn,
**task_init_kwargs,
)
# See if we're being called as @task or @task().
if fn is None:
# We're called with parens.
return wrap
# We're called as @task without parens.
return wrap(fn)
Jim Crist-Harif
02/12/2021, 7:37 PM