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 HfFileSystemFile when init fails + improve error message #1805

Merged
merged 4 commits into from
Nov 6, 2023
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
37 changes: 31 additions & 6 deletions src/huggingface_hub/hf_file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:])
Expand All @@ -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 = ""
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -413,7 +413,21 @@ 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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fsspec's AbstractBufferedFile has "rb" as the default mode...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok, thanks for letting me know. I'll open a follow-up PR to update that but in any case it doesn't change anything (this value is used only to check if it's "w" or "wb")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made this commit (18d0ae2) to avoid confusion

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 __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 = {
Expand Down Expand Up @@ -462,3 +476,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
1 change: 1 addition & 0 deletions src/huggingface_hub/lfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading