From 787e8a73a8d207357a462647b369139283ebeb70 Mon Sep 17 00:00:00 2001 From: Ad1th <90973387+Ad1th@users.noreply.github.com> Date: Fri, 2 Oct 2026 17:08:36 +0530 Subject: [PATCH 1/3] [Download] Warn when allow/ignore patterns match no files in snapshot_download --- src/huggingface_hub/_snapshot_download.py | 7 ++++ tests/test_snapshot_download.py | 47 +++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/huggingface_hub/_snapshot_download.py b/src/huggingface_hub/_snapshot_download.py index 42288c07870..df5064d090d 100644 --- a/src/huggingface_hub/_snapshot_download.py +++ b/src/huggingface_hub/_snapshot_download.py @@ -413,6 +413,13 @@ def snapshot_download( ignore_patterns=ignore_patterns, ) ) + if tree_entries and not filtered_repo_files and (allow_patterns is not None or ignore_patterns is not None): + provided_patterns = ", ".join( + f"{name}={value!r}" + for name, value in (("allow_patterns", allow_patterns), ("ignore_patterns", ignore_patterns)) + if value is not None + ) + logger.warning(f"No files matched {provided_patterns} (repo has {len(tree_entries)} files).") tqdm_desc = f"Fetching {len(filtered_repo_files)} files" if dry_run: tqdm_desc = "[dry-run] " + tqdm_desc diff --git a/tests/test_snapshot_download.py b/tests/test_snapshot_download.py index dd0ea59f789..7961dac6815 100644 --- a/tests/test_snapshot_download.py +++ b/tests/test_snapshot_download.py @@ -46,6 +46,53 @@ def test_tree_with_redacted_xet_hash_is_not_cached(tmp_path: Path): assert read_tree_cache(str(storage_folder), COMMIT_HASH) is None +def _mock_repo_tree(*paths: str) -> list[RepoFile]: + return [RepoFile(path=path, size=42, oid=f"blob-{i}") for i, path in enumerate(paths)] + + +@pytest.mark.parametrize( + ("kwargs", "expect_warning"), + [ + ({"allow_patterns": ["*.safetensor"]}, True), + ({"allow_patterns": "*.safetensor"}, True), + ({"ignore_patterns": ["*"]}, True), + ({"allow_patterns": ["*.json"], "ignore_patterns": ["*.json"]}, True), + ({"allow_patterns": ["*.json"]}, False), + ({"ignore_patterns": ["*.bin"]}, False), + ({}, False), + ], +) +def test_warn_when_patterns_match_no_files(tmp_path: Path, caplog, kwargs: dict, expect_warning: bool): + tree = _mock_repo_tree("config.json", "model.safetensors") + + with ( + patch("huggingface_hub._snapshot_download.HfApi.repo_info", return_value=MagicMock(sha=COMMIT_HASH)), + patch("huggingface_hub._snapshot_download.HfApi.list_repo_tree", return_value=tree), + patch("huggingface_hub._snapshot_download.hf_thread_map"), + caplog.at_level("WARNING", logger="huggingface_hub"), + ): + snapshot_download("user/repo", cache_dir=tmp_path, **kwargs) + + messages = [record.message for record in caplog.records if "No files matched" in record.message] + if expect_warning: + assert len(messages) == 1 + assert "(repo has 2 files)" in messages[0] + else: + assert messages == [] + + +def test_no_warning_when_repo_is_empty(tmp_path: Path, caplog): + with ( + patch("huggingface_hub._snapshot_download.HfApi.repo_info", return_value=MagicMock(sha=COMMIT_HASH)), + patch("huggingface_hub._snapshot_download.HfApi.list_repo_tree", return_value=[]), + patch("huggingface_hub._snapshot_download.hf_thread_map"), + caplog.at_level("WARNING", logger="huggingface_hub"), + ): + snapshot_download("user/repo", cache_dir=tmp_path, allow_patterns=["*.safetensors"]) + + assert not [record for record in caplog.records if "No files matched" in record.message] + + class TestSnapshotDownload: @pytest.fixture(scope="class", autouse=True) def _shared_repo(self, request, api: HfApi): From 0f0b380c2f8dbf1285b6f19e7166dde415d112b2 Mon Sep 17 00:00:00 2001 From: Lucain Date: Fri, 2 Oct 2026 13:57:56 +0200 Subject: [PATCH 2/3] Apply suggestion from @Wauplin --- tests/test_snapshot_download.py | 47 --------------------------------- 1 file changed, 47 deletions(-) diff --git a/tests/test_snapshot_download.py b/tests/test_snapshot_download.py index 7961dac6815..dd0ea59f789 100644 --- a/tests/test_snapshot_download.py +++ b/tests/test_snapshot_download.py @@ -46,53 +46,6 @@ def test_tree_with_redacted_xet_hash_is_not_cached(tmp_path: Path): assert read_tree_cache(str(storage_folder), COMMIT_HASH) is None -def _mock_repo_tree(*paths: str) -> list[RepoFile]: - return [RepoFile(path=path, size=42, oid=f"blob-{i}") for i, path in enumerate(paths)] - - -@pytest.mark.parametrize( - ("kwargs", "expect_warning"), - [ - ({"allow_patterns": ["*.safetensor"]}, True), - ({"allow_patterns": "*.safetensor"}, True), - ({"ignore_patterns": ["*"]}, True), - ({"allow_patterns": ["*.json"], "ignore_patterns": ["*.json"]}, True), - ({"allow_patterns": ["*.json"]}, False), - ({"ignore_patterns": ["*.bin"]}, False), - ({}, False), - ], -) -def test_warn_when_patterns_match_no_files(tmp_path: Path, caplog, kwargs: dict, expect_warning: bool): - tree = _mock_repo_tree("config.json", "model.safetensors") - - with ( - patch("huggingface_hub._snapshot_download.HfApi.repo_info", return_value=MagicMock(sha=COMMIT_HASH)), - patch("huggingface_hub._snapshot_download.HfApi.list_repo_tree", return_value=tree), - patch("huggingface_hub._snapshot_download.hf_thread_map"), - caplog.at_level("WARNING", logger="huggingface_hub"), - ): - snapshot_download("user/repo", cache_dir=tmp_path, **kwargs) - - messages = [record.message for record in caplog.records if "No files matched" in record.message] - if expect_warning: - assert len(messages) == 1 - assert "(repo has 2 files)" in messages[0] - else: - assert messages == [] - - -def test_no_warning_when_repo_is_empty(tmp_path: Path, caplog): - with ( - patch("huggingface_hub._snapshot_download.HfApi.repo_info", return_value=MagicMock(sha=COMMIT_HASH)), - patch("huggingface_hub._snapshot_download.HfApi.list_repo_tree", return_value=[]), - patch("huggingface_hub._snapshot_download.hf_thread_map"), - caplog.at_level("WARNING", logger="huggingface_hub"), - ): - snapshot_download("user/repo", cache_dir=tmp_path, allow_patterns=["*.safetensors"]) - - assert not [record for record in caplog.records if "No files matched" in record.message] - - class TestSnapshotDownload: @pytest.fixture(scope="class", autouse=True) def _shared_repo(self, request, api: HfApi): From 7fda4f3e7920d75eaa9d34afa41bdb3905897a81 Mon Sep 17 00:00:00 2001 From: Lucain Date: Fri, 2 Oct 2026 13:58:58 +0200 Subject: [PATCH 3/3] Apply suggestion from @Wauplin --- src/huggingface_hub/_snapshot_download.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/huggingface_hub/_snapshot_download.py b/src/huggingface_hub/_snapshot_download.py index df5064d090d..8b6aa8cd17b 100644 --- a/src/huggingface_hub/_snapshot_download.py +++ b/src/huggingface_hub/_snapshot_download.py @@ -419,7 +419,7 @@ def snapshot_download( for name, value in (("allow_patterns", allow_patterns), ("ignore_patterns", ignore_patterns)) if value is not None ) - logger.warning(f"No files matched {provided_patterns} (repo has {len(tree_entries)} files).") + logger.warning(f"No files matched {provided_patterns}.") tqdm_desc = f"Fetching {len(filtered_repo_files)} files" if dry_run: tqdm_desc = "[dry-run] " + tqdm_desc