From a624be26267736271aa5cd21f010179222be6f08 Mon Sep 17 00:00:00 2001 From: Lucain Pouget Date: Mon, 6 Nov 2023 14:26:10 +0100 Subject: [PATCH 1/3] Improve error message in hffs if repo not found --- src/huggingface_hub/hf_file_system.py | 31 +++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/huggingface_hub/hf_file_system.py b/src/huggingface_hub/hf_file_system.py index f19f8551aa..c80bc4012c 100644 --- a/src/huggingface_hub/hf_file_system.py +++ b/src/huggingface_hub/hf_file_system.py @@ -4,7 +4,7 @@ import tempfile from dataclasses import dataclass from datetime import datetime -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Dict, List, NoReturn, Optional, Tuple, Union from urllib.parse import quote, unquote import fsspec @@ -180,7 +180,7 @@ def _align_revision_in_path_with_revision( revision = _align_revision_in_path_with_revision(revision_in_path, revision) repo_and_revision_exist, err = self._repo_and_revision_exist(repo_type, repo_id, revision) if not repo_and_revision_exist: - raise FileNotFoundError(path) from err + _raise_file_not_found(path, err) else: repo_id_with_namespace = "/".join(path.split("/")[:2]) path_in_repo_with_namespace = "/".join(path.split("/")[2:]) @@ -195,9 +195,9 @@ def _align_revision_in_path_with_revision( path_in_repo = path_in_repo_without_namespace repo_and_revision_exist, _ = self._repo_and_revision_exist(repo_type, repo_id, revision) if not repo_and_revision_exist: - raise FileNotFoundError(path) from err + _raise_file_not_found(path, err) else: - raise FileNotFoundError(path) from err + _raise_file_not_found(path, err) else: repo_id = path path_in_repo = "" @@ -229,7 +229,7 @@ def _open( revision: Optional[str] = None, **kwargs, ) -> "HfFileSystemFile": - if mode == "ab": + if "a" in mode: raise NotImplementedError("Appending to remote files is not yet supported.") return HfFileSystemFile(self, path, mode=mode, revision=revision, **kwargs) @@ -413,7 +413,15 @@ class HfFileSystemFile(fsspec.spec.AbstractBufferedFile): def __init__(self, fs: HfFileSystem, path: str, revision: Optional[str] = None, **kwargs): super().__init__(fs, path, **kwargs) self.fs: HfFileSystem - self.resolved_path = fs.resolve_path(path, revision=revision) + + mode = kwargs.get("mode", "r") + try: + self.resolved_path = fs.resolve_path(path, revision=revision) + except FileNotFoundError as e: + if "w" in mode: + raise FileNotFoundError( + f"{e}.\nMake sure the repository and revision exist before writing data." + ) from e def _fetch_range(self, start: int, end: int) -> bytes: headers = { @@ -462,3 +470,14 @@ def safe_revision(revision: str) -> str: def safe_quote(s: str) -> str: return quote(s, safe="") + + +def _raise_file_not_found(path: str, err: Optional[Exception]) -> NoReturn: + msg = path + if isinstance(err, RepositoryNotFoundError): + msg = f"{path} (repository not found)" + elif isinstance(err, RevisionNotFoundError): + msg = f"{path} (revision not found)" + elif isinstance(err, HFValidationError): + msg = f"{path} (invalid repository id)" + raise FileNotFoundError(msg) from err From b6e73e425a05a208bd8ad1acaa542ea4ab5c2cc8 Mon Sep 17 00:00:00 2001 From: Lucain Pouget Date: Mon, 6 Nov 2023 14:41:44 +0100 Subject: [PATCH 2/3] fix __del__ if __init__ failed --- src/huggingface_hub/hf_file_system.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/huggingface_hub/hf_file_system.py b/src/huggingface_hub/hf_file_system.py index c80bc4012c..ea86537710 100644 --- a/src/huggingface_hub/hf_file_system.py +++ b/src/huggingface_hub/hf_file_system.py @@ -423,6 +423,12 @@ def __init__(self, fs: HfFileSystem, path: str, revision: Optional[str] = None, f"{e}.\nMake sure the repository and revision exist before writing data." ) from e + def __del__(self): + if not hasattr(self, "resolved_path"): + # Means that the constructor failed. Nothing to do. + return + return super().__del__() + def _fetch_range(self, start: int, end: int) -> bytes: headers = { "range": f"bytes={start}-{end - 1}", From faa8a369743c490cd25230f1aa5da61b50ec3fec Mon Sep 17 00:00:00 2001 From: Lucain Pouget Date: Mon, 6 Nov 2023 14:53:15 +0100 Subject: [PATCH 3/3] quality --- src/huggingface_hub/lfs.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/huggingface_hub/lfs.py b/src/huggingface_hub/lfs.py index 1aad3250e5..e6c1b7f276 100644 --- a/src/huggingface_hub/lfs.py +++ b/src/huggingface_hub/lfs.py @@ -422,6 +422,7 @@ def _upload_parts_hf_transfer( progress.update(total) return output + class SliceFileObj(AbstractContextManager): """ Utility context manager to read a *slice* of a seekable file-like object as a seekable, file-like object.