Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX url encoding in hf_hub_url #1164

Merged
merged 2 commits into from
Nov 7, 2022
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
2 changes: 1 addition & 1 deletion src/huggingface_hub/file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def hf_hub_url(
return HUGGINGFACE_CO_URL_TEMPLATE.format(
repo_id=repo_id,
revision=quote(revision, safe=""),
filename=filename,
filename=quote(filename),
)


Expand Down
43 changes: 43 additions & 0 deletions tests/test_file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,49 @@ def test_download_from_a_gated_repo_with_hf_hub_download(self):
)


@pytest.mark.usefixtures("fx_cache_dir")
class StagingCachedDownloadOnAwfulFilenamesTest(unittest.TestCase):
"""Implement regression tests for #1161.
Issue was on filename not url encoded by `hf_hub_download` and `hf_hub_url`.
See https://github.com/huggingface/huggingface_hub/issues/1161
"""

cache_dir: Path
repo_id = "valid_org/repo_with_awful_filename"
subfolder = "subfolder/to?"
filename = "awful?filename%you:should,never.give"
filepath = "subfolder/to?/awful?filename%you:should,never.give"
expected_url = "https://hub-ci.huggingface.co/valid_org/repo_with_awful_filename/resolve/main/subfolder/to%3F/awful%3Ffilename%25you%3Ashould%2Cnever.give"

def test_hf_hub_url_on_awful_filepath(self):
self.assertEqual(hf_hub_url(self.repo_id, self.filepath), self.expected_url)

def test_hf_hub_url_on_awful_subfolder_and_filename(self):
self.assertEqual(
hf_hub_url(self.repo_id, self.filename, subfolder=self.subfolder),
self.expected_url,
)

def test_hf_hub_download_on_awful_filepath(self):
local_path = hf_hub_download(
self.repo_id, self.filepath, cache_dir=self.cache_dir
)
# Local path is not url-encoded
self.assertTrue(local_path.endswith(self.filepath))

def test_hf_hub_download_on_awful_subfolder_and_filename(self):
local_path = hf_hub_download(
self.repo_id,
self.filename,
subfolder=self.subfolder,
cache_dir=self.cache_dir,
)
# Local path is not url-encoded
self.assertTrue(local_path.endswith(self.filepath))


class CreateSymlinkTest(unittest.TestCase):
@patch("huggingface_hub.file_download.are_symlinks_supported")
def test_create_relative_symlink_concurrent_access(
Expand Down