Skip to content

Commit 7709178

Browse files
authored
Fix download local dir edge case (remove lru_cache) (#2629)
1 parent dff7dbf commit 7709178

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

src/huggingface_hub/_local_folder.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import os
5454
import time
5555
from dataclasses import dataclass
56-
from functools import lru_cache
5756
from pathlib import Path
5857
from typing import Optional
5958

@@ -179,7 +178,6 @@ def save(self, paths: LocalUploadFilePaths) -> None:
179178
self.timestamp = new_timestamp
180179

181180

182-
@lru_cache(maxsize=128) # ensure singleton
183181
def get_local_download_paths(local_dir: Path, filename: str) -> LocalDownloadFilePaths:
184182
"""Compute paths to the files related to a download process.
185183
@@ -220,7 +218,6 @@ def get_local_download_paths(local_dir: Path, filename: str) -> LocalDownloadFil
220218
return LocalDownloadFilePaths(file_path=file_path, lock_path=lock_path, metadata_path=metadata_path)
221219

222220

223-
@lru_cache(maxsize=128) # ensure singleton
224221
def get_local_upload_paths(local_dir: Path, filename: str) -> LocalUploadFilePaths:
225222
"""Compute paths to the files related to an upload process.
226223
@@ -404,7 +401,6 @@ def write_download_metadata(local_dir: Path, filename: str, commit_hash: str, et
404401
f.write(f"{commit_hash}\n{etag}\n{time.time()}\n")
405402

406403

407-
@lru_cache()
408404
def _huggingface_dir(local_dir: Path) -> Path:
409405
"""Return the path to the `.cache/huggingface` directory in a local directory."""
410406
# Wrap in lru_cache to avoid overwriting the .gitignore file if called multiple times

tests/test_local_folder.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,17 @@ def test_local_download_paths(tmp_path: Path):
7474
assert paths.incomplete_path("etag123").parent.is_dir()
7575

7676

77-
def test_local_download_paths_are_cached(tmp_path: Path):
78-
"""Test local download paths are cached."""
79-
# No need for an exact singleton here.
80-
# We just want to avoid recreating the dataclass on consecutive calls (happens often
81-
# in the process).
77+
def test_local_download_paths_are_recreated_each_time(tmp_path: Path):
8278
paths1 = get_local_download_paths(tmp_path, "path/in/repo.txt")
79+
assert paths1.file_path.parent.is_dir()
80+
assert paths1.metadata_path.parent.is_dir()
81+
82+
paths1.file_path.parent.rmdir()
83+
paths1.metadata_path.parent.rmdir()
84+
8385
paths2 = get_local_download_paths(tmp_path, "path/in/repo.txt")
84-
assert paths1 is paths2
86+
assert paths2.file_path.parent.is_dir()
87+
assert paths2.metadata_path.parent.is_dir()
8588

8689

8790
@pytest.mark.skipif(os.name != "nt", reason="Windows-specific test.")
@@ -198,14 +201,17 @@ def test_local_upload_paths(tmp_path: Path):
198201
assert paths.lock_path.parent.is_dir()
199202

200203

201-
def test_local_upload_paths_are_cached(tmp_path: Path):
202-
"""Test local upload paths are cached."""
203-
# No need for an exact singleton here.
204-
# We just want to avoid recreating the dataclass on consecutive calls (happens often
205-
# in the process).
206-
paths1 = get_local_download_paths(tmp_path, "path/in/repo.txt")
207-
paths2 = get_local_download_paths(tmp_path, "path/in/repo.txt")
208-
assert paths1 is paths2
204+
def test_local_upload_paths_are_recreated_each_time(tmp_path: Path):
205+
paths1 = get_local_upload_paths(tmp_path, "path/in/repo.txt")
206+
assert paths1.file_path.parent.is_dir()
207+
assert paths1.metadata_path.parent.is_dir()
208+
209+
paths1.file_path.parent.rmdir()
210+
paths1.metadata_path.parent.rmdir()
211+
212+
paths2 = get_local_upload_paths(tmp_path, "path/in/repo.txt")
213+
assert paths2.file_path.parent.is_dir()
214+
assert paths2.metadata_path.parent.is_dir()
209215

210216

211217
@pytest.mark.skipif(os.name != "nt", reason="Windows-specific test.")

0 commit comments

Comments
 (0)