Skip to content

Optional class instances #624

@hadipash

Description

@hadipash

Hello! I would like to ask what is a good way to handle optional class instances? For example, instantiating a validation dataset. In the original Python code, it would look like this:

if args.val_dataset is not None:
    # instantiate the validation set class

However, add_class_arguments() doesn't support optional instances. To work around this, I'm currently using add_subclass_arguments(required=False), but I find this approach suboptimal. Is there a better way to handle situations like this in jsonargparse? Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions