Skip to content

How to add custom parsing #332

@Conchylicultor

Description

@Conchylicultor

I have a code like:

@dataclasses.dataclass
class Args:
  model_cls: type[Model] = Transformer

I would like to support this through CLI. I have a mapping str -> type[Model], such as the user can pass a string --model_cls=Transformer that I can then normalize to the model class.

Something like:

@dataclasses.dataclass
class Args:
  model_cls: type[Model] = field(default=Transformer, parsing_type=str)

  def __post_init__(self):
    if isinstance(self.model_cls, str):
      self.model_cls = _NAME_TO_CLS[self.model]

However I do not know how to make simple_parsing parse model_cls as a str.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions