Skip to content

Commit

Permalink
Pass revision in path to AbstractBufferedFile init (#1948)
Browse files Browse the repository at this point in the history
* Pass revision in path to AbstractBufferedFile init

* Add regression test
  • Loading branch information
albertvillanova authored Jan 3, 2024
1 parent e20e916 commit a331e82
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/huggingface_hub/hf_file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,16 +593,15 @@ def start_transaction(self):

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

try:
self.resolved_path = fs.resolve_path(path, revision=revision)
except FileNotFoundError as e:
if "w" in kwargs.get("mode", ""):
raise FileNotFoundError(
f"{e}.\nMake sure the repository and revision exist before writing data."
) from e
super().__init__(fs, self.resolved_path.unresolve(), **kwargs)
self.fs: HfFileSystem

def __del__(self):
if not hasattr(self, "resolved_path"):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_hf_file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ def test_read_file(self):
with self.hffs.open(self.hf_path + "/data/text_data.txt", "r") as f:
self.assertEqual(f.read(), "dummy text data")

def test_read_file_with_revision(self):
with self.hffs.open(self.hf_path + "/data/binary_data_for_pr.bin", "rb", revision="refs/pr/1") as f:
self.assertEqual(f.read(), b"dummy binary data on pr")

def test_write_file(self):
data = "new text data"
with self.hffs.open(self.hf_path + "/data/new_text_data.txt", "w") as f:
Expand Down

0 comments on commit a331e82

Please sign in to comment.