diff --git a/replicate/file.py b/replicate/file.py index bb3ff86..07ec5ec 100644 --- a/replicate/file.py +++ b/replicate/file.py @@ -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)): @@ -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)): diff --git a/tests/test_file.py b/tests/test_file.py index c92802e..307d9aa 100644 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -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])