https://prefect.io logo
Title
m

Maxwell Dylla

08/20/2020, 12:09 AM
Does anyone else in the community use type-hints to track data-dependencies in a Flow? Creating tasks with the
@flow
decorator works well for me, but the class-based API confuses my IDE. For example, my IDE thinks the class-based result should be type `Task`:
from prefect import Flow, Task, task


@task
def func_task() -> str:
    return ""


class ClassTask(Task):
    def run(self) -> str:
        return ""

class_task = ClassTask()


with Flow("test_type_hints") as flow:
    func_result: str = func_task()
    class_result: str = class_task()  # Expected type 'str', got 'Task' instead 
flow.run()
n

nicholas

08/20/2020, 12:22 AM
Hi @Maxwell Dylla - your type-checking is evaluating correctly but I can see where the confusion is. It boils down to type-checking happening at script runtime, when the instantiation of that task is correctly returning an instance of the class; what you're expecting is the runtime context resolved with
flow.run
, which would return the type
str
that you're expecting from the task's run method.
m

Maxwell Dylla

08/20/2020, 1:13 AM
@nicholas I'm really interested in how the type hints could interact with mypy. It would be a significant quality-of-life improvement for developing/debugging flows if I could type-check data-dependencies between tasks before running/registering flows.
(It doesn't work currently, even with the decorated-tasks, but would be neat)
n

nicholas

08/20/2020, 4:12 AM
@Maxwell Dylla - I can see how that would be useful! I'm not very familiar with mypy but I'm sure others would also benefit from strong typing; would you mind opening a feature request on the Prefect Core repo describing what you'd like to see? That'll let the Core team and others chime in on what sort of effort that would require.
m

Maxwell Dylla

08/20/2020, 3:56 PM
Absolutely - thanks, nicholas!