Skip to content

HfArgumentParser should parse hyphens in long-option parameters #33933

Closed
@djmarti

Description

@djmarti

Feature request

Given a dataclass

from dataclasses import dataclass

@dataclass
class Config:
    item_type: str
    item_length: int

I would expect HfArgumentParser.parse_args_into_dataclasses to correctly parse parameters like --item-type or --item-length (with a hyphen instead of an underscore). Unfortunately, it doesn't:

from transformers import HfArgumentParser
parser = HfArgumentParser([Config])
parser.parse_args_into_dataclasses(args=["--item-type", "my_type", "--item-length", "23"])

throws an error because it is expecting --item_type and --item_length.

Motivation

  1. Consistency with argparse. argparse converts hyphens into underscores before assignment:

    If no long option strings were supplied, dest will be derived from the first short option string by stripping the initial - character. Any internal - characters will be converted to _ characters to make sure the string is a valid attribute name.

  2. It is standard convention to use hyphens in long-option parameters (e.g., --file-type, not --file_type).

Your contribution

If there is no reason preventing this feature request to be accepted, yes, I could contribute to a PR.
I just want to know if there is a fundamental reason why this is not implemented yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions