torchx icon indicating copy to clipboard operation
torchx copied to clipboard

support dataclasses for component args

Open daniel-ohayon opened this issue 8 months ago • 1 comments

Summary: This change adds the ability to provide component arguments via a single dataclass (which can still be combined with positional varargs). One motivation for supporting dataclasses is to facilitate composition of components, eg consider a scenario where component2 is an extension of component1 – it takes the same args as component1 + some extra ones, calls component1 and does some extra work on top with the additional arguments.

Before

def component1(arg1: str, arg2: int, ...argN: str): ...

# need to repeat all arguments, unclear which ones are specific to component2
def component2(arg1: str, arg2: int, ... argN: str, otherArg: str):
    # need to pass all component1-specific args to component1 explicitly
   app_def = component1(arg1, arg2, ... argN)
   return do_something(app_def, otherArg)

After

dataclass
class Comp1Args:
   arg1: str
   arg2: int
   ...
   argN: str

# separation of args for the 2 components is explicit via the dataclasses
# no need to spell out all the args thanks to inheritance
dataclass
class Comp2Args(Comp1Args):
   other_arg: str

def component1(args: Comp1Args): ...

def component2(args: Comp2Args): 
     # no need to spell out all the args when calling component1
    app_def = component1(args)
    return do_something(app_def, other_arg)

Differential Revision: D75320316

daniel-ohayon avatar May 27 '25 14:05 daniel-ohayon

This pull request was exported from Phabricator. Differential Revision: D75320316

facebook-github-bot avatar May 27 '25 14:05 facebook-github-bot