Skip to content

Commit

Permalink
FIX url encoding in hf_hub_url (#1164)
Browse files Browse the repository at this point in the history
* FIX url encoding in hf_hub_url

* tests on subfolders as well
  • Loading branch information
Wauplin authored Nov 7, 2022
1 parent 9ccdf02 commit b0e441e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
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

0 comments on commit b0e441e

Please sign in to comment.