Skip to content

Commit 0cbbdeb

Browse files
authored
[BUGFIX] prevent errors when updating user (#5742)
# 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 resolves some errors found after changes included in #5615. **Type of change** <!-- Please delete options that are not relevant. Remember to title the PR according to the type of change --> - Bug fix (non-breaking change which fixes an issue) **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 e6fe97c commit 0cbbdeb

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

argilla/src/argilla/_api/_users.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def create(self, user: UserModel) -> UserModel:
4545

4646
@api_error_handler
4747
def update(self, user: UserModel) -> UserModel:
48-
json_body = user.model_dump()
48+
json_body = user.model_dump(exclude_unset=True)
4949
response = self.http_client.patch(f"/api/v1/users/{user.id}", json=json_body).raise_for_status()
5050
user_updated = self._model_from_json(response_json=response.json())
5151
self._log_message(message=f"Updated user {user_updated.username}")

argilla/src/argilla/_models/_user.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from typing import Optional
1717

1818
from pydantic import field_validator, ConfigDict
19+
from pydantic_core.core_schema import ValidationInfo
1920

2021
from argilla._models import ResourceModel
2122

@@ -43,12 +44,12 @@ class UserModel(ResourceModel):
4344

4445
@field_validator("first_name")
4546
@classmethod
46-
def __validate_first_name(cls, v, values):
47+
def __validate_first_name(cls, v, info: ValidationInfo):
4748
"""Set first_name to username if not provided"""
4849
if isinstance(v, str):
4950
return v
5051
elif not v:
51-
return values["username"]
52+
return info.data["username"]
5253

5354
@field_validator("username", mode="before")
5455
@classmethod

argilla/tests/integration/test_manage_users.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ def test_update_user(self, client: Argilla):
6464
assert updated_user.last_name == "Updated Last Name"
6565
assert updated_user.role == "admin"
6666

67+
def test_update_user_role(self, client: Argilla):
68+
user = User(username=f"test_update_user_{uuid.uuid4()}", password="test_password")
69+
client.users.add(user)
70+
71+
user = client.users(username=user.username)
72+
73+
user.role = "admin"
74+
user.update()
75+
76+
updated_user = client.users(id=user.id)
77+
assert updated_user.role == "admin"
78+
6779
def test_update_user_with_duplicate_username(self, client: Argilla):
6880
user1 = User(username=f"test_user1_{uuid.uuid4()}", password="test_password")
6981
user2 = User(username=f"test_user2_{uuid.uuid4()}", password="test_password")

argilla/tests/unit/test_resources/test_users.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,9 @@ def test_create_user(self, httpx_mock: HTTPXMock):
322322
assert user.id == user_id
323323
assert user.username == "test-user"
324324
assert user.password == "test-password"
325+
326+
327+
class TestUserModel:
328+
def test_create_user_with_empty_first_name(self):
329+
user = UserModel(username="test-user", first_name=None)
330+
assert user.first_name is user.username

0 commit comments

Comments
 (0)