Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions swift/dataset/dataset_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ def _safe_split(s: str,
@classmethod
def parse(cls, dataset: str) -> 'DatasetSyntax':
"""Parse the dataset from the command line"""
if dataset:
dataset = dataset.strip()
if not dataset:
raise ValueError('Received an empty dataset entry. Check `--dataset`/`--val_dataset` for a stray empty '
'or whitespace-only string (e.g. an unset shell variable wrapped in quotes).')
# hf/ms::dataset_id or dataset_path:subset1/subset2/subset3#dataset_sample
if os.path.exists(dataset):
use_hf = None
Expand Down
14 changes: 14 additions & 0 deletions tests/general/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ def test_cls():
_test_dataset(['simpleai/HC3-Chinese:baike_cls'])


def test_dataset_syntax_rejects_empty_entry():
import pytest

from swift.dataset.dataset_syntax import DatasetSyntax

# An empty or whitespace-only dataset entry (e.g. an unset shell variable
# wrapped in quotes in a `--dataset "$DS1" "$DS2"` invocation) must raise a
# clear ValueError instead of crashing deep inside with a cryptic
# `os.path.exists(None)` TypeError.
for entry in ('', ' ', '\t', '\n'):
with pytest.raises(ValueError, match='empty dataset entry'):
DatasetSyntax.parse(entry)


if __name__ == '__main__':
# test_sft()
# test_agent()
Expand Down
Loading