Skip to content

fix: Handle tuple and set types in HfArgumentParser - #48483

Open
dajiaohuang wants to merge 1 commit into
huggingface:mainfrom
dajiaohuang:fix/48419-hf-argparser-tuple-set
Open

fix: Handle tuple and set types in HfArgumentParser#48483
dajiaohuang wants to merge 1 commit into
huggingface:mainfrom
dajiaohuang:fix/48419-hf-argparser-tuple-set

Conversation

@dajiaohuang

@dajiaohuang dajiaohuang commented Sep 2, 2026

Copy link
Copy Markdown

CPU CI GPU run-slow

Description

HfArgumentParser was not handling tuple and set dataclass fields correctly. When parsing --layers 4,5 for a tuple[int, int] field, argparse would call tuple("4,5") which iterates character-by-character, producing ('4', ',', '5') instead of (4, 5).

The fix adds handling for tuple and set types similar to list:

  • tuple[int, int] (fixed-length): nargs=2, type=int
  • tuple[int, ...] (variable-length): nargs="+", type=int
  • set[str]: nargs="+", type=str

Example

from dataclasses import dataclass, field
from transformers import HfArgumentParser

@dataclass
class ModelArgs:
    layers: tuple[int, int] = (1, 2)
    tags: set[str] = field(default_factory=set)

parser = HfArgumentParser(ModelArgs)

# Before fix: layers=('4', ',', '5'), tags={'1', '2', ',', 'a', 'g', 't'}
# After fix: layers=(4, 5), tags={'tag1', 'tag2'}
args = parser.parse_args_into_dataclasses(["--layers", "4", "5", "--tags", "tag1", "tag2"])[0]

Related Issue

Fixes #48419

HfArgumentParser was not handling tuple and set dataclass fields correctly.
When parsing --layers 4,5 for a tuple[int, int] field, argparse would
call tuple("4,5") which iterates character-by-character, producing
('4', ',', '5') instead of (4, 5).

The fix adds handling for tuple and set types similar to list:
- For tuple[int, int] (fixed-length): nargs=2, type=int
- For tuple[int, ...] (variable-length): nargs="+", type=int
- For set[str]: nargs="+", type=str

Fixes huggingface#48419
@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 33615004147
Result: success | Grafana metrics are not available yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] HfArgumentParser silently corrupts tuple and set typed dataclass fields from CLI

1 participant