INNER CODE UNIT · Python
DataClass
allenai/dolma · python/dolma/cli/__init__.py:66
class DataClass(Protocol):
__dataclass_fields__: Dict[str, Field]
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]