Skip to content
Merged
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
13 changes: 7 additions & 6 deletions src/huggingface_hub/utils/_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""Contains utilities to handle paths in Huggingface Hub."""

from collections.abc import Callable, Generator, Iterable
from fnmatch import fnmatch
from fnmatch import fnmatchcase
from pathlib import Path
from typing import TypeVar

Expand Down Expand Up @@ -50,8 +50,9 @@ def filter_repo_objects(
that is used to extract a path from each element in iterable.

Patterns are Standard Wildcards (globbing patterns), NOT regular expressions.
The pattern matching is based on Python's `fnmatch`. Note that `fnmatch` matches
`*` across path boundaries, unlike traditional Unix shell globbing. For example,
The pattern matching is based on Python's `fnmatch.fnmatchcase`, so it is
case-sensitive on every platform. Note that it matches `*` across path
boundaries, unlike traditional Unix shell globbing. For example,
`"data/*.json"` will match both `data/file.json` and `data/subdir/file.json`.
See https://docs.python.org/3/library/fnmatch.html for more details.

Expand Down Expand Up @@ -79,7 +80,7 @@ def filter_repo_objects(
```python
>>> # Filter only PDFs that are not hidden.
>>> list(filter_repo_objects(
... ["aaa.PDF", "bbb.jpg", ".ccc.pdf", ".ddd.png"],
... ["aaa.pdf", "bbb.jpg", ".ccc.pdf", ".ddd.png"],
... allow_patterns=["*.pdf"],
... ignore_patterns=[".*"],
... ))
Expand Down Expand Up @@ -128,11 +129,11 @@ def _identity(item: T) -> str:
path = key(item)

# Skip if there's an allowlist and path doesn't match any
if allow_patterns is not None and not any(fnmatch(path, r) for r in allow_patterns):
if allow_patterns is not None and not any(fnmatchcase(path, r) for r in allow_patterns):
continue

# Skip if there's a denylist and path matches any
if ignore_patterns is not None and any(fnmatch(path, r) for r in ignore_patterns):
if ignore_patterns is not None and any(fnmatchcase(path, r) for r in ignore_patterns):
continue

yield item
Expand Down
23 changes: 23 additions & 0 deletions tests/test_utils_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,29 @@ def test_filter_object_with_empty_pattern(self) -> None:
ignore_patterns=[""],
)

def test_filter_is_case_sensitive(self) -> None:
"""Pattern matching is case-sensitive.
Regression test for https://github.com/huggingface/huggingface_hub/issues/4434.
"""
# Uppercase pattern matches only the uppercase path, not the lowercase one.
self._check(
items=["README.md", "notes.MD"],
expected_items=["notes.MD"],
allow_patterns=["*.MD"],
)
# Lowercase pattern matches only the lowercase path, not the uppercase one.
self._check(
items=["README.md", "notes.MD"],
expected_items=["README.md"],
allow_patterns=["*.md"],
)
# Case-sensitivity also applies to the ignore list.
self._check(
items=["keep.txt", "drop.LOG", "keep.log"],
expected_items=["keep.txt", "keep.log"],
ignore_patterns=["*.LOG"],
)

def _check(
self,
items: list[Any],
Expand Down
Loading