Skip to content

Commit

Permalink
fix: correct file not found error (#171)
Browse files Browse the repository at this point in the history
* fix: correct file not found error

* fix: allow load binary to recieve path
  • Loading branch information
davidbp authored Mar 4, 2022
1 parent d7062b7 commit 01b903c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 4 additions & 2 deletions docarray/array/mixins/io/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import pickle
from contextlib import nullcontext
from pathlib import Path
from typing import Union, BinaryIO, TYPE_CHECKING, Type, Optional, Generator

from ....helper import (
Expand All @@ -24,7 +25,7 @@ class BinaryIOMixin:
@classmethod
def load_binary(
cls: Type['T'],
file: Union[str, BinaryIO, bytes],
file: Union[str, BinaryIO, bytes, Path],
protocol: str = 'pickle-array',
compress: Optional[str] = None,
_show_progress: bool = False,
Expand Down Expand Up @@ -54,13 +55,14 @@ def load_binary(
file_ctx = nullcontext(file)
elif isinstance(file, bytes):
file_ctx = nullcontext(file)
# by checking path existence we allow file to be of type Path, LocalPath, PurePath and str
elif os.path.exists(file):
protocol, compress = protocol_and_compress_from_file_path(
file, protocol, compress
)
file_ctx = open(file, 'rb')
else:
raise ValueError(f'unsupported input {file!r}')
raise FileNotFoundError(f'cannot find file {file}')
if streaming:
return cls._load_binary_stream(
file_ctx,
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/array/test_from_to_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ def test_from_to_protobuf(target_da):
DocumentArray.from_protobuf(target_da.to_protobuf())


def test_non_existing_file_raises_file_not_found_error():
with pytest.raises(FileNotFoundError):
DocumentArray.load_binary('file_does_not_exists.bin')


@pytest.mark.parametrize('target', [DocumentArray.empty(10), random_docs(10)])
@pytest.mark.parametrize('protocol', ['jsonschema', 'protobuf'])
@pytest.mark.parametrize('to_fn', ['dict', 'json'])
Expand Down

0 comments on commit 01b903c

Please sign in to comment.