Skip to content

Commit 01b903c

Browse files
authored
fix: correct file not found error (#171)
* fix: correct file not found error * fix: allow load binary to recieve path
1 parent d7062b7 commit 01b903c

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

docarray/array/mixins/io/binary.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import pickle
66
from contextlib import nullcontext
7+
from pathlib import Path
78
from typing import Union, BinaryIO, TYPE_CHECKING, Type, Optional, Generator
89

910
from ....helper import (
@@ -24,7 +25,7 @@ class BinaryIOMixin:
2425
@classmethod
2526
def load_binary(
2627
cls: Type['T'],
27-
file: Union[str, BinaryIO, bytes],
28+
file: Union[str, BinaryIO, bytes, Path],
2829
protocol: str = 'pickle-array',
2930
compress: Optional[str] = None,
3031
_show_progress: bool = False,
@@ -54,13 +55,14 @@ def load_binary(
5455
file_ctx = nullcontext(file)
5556
elif isinstance(file, bytes):
5657
file_ctx = nullcontext(file)
58+
# by checking path existence we allow file to be of type Path, LocalPath, PurePath and str
5759
elif os.path.exists(file):
5860
protocol, compress = protocol_and_compress_from_file_path(
5961
file, protocol, compress
6062
)
6163
file_ctx = open(file, 'rb')
6264
else:
63-
raise ValueError(f'unsupported input {file!r}')
65+
raise FileNotFoundError(f'cannot find file {file}')
6466
if streaming:
6567
return cls._load_binary_stream(
6668
file_ctx,

tests/unit/array/test_from_to_bytes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ def test_from_to_protobuf(target_da):
9393
DocumentArray.from_protobuf(target_da.to_protobuf())
9494

9595

96+
def test_non_existing_file_raises_file_not_found_error():
97+
with pytest.raises(FileNotFoundError):
98+
DocumentArray.load_binary('file_does_not_exists.bin')
99+
100+
96101
@pytest.mark.parametrize('target', [DocumentArray.empty(10), random_docs(10)])
97102
@pytest.mark.parametrize('protocol', ['jsonschema', 'protobuf'])
98103
@pytest.mark.parametrize('to_fn', ['dict', 'json'])

0 commit comments

Comments
 (0)