Skip to content
Open
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
4 changes: 2 additions & 2 deletions replicate/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def create(

if isinstance(file, (str, pathlib.Path)):
file_path = pathlib.Path(file)
params["filename"] = file_path.name
params.setdefault("filename", file_path.name)
with open(file, "rb") as f:
return self.create(f, **params)
elif not isinstance(file, (io.IOBase, BinaryIO)):
Expand All @@ -95,7 +95,7 @@ async def async_create(

if isinstance(file, (str, pathlib.Path)):
file_path = pathlib.Path(file)
params["filename"] = file_path.name
params.setdefault("filename", file_path.name)
with open(file_path, "rb") as f:
return await self.async_create(f, **params)
elif not isinstance(file, (io.IOBase, BinaryIO)):
Expand Down
28 changes: 28 additions & 0 deletions tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,34 @@
)


@pytest.mark.asyncio
@pytest.mark.parametrize("async_flag", [True, False])
@pytest.mark.parametrize("use_string", [True, False])
async def test_file_create_preserves_explicit_filename(
tmp_path, async_flag, use_string
):
path = tmp_path / "temporary.bin"
path.write_bytes(b"image contents")
file_arg = str(path) if use_string else path
local_router = respx.Router(base_url="https://api.replicate.com/v1")
route = local_router.post("/files").mock(
return_value=router["files.create"].return_value
)
client = Client(
api_token="test-token", transport=httpx.MockTransport(local_router.handler)
)

if async_flag:
await client.files.async_create(file_arg, filename="image.png")
else:
client.files.create(file_arg, filename="image.png")

request = route.calls[0].request
assert b'filename="image.png"' in request.content
assert b"Content-Type: image/png" in request.content
assert b"image contents" in request.content


@pytest.mark.asyncio
@pytest.mark.parametrize("async_flag", [True, False])
@pytest.mark.parametrize("use_path", [True, False])
Expand Down