From 2e2f8b1b0b84185c564e71e8b8f728d7b5c2ff36 Mon Sep 17 00:00:00 2001 From: Twiink Date: Sat, 28 Nov 2026 15:54:39 +0800 Subject: [PATCH] =?UTF-8?q?fix(files):=20=E4=BF=9D=E7=95=99=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=B7=AF=E5=BE=84=E4=B8=8A=E4=BC=A0=E7=9A=84=E6=98=BE?= =?UTF-8?q?=E5=BC=8F=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Twiink --- replicate/file.py | 4 ++-- tests/test_file.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/replicate/file.py b/replicate/file.py index bb3ff86b..07ec5ecf 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 c92802eb..307d9aa8 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])