Skip to content

Question on how to Instantiate a list of objects with multiple configs #455

@ryokan0123

Description

@ryokan0123

Hi, I find jsonargparse incredibly useful for organizing code in ML experiments.
Thank you for creating such an excellent project.

I frequently encounter a use case where I need to pass a list of objects, each with separate config files.
This would allow me to experiment with combinations of complex objects.
However, I'm unsure if this is possible with the current version of jsonargparse.

For a minimal example, I'm looking to implement something like the following:

from dataclasses import dataclass
from typing import Any

import jsonargparse

@dataclass
class Foo:
    arg1: Any

@dataclass
class Bar:
    arg1: Any

@dataclass
class ComplexClass:
    foo: Foo
    bar: Bar


if __name__ == "__main__":
    parser = jsonargparse.ArgumentParser()
    parser.add_argument("--class_list", type=list[ComplexClass])

    args = parser.parse_args()
    args = parser.instantiate_classes(args)
    print(args)

And each ComplexClass would be configured through a separate config file like this:

foo:
  class_path: Foo
  init_args:
    arg: 1
bar:
  class_path: Bar
  init_args:
    arg: 2

Then the command would be:

python example.py --class_list+="complex_class.yaml"  --class_list+="complex_class2.yaml

Is there a way to achieve something like this?
I understand that configuring through a single config file is possible, possibly by using jsonnet to import multiple config files, but being able to directly specify multiple config files through command-line arguments would be convenient.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions