Skip to content

Commit 556b825

Browse files
authored
[ENHANCEMENT] argilla: Remove exists method and change client behavior (#5199)
# Description <!-- Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change. --> This PR changes the resource existence flow. The `user.exists`, `workspace.exists`, and `dataset.exists` methods have been removed and the `client.datasets(...)`, `client.workspaces(...)`, and `client.users(...)` methods return `None` if resource is not found. Closes #5157 Closes #5121 **Type of change** <!-- Please delete options that are not relevant. Remember to title the PR according to the type of change --> - Improvement (change adding some improvement to an existing functionality) - Documentation update **How Has This Been Tested** <!-- Please add some reference about how your feature has been tested. --> **Checklist** <!-- Please go over the list and make sure you've taken everything into account --> - I added relevant documentation - I followed the style guidelines of this project - I did a self-review of my code - I made corresponding changes to the documentation - I confirm My changes generate no new warnings - I have added tests that prove my fix is effective or that my feature works - I have added relevant notes to the CHANGELOG.md file (See https://keepachangelog.com/)
1 parent 7bd8ce3 commit 556b825

20 files changed

+89
-157
lines changed

argilla/docs/how_to_guides/dataset.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ retrieved_dataset = client.datasets(name="my_dataset", workspace=workspace)
432432

433433
## Check dataset existence
434434

435-
You can check if a retrieved dataset exists by calling the `exists` method on the `Dataset` class. This method returns a boolean value.
435+
You can check if a dataset exists. The `client.datasets` method will return `None` if the dataset was not found.
436436

437437
```python
438438
import argilla as rg
@@ -441,7 +441,7 @@ client = rg.Argilla(api_url="<api_url>", api_key="<api_key>")
441441

442442
dataset = client.datasets(name="my_dataset")
443443

444-
dataset_existed = dataset.exists()
444+
dataset_exist = dataset is not None
445445
```
446446

447447
## Update a dataset

argilla/docs/how_to_guides/migrate_from_legacy_datasets.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ dataset.create()
106106
```python
107107
dataset = client.datasets(name=dataset_name)
108108

109-
if dataset.exists():
109+
if dataset is not None:
110110
dataset.delete()
111111
```
112112

argilla/docs/how_to_guides/workspace.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ retrieved_workspace = client.workspaces("my_workspace")
8686

8787
## Check workspace existence
8888

89-
You can check if a workspace exists by calling the `exists` method on the `Workspace` class. This method returns a boolean value.
89+
You can check if a workspace exists. The `client.workspaces` method will return `None` if the workspace was not found.
9090

9191
```python
9292
import argilla as rg
@@ -95,7 +95,7 @@ client = rg.Argilla(api_url="<api_url>", api_key="<api_key>")
9595

9696
workspace = client.workspaces("my_workspace")
9797

98-
workspace_existed = workspace.exists()
98+
workspace_exists = workspace is not None
9999
```
100100

101101
## List users in a workspace

argilla/src/argilla/client.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ def __init__(self, client: "Argilla") -> None:
110110
self._client = client
111111
self._api = client.api.users
112112

113-
def __call__(self, username: str, **kwargs) -> "User":
113+
def __call__(self, username: str) -> "User":
114114
from argilla.users import User
115115

116116
user_models = self._api.list()
117117
for model in user_models:
118118
if model.username == username:
119119
return User(_model=model, client=self._client)
120-
warnings.warn(f"User {username} not found. Creating a new user. Do `user.create()` to create the user.")
121-
return User(username=username, client=self._client, **kwargs)
120+
121+
warnings.warn(f"User with username {username!r} not found.")
122122

123123
def __iter__(self):
124124
return self._Iterator(self.list())
@@ -188,18 +188,13 @@ def __init__(self, client: "Argilla") -> None:
188188
self._client = client
189189
self._api = client.api.workspaces
190190

191-
def __call__(self, name: str, **kwargs) -> "Workspace":
192-
from argilla.workspaces import Workspace
193-
191+
def __call__(self, name: str) -> Optional["Workspace"]:
194192
workspace_models = self._api.list()
195193

196194
for model in workspace_models:
197195
if model.name == name:
198196
return self._from_model(model)
199-
warnings.warn(
200-
f"Workspace {name} not found. Creating a new workspace. Do `workspace.create()` to create the workspace."
201-
)
202-
return Workspace(name=name, client=self._client, **kwargs)
197+
warnings.warn(f"Workspace with name {name!r} not found.")
203198

204199
def __iter__(self):
205200
return self._Iterator(self.list())
@@ -265,19 +260,16 @@ def __init__(self, client: "Argilla") -> None:
265260
self._client = client
266261
self._api = client.api.datasets
267262

268-
def __call__(self, name: str, workspace: Optional[Union["Workspace", str]] = None, **kwargs) -> "Dataset":
269-
from argilla.datasets import Dataset
270-
263+
def __call__(self, name: str, workspace: Optional[Union["Workspace", str]] = None) -> Optional["Dataset"]:
271264
if isinstance(workspace, str):
272265
workspace = self._client.workspaces(workspace)
273266
elif workspace is None:
274-
workspace = self._client.workspaces[0]
267+
workspace = self._client.workspaces.default
275268

276269
for dataset in workspace.datasets:
277270
if dataset.name == name:
278271
return dataset.get()
279-
warnings.warn(f"Dataset {name} not found. Creating a new dataset. Do `dataset.create()` to create the dataset.")
280-
return Dataset(name=name, workspace=workspace, client=self._client, **kwargs)
272+
warnings.warn(f"Dataset with name {name!r} not found in workspace {workspace.name!r}")
281273

282274
def __iter__(self):
283275
return self._Iterator(self.list())

argilla/src/argilla/datasets/_export.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ def to_disk(self: "Dataset", path: str) -> str:
5252
# Export the dataset model, settings and records
5353
self._persist_dataset_model(path=dataset_path)
5454
self.settings.to_json(path=settings_path)
55-
if self.exists():
56-
self.records.to_json(path=records_path)
55+
self.records.to_json(path=records_path)
56+
5757
return path
5858

5959
@classmethod

argilla/src/argilla/datasets/_resource.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,6 @@ def get(self) -> "Dataset":
142142
self.settings.get()
143143
return self
144144

145-
def exists(self) -> bool:
146-
"""Checks if the dataset exists on the server
147-
148-
Returns:
149-
bool: True if the dataset exists, False otherwise
150-
"""
151-
return self.id and self._api.exists(self.id)
152-
153145
def create(self) -> "Dataset":
154146
"""Creates the dataset on the server with the `Settings` configuration.
155147
@@ -202,7 +194,7 @@ def _resolve_workspace(self) -> Workspace:
202194
warnings.warn(f"Workspace not provided. Using default workspace: {workspace.name} id: {workspace.id}")
203195
elif isinstance(workspace, str):
204196
workspace = self._client.workspaces(workspace)
205-
if not workspace.exists():
197+
if workspace is None:
206198
available_workspace_names = [ws.name for ws in self._client.workspaces]
207199
raise NotFoundError(
208200
f"Workspace with name { workspace} not found. Available workspaces: {available_workspace_names}"
@@ -216,8 +208,8 @@ def _resolve_workspace(self) -> Workspace:
216208
return workspace
217209

218210
def _rollback_dataset_creation(self):
219-
if self.exists() and not self._is_published():
211+
if not self._is_published():
220212
self.delete()
221213

222214
def _is_published(self) -> bool:
223-
return self.exists() and self._model.status == "ready"
215+
return self._model.status == "ready"

argilla/src/argilla/records/_dataset_records.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
14+
import warnings
1515
from pathlib import Path
1616
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Sequence, Union
1717
from uuid import UUID
@@ -81,11 +81,10 @@ def _list(self) -> Sequence[Record]:
8181
yield Record.from_model(model=record_model, dataset=self.__dataset)
8282

8383
def _fetch_from_server(self) -> List[RecordModel]:
84-
if not self.__dataset.exists():
85-
raise ValueError(f"Dataset {self.__dataset.name} does not exist on the server.")
86-
if self._is_search_query():
87-
return self._fetch_from_server_with_search()
88-
return self._fetch_from_server_with_list()
84+
if not self.__client.api.datasets.exists(self.__dataset.id):
85+
warnings.warn(f"Dataset {self.__dataset.id!r} does not exist on the server. Skipping...")
86+
return []
87+
return self._fetch_from_server_with_search() if self._is_search_query() else self._fetch_from_server_with_list()
8988

9089
def _fetch_from_server_with_list(self) -> List[RecordModel]:
9190
return self.__client.api.records.list(

argilla/src/argilla/users/_resource.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,6 @@ def delete(self) -> None:
9898
# exists relies on the id, so we need to set it to None
9999
self._model = UserModel(username=self.username)
100100

101-
def exists(self) -> bool:
102-
"""Checks if the user exists in Argilla
103-
104-
Returns:
105-
bool: True if the user exists, False otherwise.
106-
"""
107-
# TODO - Implement the exist method in the API
108-
return self.id is not None
109-
110101
def add_to_workspace(self, workspace: "Workspace") -> "User":
111102
"""Adds the user to a workspace. After adding a user to a workspace, it will have access to the datasets
112103
in the workspace.

argilla/src/argilla/workspaces/_resource.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,6 @@ def __init__(
6060

6161
self._model = WorkspaceModel(name=name, id=id)
6262

63-
def exists(self) -> bool:
64-
"""
65-
Checks if the workspace exists in the server
66-
67-
Returns:
68-
bool: True if the workspace exists, False otherwise
69-
"""
70-
return self._api.exists(self.id)
71-
7263
def add_user(self, user: Union["User", str]) -> "User":
7364
"""Adds a user to the workspace. After adding a user to the workspace, it will have access to the datasets
7465
in the workspace.
@@ -192,12 +183,12 @@ def _list_users(self) -> List["User"]:
192183

193184
def _delete_user_by_username(self, username: str) -> "User":
194185
user = self._workspace._client.users(username=username)
195-
if not user.exists():
186+
if user is None:
196187
raise ValueError(f"User {username} does not exist")
197188
return user.remove_from_workspace(workspace=self._workspace)
198189

199190
def _add_user_by_username(self, username: str) -> "User":
200191
user = self._workspace._client.users(username=username)
201-
if not user.exists():
192+
if user is None:
202193
raise ValueError(f"User {username} does not exist")
203194
return user.add_to_workspace(workspace=self._workspace)

argilla/tests/integration/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import pytest
1616

1717
import argilla as rg
18+
from argilla import Argilla, Workspace
1819

1920

2021
@pytest.fixture(scope="session")
@@ -34,3 +35,18 @@ def cleanup(client: rg.Argilla):
3435
for user in client.users:
3536
if user.username.startswith("test_"):
3637
user.delete()
38+
39+
40+
@pytest.fixture
41+
def workspace(client: Argilla) -> Workspace:
42+
ws_name = "test-workspace"
43+
44+
workspace = client.workspaces(ws_name)
45+
if workspace is None:
46+
workspace = Workspace(name=ws_name).create()
47+
yield workspace
48+
49+
for dataset in workspace.list_datasets():
50+
dataset.delete()
51+
52+
workspace.delete()

0 commit comments

Comments
 (0)