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

fix: allow visible_labels=None & disallow fields/questions with the same name #3126

Merged
merged 7 commits into from
Jun 8, 2023
Merged
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: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ These are the section headers that we use:
- Database setup for unit tests. Now the unit tests use a different database than the one used by the local Argilla server (Closes [#2987]).
- Updated `alembic` setup to be able to autogenerate revision/migration scripts using SQLAlchemy metadata from Argilla server models ([#3044])

### Fixed

- Disallow `fields` and `questions` in `FeedbackDataset` with the same name ([#3126]).
- Keep `visible_labels=None` if `None` is specified in `LabelQuestion` or `MultiLabelQuestion`, otherwise, use default 20 ([#3126]).

frascuchon marked this conversation as resolved.
Show resolved Hide resolved
[#3126]: https://github.com/argilla-io/argilla/pull/3126

## [1.8.0](https://github.com/argilla-io/argilla/compare/v1.7.0...v1.8.0)

### Added
Expand Down
8 changes: 8 additions & 0 deletions src/argilla/client/feedback/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,13 @@ def __init__(
if not isinstance(fields, list):
raise TypeError(f"Expected `fields` to be a list, got {type(fields)} instead.")
any_required = False
unique_names = set()
for field in fields:
if not isinstance(field, FieldSchema):
raise TypeError(f"Expected `fields` to be a list of `FieldSchema`, got {type(field)} instead.")
if field.name in unique_names:
raise ValueError(f"Expected `fields` to have unique names, got {field.name} twice instead.")
unique_names.add(field.name)
if not any_required and field.required:
any_required = True
if not any_required:
Expand All @@ -220,13 +224,17 @@ def __init__(
if not isinstance(questions, list):
raise TypeError(f"Expected `questions` to be a list, got {type(questions)} instead.")
any_required = False
unique_names = set()
for question in questions:
if not isinstance(question, (TextQuestion, RatingQuestion, LabelQuestion, MultiLabelQuestion)):
raise TypeError(
"Expected `questions` to be a list of `TextQuestion`, `RatingQuestion`,"
" `LabelQuestion`, and/or `MultiLabelQuestion` got a"
f" question in the list with type {type(question)} instead."
)
if question.name in unique_names:
raise ValueError(f"Expected `questions` to have unique names, got {question.name} twice instead.")
unique_names.add(question.name)
if not any_required and question.required:
any_required = True
if not any_required:
Expand Down
4 changes: 3 additions & 1 deletion src/argilla/client/feedback/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ def update_settings(cls, values: Dict[str, Any]) -> Dict[str, Any]:
]
if isinstance(values.get("labels"), list):
values["settings"]["options"] = [{"value": label, "text": label} for label in values.get("labels")]
values["settings"]["visible_options"] = values.get("visible_labels", 20)
values["settings"]["visible_options"] = values.get(
"visible_labels"
) # `None` is a possible value, which means all labels are visible
return values


Expand Down
23 changes: 20 additions & 3 deletions tests/client/feedback/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import datasets
import pytest
from argilla.client import api
from pydantic import ValidationError

if TYPE_CHECKING:
from argilla.client.feedback.schemas import AllowedFieldTypes, AllowedQuestionTypes
Expand Down Expand Up @@ -84,6 +83,15 @@ def test_init_wrong_fields(feedback_dataset_guidelines: str, feedback_dataset_qu
fields=[TextField(name="test", required=False)],
questions=feedback_dataset_questions,
)
with pytest.raises(ValueError, match="Expected `fields` to have unique names"):
FeedbackDataset(
guidelines=feedback_dataset_guidelines,
fields=[
TextField(name="test", required=True),
TextField(name="test", required=True),
],
questions=feedback_dataset_questions,
)


@pytest.mark.usefixtures("feedback_dataset_guidelines", "feedback_dataset_fields")
Expand All @@ -108,8 +116,17 @@ def test_init_wrong_questions(feedback_dataset_guidelines: str, feedback_dataset
guidelines=feedback_dataset_guidelines,
fields=feedback_dataset_fields,
questions=[
TextQuestion(name="test", required=False),
RatingQuestion(name="test", values=[0, 1], required=False),
TextQuestion(name="question-1", required=False),
RatingQuestion(name="question-2", values=[0, 1], required=False),
],
)
with pytest.raises(ValueError, match="Expected `questions` to have unique names"):
FeedbackDataset(
guidelines=feedback_dataset_guidelines,
fields=feedback_dataset_fields,
questions=[
TextQuestion(name="question-1", required=True),
TextQuestion(name="question-1", required=True),
],
)

Expand Down
16 changes: 16 additions & 0 deletions tests/client/feedback/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ def test_label_question_errors(
"visible_options": 5,
},
),
(
{"name": "a", "description": "a", "required": True, "labels": ["a", "b"], "visible_labels": None},
{
"type": "label_selection",
"options": [{"value": "a", "text": "a"}, {"value": "b", "text": "b"}],
"visible_options": None,
},
),
],
)
def test_label_question(schema_kwargs: Dict[str, Any], expected_settings: Dict[str, Any]) -> None:
Expand Down Expand Up @@ -163,6 +171,14 @@ def test_multi_label_question_errors(
"visible_options": 5,
},
),
(
{"name": "a", "description": "a", "required": True, "labels": ["a", "b"], "visible_labels": None},
{
"type": "multi_label_selection",
"options": [{"value": "a", "text": "a"}, {"value": "b", "text": "b"}],
"visible_options": None,
},
),
],
)
def test_multi_label_question(schema_kwargs: Dict[str, Any], expected_settings: Dict[str, Any]) -> None:
Expand Down