Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Skip responses with deleted users when log records #5095

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions argilla/src/argilla/_models/_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Optional

from argilla._models import ResourceModel

Expand All @@ -21,7 +22,7 @@


class WorkspaceModel(ResourceModel):
name: str
name: Optional[str]

model_config = ConfigDict(
validate_assignment=True,
Expand All @@ -30,8 +31,8 @@ class WorkspaceModel(ResourceModel):

@field_validator("name")
@classmethod
def validate_name(cls, value):
def validate_name(cls, value: Optional[str]) -> Optional[str]:
"""Validate the name of the workspace is url safe"""
if not re.match(r"^[a-zA-Z0-9_-]+$", value):
if value and not re.match(r"^[a-zA-Z0-9_-]+$", value):
raise ValueError("Workspace name must be url safe")
return value
5 changes: 4 additions & 1 deletion argilla/src/argilla/records/_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union, Iterable
from uuid import UUID, uuid4

from argilla import users

from argilla._models import (
MetadataModel,
RecordModel,
Expand Down Expand Up @@ -342,7 +344,8 @@ def api_models(self) -> List[UserResponseModel]:

responses_by_user_id = defaultdict(list)
for response in self.__responses:
responses_by_user_id[response.user_id].append(response)
if not users.is_deleted_user_id(response.user_id):
responses_by_user_id[response.user_id].append(response)

return [
UserResponse(answers=responses, _record=self.record).api_model()
Expand Down
11 changes: 10 additions & 1 deletion argilla/src/argilla/users/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@

from argilla.users._resource import User

__all__ = ["User", "DELETED_USER"]
__all__ = ["User", "DELETED_USER", "is_deleted_user_id"]

# This is the user id for the deleted user. Used when records contains responses from a user that has been deleted.
DELETED_USER = User(id=UUID("00000000-0000-0000-0000-000000000000"), username="deleted")


def is_deleted_user_id(user_id: UUID) -> bool:
"""Check if a specific user id is the delete user id

Returns:
True: If the provided user id is the DELETE_USER.id. False, otherwise
"""
return DELETED_USER.id == user_id
2 changes: 1 addition & 1 deletion argilla/tests/integration/test_update_dataset_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class TestUpdateDatasetSettings:
def test_update_settings(self, client: Argilla, dataset: Dataset):
settings = dataset.settings

settings.fields.text.use_markdown = True
settings.fields["text"].use_markdown = True
dataset.settings.vectors.add(VectorField(name="vector", dimensions=10))
dataset.settings.metadata.add(FloatMetadataProperty(name="metadata"))
dataset.settings.update()
Expand Down
32 changes: 30 additions & 2 deletions argilla/tests/integration/test_update_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import pytest

import argilla as rg
from argilla import Record
from argilla._models import RecordModel
from argilla.users import DELETED_USER


@pytest.fixture
Expand Down Expand Up @@ -188,3 +187,32 @@ def test_update_records_add_responses(self, client: rg.Argilla, dataset: rg.Data

for record in dataset.records(with_suggestions=True):
assert record.responses["label"][-1].value == "positive"

def test_update_record_with_responses_for_deleted_users(self, client: rg.Argilla, dataset: rg.Dataset):
new_user = rg.User(username=f"test_user_{uuid.uuid4()}", password="test_password").create()
workspace = rg.Workspace(id=dataset.workspace_id).get()

workspace.users.add(new_user)

record = rg.Record(
id=uuid.uuid4(),
fields={"text": "Hello World, how are you?"},
responses=[rg.Response(question_name="label", user_id=new_user.id, value="positive")],
)
dataset.records.log([record])

new_user.delete()
records = list(dataset.records)
assert len(records) == 1
record = records[0]
assert record.responses["label"][0].user_id == DELETED_USER.id

record.responses.add(rg.Response(question_name="label", user_id=client.me.id, value="negative"))
dataset.records.log([record])

records = list(dataset.records)
assert len(records) == 1
record = records[0]

assert record.responses["label"][0].user_id == DELETED_USER.id
assert record.responses["label"][1].user_id == client.me.id
Loading