From 0d7df86723e1e9ffecd932f0fd20ebe6ef63c6c5 Mon Sep 17 00:00:00 2001 From: Twiink Date: Fri, 27 Nov 2026 13:46:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(files):=20=E4=BF=AE=E5=A4=8D=E4=B8=B4?= =?UTF-8?q?=E6=97=B6=E6=96=87=E4=BB=B6=E4=BD=9C=E4=B8=BA=E9=A2=84=E6=B5=8B?= =?UTF-8?q?=E8=BE=93=E5=85=A5=E6=97=B6=E7=9A=84=E6=96=87=E4=BB=B6=E5=90=8D?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Twiink --- replicate/file.py | 9 ++++++++- replicate/helpers.py | 4 +++- tests/test_run.py | 33 +++++++++++++++++++++++++++------ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/replicate/file.py b/replicate/file.py index bb3ff86..8257cd1 100644 --- a/replicate/file.py +++ b/replicate/file.py @@ -146,6 +146,13 @@ async def async_delete(self, file_id: str) -> bool: return resp.status_code == 204 +def get_file_name(file: Union[BinaryIO, io.IOBase]) -> str: + name = getattr(file, "name", None) + if isinstance(name, (str, bytes, os.PathLike)): + return os.path.basename(os.fsdecode(name)) + return "file" + + def _create_file_params( file: Union[BinaryIO, io.IOBase], **params: Unpack["Files.CreateFileParams"], @@ -155,7 +162,7 @@ def _create_file_params( if params is None: params = {} - filename = params.get("filename", os.path.basename(getattr(file, "name", "file"))) + filename = params.get("filename", get_file_name(file)) content_type = ( params.get("content_type") or mimetypes.guess_type(filename)[0] diff --git a/replicate/helpers.py b/replicate/helpers.py index 62c7d67..725e968 100644 --- a/replicate/helpers.py +++ b/replicate/helpers.py @@ -8,6 +8,8 @@ import httpx +from replicate.file import get_file_name + if TYPE_CHECKING: from replicate.client import Client from replicate.file import FileEncodingStrategy @@ -110,7 +112,7 @@ def base64_encode_file(file: io.IOBase) -> str: encoded_body = base64.b64encode(body).decode("utf-8") mime_type = ( - mimetypes.guess_type(getattr(file, "name", ""))[0] or "application/octet-stream" + mimetypes.guess_type(get_file_name(file))[0] or "application/octet-stream" ) return f"data:{mime_type};base64,{encoded_body}" diff --git a/tests/test_run.py b/tests/test_run.py index 93f7248..7fd4f1b 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -586,9 +586,27 @@ async def test_run_with_model_error(mock_replicate_api_token): assert excinfo.value.prediction.status == "failed" +@pytest.fixture(params=["unnamed", "null-name", "descriptor"]) +def prediction_file(request, tmp_path): + if request.param == "descriptor": + with (tmp_path / "source").open("wb+") as source: + with open(source.fileno(), "rb+", closefd=False) as file: + file.write(b"hello world") + yield file + else: + file = io.BytesIO(b"hello world") + if request.param == "null-name": + # An unrolled SpooledTemporaryFile can expose a null name. + file.name = None + with file: + yield file + + @pytest.mark.asyncio @pytest.mark.parametrize("async_flag", [True, False]) -async def test_run_with_file_input_files_api(async_flag, mock_replicate_api_token): +async def test_run_with_file_input_files_api( + async_flag, mock_replicate_api_token, prediction_file +): router = respx.Router(base_url="https://api.replicate.com/v1") mock_predictions_create = router.route(method="POST", path="/predictions").mock( return_value=httpx.Response( @@ -633,12 +651,12 @@ async def test_run_with_file_input_files_api(async_flag, mock_replicate_api_toke if async_flag: await client.async_run( "test/example:v1", - input={"file": io.BytesIO(initial_bytes=b"hello world")}, + input={"file": prediction_file}, ) else: client.run( "test/example:v1", - input={"file": io.BytesIO(initial_bytes=b"hello world")}, + input={"file": prediction_file}, ) assert mock_predictions_create.called @@ -660,13 +678,16 @@ async def test_run_with_file_input_files_api(async_flag, mock_replicate_api_toke next(parsed_message_generator) # wrapper input_file = next(parsed_message_generator) assert mock_files_create.called + assert input_file.get_filename() == "file" assert input_file.get_content() == b"hello world" assert input_file.get_content_type() == "application/octet-stream" @pytest.mark.asyncio @pytest.mark.parametrize("async_flag", [True, False]) -async def test_run_with_file_input_data_url(async_flag, mock_replicate_api_token): +async def test_run_with_file_input_data_url( + async_flag, mock_replicate_api_token, prediction_file +): router = respx.Router(base_url="https://api.replicate.com/v1") mock_predictions_create = router.route(method="POST", path="/predictions").mock( return_value=httpx.Response( @@ -692,13 +713,13 @@ async def test_run_with_file_input_data_url(async_flag, mock_replicate_api_token if async_flag: await client.async_run( "test/example:v1", - input={"file": io.BytesIO(initial_bytes=b"hello world")}, + input={"file": prediction_file}, file_encoding_strategy="base64", ) else: client.run( "test/example:v1", - input={"file": io.BytesIO(initial_bytes=b"hello world")}, + input={"file": prediction_file}, file_encoding_strategy="base64", )