Skip to content

Commit

Permalink
[BUGFIX] prevent errors when updating user (#5742)
Browse files Browse the repository at this point in the history
# 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/)
  • Loading branch information
frascuchon authored Dec 10, 2024
1 parent e6fe97c commit 0cbbdeb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion argilla/src/argilla/_api/_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def create(self, user: UserModel) -> UserModel:

@api_error_handler
def update(self, user: UserModel) -> UserModel:
json_body = user.model_dump()
json_body = user.model_dump(exclude_unset=True)
response = self.http_client.patch(f"/api/v1/users/{user.id}", json=json_body).raise_for_status()
user_updated = self._model_from_json(response_json=response.json())
self._log_message(message=f"Updated user {user_updated.username}")
Expand Down
5 changes: 3 additions & 2 deletions argilla/src/argilla/_models/_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from typing import Optional

from pydantic import field_validator, ConfigDict
from pydantic_core.core_schema import ValidationInfo

from argilla._models import ResourceModel

Expand Down Expand Up @@ -43,12 +44,12 @@ class UserModel(ResourceModel):

@field_validator("first_name")
@classmethod
def __validate_first_name(cls, v, values):
def __validate_first_name(cls, v, info: ValidationInfo):
"""Set first_name to username if not provided"""
if isinstance(v, str):
return v
elif not v:
return values["username"]
return info.data["username"]

@field_validator("username", mode="before")
@classmethod
Expand Down
12 changes: 12 additions & 0 deletions argilla/tests/integration/test_manage_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ def test_update_user(self, client: Argilla):
assert updated_user.last_name == "Updated Last Name"
assert updated_user.role == "admin"

def test_update_user_role(self, client: Argilla):
user = User(username=f"test_update_user_{uuid.uuid4()}", password="test_password")
client.users.add(user)

user = client.users(username=user.username)

user.role = "admin"
user.update()

updated_user = client.users(id=user.id)
assert updated_user.role == "admin"

def test_update_user_with_duplicate_username(self, client: Argilla):
user1 = User(username=f"test_user1_{uuid.uuid4()}", password="test_password")
user2 = User(username=f"test_user2_{uuid.uuid4()}", password="test_password")
Expand Down
6 changes: 6 additions & 0 deletions argilla/tests/unit/test_resources/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,9 @@ def test_create_user(self, httpx_mock: HTTPXMock):
assert user.id == user_id
assert user.username == "test-user"
assert user.password == "test-password"


class TestUserModel:
def test_create_user_with_empty_first_name(self):
user = UserModel(username="test-user", first_name=None)
assert user.first_name is user.username

0 comments on commit 0cbbdeb

Please sign in to comment.