diff --git a/src/huggingface_hub/repocard_data.py b/src/huggingface_hub/repocard_data.py index c5b85912ceb..d8ec8d33bb4 100644 --- a/src/huggingface_hub/repocard_data.py +++ b/src/huggingface_hub/repocard_data.py @@ -614,6 +614,9 @@ def model_index_to_eval_results(model_index: list[dict[str, Any]]) -> tuple[str, ``` """ + if not model_index: + raise ValueError("`model_index` must be a non-empty list.") + eval_results = [] for elem in model_index: name = elem["name"] diff --git a/src/huggingface_hub/serialization/_torch.py b/src/huggingface_hub/serialization/_torch.py index 9591493f9f2..d77754a36f7 100644 --- a/src/huggingface_hub/serialization/_torch.py +++ b/src/huggingface_hub/serialization/_torch.py @@ -425,7 +425,7 @@ def load_torch_model( [`ImportError`](https://docs.python.org/3/library/exceptions.html#ImportError) If safetensors or torch is not installed when trying to load a .safetensors file or a PyTorch checkpoint respectively. [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError) - If the checkpoint path is invalid or if the checkpoint format cannot be determined. + If the checkpoint format cannot be determined. Example: ```python @@ -437,7 +437,7 @@ def load_torch_model( checkpoint_path = Path(checkpoint_path) if not checkpoint_path.exists(): - raise ValueError(f"Checkpoint path {checkpoint_path} does not exist") + raise FileNotFoundError(f"Checkpoint path {checkpoint_path} does not exist") # 1. Check if checkpoint is a single file if checkpoint_path.is_file(): state_dict = load_state_dict_from_file( diff --git a/tests/test_repocard_data.py b/tests/test_repocard_data.py index 51527d95dc7..d1100a937a9 100644 --- a/tests/test_repocard_data.py +++ b/tests/test_repocard_data.py @@ -200,6 +200,10 @@ def test_model_index_to_eval_results_source_null(self): assert eval_results[0].source_name is None assert eval_results[0].source_url is None + def test_model_index_to_eval_results_empty(self): + with pytest.raises(ValueError, match="`model_index` must be a non-empty list."): + model_index_to_eval_results([]) + def test_card_data_requires_model_name_for_eval_results(self): with pytest.raises(ValueError, match="`eval_results` requires `model_name` to be set."): diff --git a/tests/test_serialization.py b/tests/test_serialization.py index e35ac639e7a..8b77936e3c1 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -714,7 +714,7 @@ def test_load_state_dict_missing_file(safe_serialization): def test_load_torch_model_directory_does_not_exist(): """Test proper error handling when directory does not contain a valid checkpoint.""" - with pytest.raises(ValueError, match="Checkpoint path does_not_exist does not exist"): + with pytest.raises(FileNotFoundError, match="Checkpoint path does_not_exist does not exist"): load_torch_model(Mock(), "does_not_exist")