Hi all! I’m trying to use Prefect’s class-based ta...
# prefect-community
d
Hi all! I’m trying to use Prefect’s class-based tasks, and struggling to understand how to best make this work with my IDE’s function/variable annotations. For example, if I have this dummy flow:
Copy code
from prefect import Flow
from prefect import Task


class AddTask(Task):
    def run(self, x: int, y: int) -> int:
        return x + y


with Flow(name="adding") as flow:
    adder: AddTask = AddTask()
    a: int = adder(x=1, y=2)
    b: int = adder(x=a, y=3)
    print(a, b)

flow.run()
It runs just fine, but I get two different “warning” highlights from my IDE (PyCharm): 1. On the
def run(self, x: int, y: int) -> int
line:
Signature of method 'AddTask.run()' does not match signature of base method in class 'Task'
2. On both assignment lines in the flow:
Expected type 'int', got 'AddTask' instead
I’d like to know what I’m doing that’s un-Prefect-like. Does Prefect work well with type annotations? I’m aware that, really, the
x
and
y
parameters to
AddTask
are actually converted to parameters (or a constant task?). But that’s maybe not as clear as annotating them as integers.
z
Hi @Danny Vilela -- to get the IDE to behave takes a lot of hacky signature updates (something like Pydantic does this) and I don't think we can easily explain to your IDE that this is the intent. I think that type-hints with Prefect are mostly for readability not enforcement with a type-checking system.
d
@Zanie Does that also apply to each task’s
run
method? It’s no real problem to silence mypy/the IDE on those lines (e.g.,
# noqa
), but just making sure 🙂
z
What do you mean?
d
Sorry, re: my first point:
Signature of method 'AddTask.run()' does not match signature of base method in class 'Task'
. For the second point you can just set
b: int = adder(x=a, y=2)  # noqa
. For the first, you’d have to do
def run(self, x: int, y: int) -> int:  # noqa
. Just checking that my understanding of how/when you need to silence the type checker is correct!
z
Yeah because the base signature is
Copy code
def run(self) -> None:
you'll get a mismatch error there. I'm not sure why we don't have it take
Any
and return
Any
but even then I think that pycharm may complain.
🙌 1
d
Sounds good! Thanks for being so responsive @Zanie 🙏
z
You're welcome! If you want to open an issue for this, I imagine it's something we could consider better support for in the future but as of now it's just sugar.