Hi Prefect, I started seeing the following typing...
# ask-community
p
Hi Prefect, I started seeing the following typing errors when checking my code with `mypy`:
Copy code
error: Signature of "run" incompatible with supertype "Task"
For example for following Task:
Copy code
class RestTaskTest(prefect.Task):
    def run(self, string_val: str, int_val: int) -> TaskTestResult:
        return TaskTestResult(string_val=string_val, int_val=int_val)
This makes sense since the definition of
Task.run
is
def run(self) -> None:
. Is there any way to make mypy work together with overriding of Task.run in custom tasks? Or is the only option to ignore these with
# type: ignore
?
I'm using mypy version 0.901 and prefect version 0.14.21
k
Hey again , could you open an enhancement request, potentially to change the annotation for Task.run to
Any
?
p
You are right, the return type should be more general by being
Any
. However, I don't think its only the Any. I tried changing that in prefect, and the error is still there. There are also the Task method arguments that are overwritten
self
is overwritten by
self, string_val: str, int_val: int
in the previous example. And I don't know how to correctly type this myself.
z
if you decorate run with @defaults_from_attrs it’ll make mypy happy, thats how i got around that
Copy code
from prefect.utilities.tasks import defaults_from_attrs
pretty useful decorator anyway and its the pattern most of the built in tasks follow
p
Thanks @Zach Schumacher, using
@defaults_from_attrs()
on top of the overridden
run()
method works. Thanks!
👍 1