INNER CODE UNIT · Python

make_parser

allenai/dolma · python/dolma/cli/__init__.py:70

def make_parser(parser: A, config: Type[DataClass], prefix: Optional[str] = None) -> A:
    for field_name, dt_field in config.__dataclass_fields__.items():
        # get type from annotations or metadata
        typ_ = config.__annotations__.get(field_name, dt_field.metadata.get("type", MISSING))

        if typ_ is MISSING:
            warning(f"No type annotation for field {field_name} in {config.__name__}")
            continue

        # join prefix and field name
        field_name = f"{prefix}.{field_name}" if prefix else field_name

        # This section here is to handle Optional[T] types; we only care for cases where T is a dataclass
        # So we first check if type is Union since Optional[T] is just a shorthand for Union[T, None]
        # and that the union contains only one non-None type
        if get_origin(typ_) == Union:
            # get all non-None types
            args = [a for a in get_args(typ_) if a is not type(None)]  # noqa: E721

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…