metaflow icon indicating copy to clipboard operation
metaflow copied to clipboard

feat: :sparkles: Change the Graph creation to allow subclassing

Open simonwardjones opened this issue 4 months ago • 2 comments

Currently it is not possible to subclass a Flow as in the example snippet below.

This PR alters the graph construction that enables this flow subclassing. Instead of using the ast.NodeVisitor class to collect the steps and their ast.FunctionDef's. I have instead iterated on the dir(flow) and then extracted the function ast.FunctionDef's.

from metaflow import FlowSpec, step


class BaseFlow(FlowSpec):
    @step
    def start(self):
        print("this is the start")
        self.next(self.step1)

    @step
    def step1(self):
        print("base step 1")
        self.next(self.end)

    @step
    def end(self):
        print("base step end.")


class SubFlow(BaseFlow):
    @step
    def step1(self):
        print("sub step 1")
        self.next(self.step2)

    @step
    def step2(self):
        print("sub step 2")
        self.next(self.end)


if __name__ == "__main__":
    SubFlow()

As referenced in this issue: https://github.com/Netflix/metaflow/issues/144 and discussed in this issue: https://github.com/Netflix/metaflow/issues/245

I have not yet added tests for this PR as the approach to this might take some discussion. The current Test harness setup uses the FlowFormatter class to generate flows on the fly but all of these assume the flow directly inherits off the FlowSpec. How might we go about testing this kind of subclassing?

Note this PR was originally raised as https://github.com/Netflix/metaflow/pull/2035 but from the wrong branch

simonwardjones avatar Oct 08 '24 11:10 simonwardjones