Skip to content

Commit aa4d3fa

Browse files
authored
Fix HfFileSystemFile when init fails + improve error message (#1805)
* Improve error message in hffs if repo not found * fix __del__ if __init__ failed
1 parent 17be1c5 commit aa4d3fa

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/huggingface_hub/hf_file_system.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import tempfile
55
from dataclasses import dataclass
66
from datetime import datetime
7-
from typing import Any, Dict, List, Optional, Tuple, Union
7+
from typing import Any, Dict, List, NoReturn, Optional, Tuple, Union
88
from urllib.parse import quote, unquote
99

1010
import fsspec
@@ -180,7 +180,7 @@ def _align_revision_in_path_with_revision(
180180
revision = _align_revision_in_path_with_revision(revision_in_path, revision)
181181
repo_and_revision_exist, err = self._repo_and_revision_exist(repo_type, repo_id, revision)
182182
if not repo_and_revision_exist:
183-
raise FileNotFoundError(path) from err
183+
_raise_file_not_found(path, err)
184184
else:
185185
repo_id_with_namespace = "/".join(path.split("/")[:2])
186186
path_in_repo_with_namespace = "/".join(path.split("/")[2:])
@@ -195,9 +195,9 @@ def _align_revision_in_path_with_revision(
195195
path_in_repo = path_in_repo_without_namespace
196196
repo_and_revision_exist, _ = self._repo_and_revision_exist(repo_type, repo_id, revision)
197197
if not repo_and_revision_exist:
198-
raise FileNotFoundError(path) from err
198+
_raise_file_not_found(path, err)
199199
else:
200-
raise FileNotFoundError(path) from err
200+
_raise_file_not_found(path, err)
201201
else:
202202
repo_id = path
203203
path_in_repo = ""
@@ -229,7 +229,7 @@ def _open(
229229
revision: Optional[str] = None,
230230
**kwargs,
231231
) -> "HfFileSystemFile":
232-
if mode == "ab":
232+
if "a" in mode:
233233
raise NotImplementedError("Appending to remote files is not yet supported.")
234234
return HfFileSystemFile(self, path, mode=mode, revision=revision, **kwargs)
235235

@@ -413,7 +413,21 @@ class HfFileSystemFile(fsspec.spec.AbstractBufferedFile):
413413
def __init__(self, fs: HfFileSystem, path: str, revision: Optional[str] = None, **kwargs):
414414
super().__init__(fs, path, **kwargs)
415415
self.fs: HfFileSystem
416-
self.resolved_path = fs.resolve_path(path, revision=revision)
416+
417+
mode = kwargs.get("mode", "r")
418+
try:
419+
self.resolved_path = fs.resolve_path(path, revision=revision)
420+
except FileNotFoundError as e:
421+
if "w" in mode:
422+
raise FileNotFoundError(
423+
f"{e}.\nMake sure the repository and revision exist before writing data."
424+
) from e
425+
426+
def __del__(self):
427+
if not hasattr(self, "resolved_path"):
428+
# Means that the constructor failed. Nothing to do.
429+
return
430+
return super().__del__()
417431

418432
def _fetch_range(self, start: int, end: int) -> bytes:
419433
headers = {
@@ -462,3 +476,14 @@ def safe_revision(revision: str) -> str:
462476

463477
def safe_quote(s: str) -> str:
464478
return quote(s, safe="")
479+
480+
481+
def _raise_file_not_found(path: str, err: Optional[Exception]) -> NoReturn:
482+
msg = path
483+
if isinstance(err, RepositoryNotFoundError):
484+
msg = f"{path} (repository not found)"
485+
elif isinstance(err, RevisionNotFoundError):
486+
msg = f"{path} (revision not found)"
487+
elif isinstance(err, HFValidationError):
488+
msg = f"{path} (invalid repository id)"
489+
raise FileNotFoundError(msg) from err

src/huggingface_hub/lfs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ def _upload_parts_hf_transfer(
422422
progress.update(total)
423423
return output
424424

425+
425426
class SliceFileObj(AbstractContextManager):
426427
"""
427428
Utility context manager to read a *slice* of a seekable file-like object as a seekable, file-like object.

0 commit comments

Comments
 (0)