Skip to content
Draft
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
6 changes: 2 additions & 4 deletions src/browserbase/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ def is_base64_file_input(obj: object) -> TypeGuard[Base64FileInput]:


def is_file_content(obj: object) -> TypeGuard[FileContent]:
return (
isinstance(obj, bytes) or isinstance(obj, tuple) or isinstance(obj, io.IOBase) or isinstance(obj, os.PathLike)
)
return isinstance(obj, bytes) or isinstance(obj, io.IOBase) or isinstance(obj, os.PathLike)


def assert_is_file_content(obj: object, *, key: str | None = None) -> None:
if not is_file_content(obj):
if not is_file_content(obj) and not is_tuple_t(obj):
prefix = f"Expected entry at `{key}`" if key is not None else f"Expected file input `{obj!r}`"
raise RuntimeError(
f"{prefix} to be bytes, an io.IOBase instance, PathLike or a tuple but received {type(obj)} instead. See https://github.com/browserbase/sdk-python/tree/main#file-uploads"
Expand Down
18 changes: 18 additions & 0 deletions tests/test_files.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Any
from pathlib import Path

import anyio
Expand All @@ -22,6 +23,23 @@ def test_tuple_input() -> None:
assert result == IsList(IsTuple("file", IsTuple("README.md", IsBytes())))


tuple_extras = pytest.mark.parametrize("extras", [(), ("text/markdown",), ("text/markdown", {"X-Test": "1"})])


@tuple_extras
def test_pathlike_in_file_tuple(extras: Any) -> None:
file: Any = ("custom.md", readme_path, *extras)
assert extract_files({"file": file}, paths=[["file"]]) == [("file", file)]
assert to_httpx_files({"file": file}) == {"file": ("custom.md", readme_path.read_bytes(), *extras)}


@tuple_extras
@pytest.mark.asyncio
async def test_async_pathlike_in_file_tuple(extras: Any) -> None:
file: Any = ("custom.md", readme_path, *extras)
assert await async_to_httpx_files({"file": file}) == {"file": ("custom.md", readme_path.read_bytes(), *extras)}


@pytest.mark.asyncio
async def test_async_pathlib_includes_file_name() -> None:
result = await async_to_httpx_files({"file": readme_path})
Expand Down
Loading