string_to_dict maps fields to unrelated regex capture groups
Problem
string_to_dict creates named groups for format fields, but builds the result by zipping field names with every regex capture. An unrelated capturing group shifts the values.
Reproduction
from datasets.utils.py_utils import string_to_dict
print(string_to_dict("train/alice", "(train|test)/{name}"))
# actual: {"name": "train"}; expected: {"name": "alice"}Candidate change
After the existing no-match check and pattern normalization, replace values = list(result.groups()) and dict(zip(keys, values)) with:
keys = re.findall(r"{(.+?)}", pattern)
_dict = {key: result.group(f"_{key}") for key in keys}This reads the named groups the helper already generates.
Validation
Current source src/datasets/utils/py_utils.py, blob df6d7b977b554cf772eeb32dcdf8dc5f0b0f2f4c, checked against the prepared reproduction. An isolated helper harness on Python 3.12.14 had 5 failed/5 passed before and 10 passed after the candidate change. Cases cover extra positional/named/nested regex groups, multiple format fields, numeric format specifications, noncapturing groups, and no match. This is helper-level validation, not a full installed-package or repository test run.
I searched existing issues and PRs for this helper. Related path-pattern changes such as #6322 do not address capture-group-to-field mapping.
Source: huggingface/datasets