diff --git a/replicate/collection.py b/replicate/collection.py index e77ef590..f9dcbb33 100644 --- a/replicate/collection.py +++ b/replicate/collection.py @@ -1,5 +1,7 @@ from typing import TYPE_CHECKING, Dict, List, Optional, Union +from typing_extensions import deprecated + from replicate.model import Model, Models from replicate.pagination import Page from replicate.resource import Namespace, Resource @@ -25,6 +27,14 @@ class Collection(Resource): models: Optional[List[Model]] = None """The models in the collection.""" + @property + @deprecated("Use `slug` instead of `id`") + def id(self) -> str: + """ + DEPRECATED: Use `slug` instead. + """ + return self.slug + def __iter__(self): # noqa: ANN204 return iter(self.models) @@ -90,13 +100,9 @@ def get(self, slug: str) -> Collection: def _prepare_model(self, attrs: Union[Collection, Dict]) -> Collection: if isinstance(attrs, Resource): - attrs.id = attrs.slug - if attrs.models is not None: attrs.models = [self._models._prepare_model(m) for m in attrs.models] elif isinstance(attrs, dict): - attrs["id"] = attrs["slug"] - if "models" in attrs: attrs["models"] = [ self._models._prepare_model(m) for m in attrs["models"] diff --git a/replicate/deployment.py b/replicate/deployment.py index 62a651a4..5b0077ce 100644 --- a/replicate/deployment.py +++ b/replicate/deployment.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union +from typing import TYPE_CHECKING, Any, Dict, List, Optional from replicate.files import upload_file from replicate.json import encode_json @@ -26,6 +26,10 @@ class Deployment(Resource): The name of the deployment. """ + @property + def id(self) -> str: + return f"{self.username}/{self.name}" + @property def predictions(self) -> "DeploymentPredictions": """ @@ -57,13 +61,6 @@ def get(self, name: str) -> Deployment: username, name = name.split("/") return self._prepare_model({"username": username, "name": name}) - def _prepare_model(self, attrs: Union[Deployment, Dict]) -> Deployment: - if isinstance(attrs, Resource): - attrs.id = f"{attrs.username}/{attrs.name}" - elif isinstance(attrs, dict): - attrs["id"] = f"{attrs['username']}/{attrs['name']}" - return super()._prepare_model(attrs) - class DeploymentPredictions(Namespace): """ diff --git a/replicate/hardware.py b/replicate/hardware.py index a5299e85..e5ea81a2 100644 --- a/replicate/hardware.py +++ b/replicate/hardware.py @@ -1,4 +1,6 @@ -from typing import Dict, List, Union +from typing import List + +from typing_extensions import deprecated from replicate.resource import Namespace, Resource @@ -18,6 +20,14 @@ class Hardware(Resource): The name of the hardware. """ + @property + @deprecated("Use `sku` instead of `id`") + def id(self) -> str: + """ + DEPRECATED: Use `sku` instead. + """ + return self.sku + class Hardwares(Namespace): """ @@ -36,11 +46,3 @@ def list(self) -> List[Hardware]: resp = self._client._request("GET", "/v1/hardware") return [self._prepare_model(obj) for obj in resp.json()] - - def _prepare_model(self, attrs: Union[Hardware, Dict]) -> Hardware: - if isinstance(attrs, Resource): - attrs.id = attrs.sku - elif isinstance(attrs, dict): - attrs["id"] = attrs["sku"] - - return super()._prepare_model(attrs) diff --git a/replicate/model.py b/replicate/model.py index 391d2c10..e9c8288a 100644 --- a/replicate/model.py +++ b/replicate/model.py @@ -76,6 +76,10 @@ class Model(Resource): The latest version of the model. """ + @property + def id(self) -> str: + return f"{self.owner}/{self.name}" + @property @deprecated("Use `model.owner` instead.") def username(self) -> str: @@ -213,11 +217,7 @@ def create( # pylint: disable=arguments-differ disable=too-many-arguments return self._prepare_model(resp.json()) def _prepare_model(self, attrs: Union[Model, Dict]) -> Model: - if isinstance(attrs, Resource): - attrs.id = f"{attrs.owner}/{attrs.name}" - elif isinstance(attrs, dict): - attrs["id"] = f"{attrs['owner']}/{attrs['name']}" - + if isinstance(attrs, dict): if attrs is not None: if "default_example" in attrs and attrs["default_example"]: attrs["default_example"].pop("version") diff --git a/replicate/resource.py b/replicate/resource.py index 9a3473fc..9c5ae52b 100644 --- a/replicate/resource.py +++ b/replicate/resource.py @@ -17,8 +17,6 @@ class Resource(pydantic.BaseModel): A base class for representing a single object on the server. """ - id: str - _client: "Client" = pydantic.PrivateAttr() _namespace: "Namespace" = pydantic.PrivateAttr()