Skip to content

Commit

Permalink
Consistently return Boolean for delete methods (#359)
Browse files Browse the repository at this point in the history
Resolves #356 

When deleting a model, Replicate's API returns with a 204 and an empty
response body. The client library fails to decode this and raises and
exception.

This PR updates the `replicate.models.delete` method to return `bool`
instead to indicate success. It also harmonizes the other delete methods
for deployments and files to do the same.

Signed-off-by: Mattt Zmuda <[email protected]>
  • Loading branch information
mattt authored Sep 25, 2024
1 parent d5f5e27 commit 67a87c5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
10 changes: 6 additions & 4 deletions replicate/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,31 +358,33 @@ async def async_update(

return _json_to_deployment(self._client, resp.json())

def delete(self, deployment_owner: str, deployment_name: str) -> None:
def delete(self, deployment_owner: str, deployment_name: str) -> bool:
"""
Delete an existing deployment.
Args:
deployment_owner: The owner of the deployment.
deployment_name: The name of the deployment.
"""
self._client._request(
resp = self._client._request(
"DELETE",
f"/v1/deployments/{deployment_owner}/{deployment_name}",
)
return resp.status_code == 204

async def async_delete(self, deployment_owner: str, deployment_name: str) -> None:
async def async_delete(self, deployment_owner: str, deployment_name: str) -> bool:
"""
Delete an existing deployment asynchronously.
Args:
deployment_owner: The owner of the deployment.
deployment_name: The name of the deployment.
"""
await self._client._async_request(
resp = await self._client._async_request(
"DELETE",
f"/v1/deployments/{deployment_owner}/{deployment_name}",
)
return resp.status_code == 204

@property
def predictions(self) -> "DeploymentsPredictions":
Expand Down
10 changes: 6 additions & 4 deletions replicate/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,17 @@ async def async_list(self) -> List[File]:
resp = await self._client._async_request("GET", "/v1/files")
return [_json_to_file(obj) for obj in resp.json().get("results", [])]

def delete(self, file_id: str) -> None:
def delete(self, file_id: str) -> bool:
"""Delete an uploaded file by its ID."""

_ = self._client._request("DELETE", f"/v1/files/{file_id}")
resp = self._client._request("DELETE", f"/v1/files/{file_id}")
return resp.status_code == 204

async def async_delete(self, file_id: str) -> None:
async def async_delete(self, file_id: str) -> bool:
"""Delete an uploaded file by its ID asynchronously."""

_ = await self._client._async_request("DELETE", f"/v1/files/{file_id}")
resp = await self._client._async_request("DELETE", f"/v1/files/{file_id}")
return resp.status_code == 204


def _create_file_params(
Expand Down
29 changes: 13 additions & 16 deletions replicate/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,41 +287,38 @@ async def async_get(self, *args, **kwargs) -> Model:
return _json_to_model(self._client, resp.json())

@overload
def delete(self, key: str) -> Model: ...
def delete(self, key: str) -> bool: ...

@overload
def delete(self, owner: str, name: str) -> Model: ...
def delete(self, owner: str, name: str) -> bool: ...

def delete(self, *args, **kwargs) -> Model:
def delete(self, *args, **kwargs) -> bool:
"""
Delete a model by name.
"""
Returns:
`True` if deletion was successful, otherwise `False`.
"""
url = _delete_model_url(*args, **kwargs)
resp = self._client._request("DELETE", url)

return _json_to_model(self._client, resp.json())
return resp.status_code == 204

@overload
async def async_delete(self, key: str) -> Model: ...
async def async_delete(self, key: str) -> bool: ...

@overload
async def async_delete(self, owner: str, name: str) -> Model: ...
async def async_delete(self, owner: str, name: str) -> bool: ...

async def async_delete(self, *args, **kwargs) -> Model:
async def async_delete(self, *args, **kwargs) -> bool:
"""
Delete a model by name.
Asynchronously delete a model by name.
Args:
key: The qualified name of the model, in the format `owner/name`.
Returns:
The model.
`True` if deletion was successful, otherwise `False`.
"""

url = _delete_model_url(*args, **kwargs)
resp = await self._client._async_request("DELETE", url)

return _json_to_model(self._client, resp.json())
return resp.status_code == 204

class CreateModelParams(TypedDict):
"""Parameters for creating a model."""
Expand Down

0 comments on commit 67a87c5

Please sign in to comment.