Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Commit

Permalink
feat: update span questions (#50)
Browse files Browse the repository at this point in the history
# Description

This PR add changes so span questions can be updated and labels on
settings reorder. The labels can not be changed only reordered as we are
already doing for label and multi label questions.

Closes #49 

**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)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] Refactor (change restructuring the codebase without changing
functionality)
- [ ] Improvement (change adding some improvement to an existing
functionality)
- [ ] Documentation update

**How Has This Been Tested**

(Please describe the tests that you ran to verify your changes. And
ideally, reference `tests`)

- [ ] Test A
- [ ] Test B

**Checklist**

- [ ] I added relevant documentation
- [ ] follows the style guidelines of this project
- [ ] I did a self-review of my code
- [ ] I made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I filled out [the contributor form](https://tally.so/r/n9XrxK)
(see text above)
- [ ] I have added relevant notes to the CHANGELOG.md file (See
https://keepachangelog.com/)
  • Loading branch information
jfcalvo authored Mar 5, 2024
1 parent fe03d2a commit 975ca82
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/argilla_server/apis/v1/handlers/suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ async def _get_suggestion(db: "AsyncSession", suggestion_id: UUID) -> Suggestion
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Suggestion with id `{suggestion_id}` not found",
)

return suggestion


Expand Down
10 changes: 7 additions & 3 deletions src/argilla_server/contexts/questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def _validate_question_settings(
) -> None:
_validate_settings_type(question_settings, question_update_settings)

if question_settings.type in [QuestionType.label_selection, QuestionType.multi_label_selection]:
if question_settings.type in [QuestionType.label_selection, QuestionType.multi_label_selection, QuestionType.span]:
_validate_label_options(question_settings, question_update_settings)


Expand All @@ -106,6 +106,11 @@ def _validate_question_before_create(dataset: Dataset, question_create: Question
_validate_span_question_settings_before_create(dataset, question_create.settings)


def _validate_question_before_update(question: Question, question_update: QuestionUpdate) -> None:
if question_update.settings:
_validate_question_settings(question.parsed_settings, question_update.settings)


def _validate_span_question_settings_before_create(
dataset: Dataset, span_question_settings_create: SpanQuestionSettingsCreate
) -> None:
Expand Down Expand Up @@ -140,8 +145,7 @@ async def update_question(db: AsyncSession, question_id: UUID, question_update:

await authorize(current_user, QuestionPolicyV1.update(question))

if question_update.settings:
_validate_question_settings(question.parsed_settings, question_update.settings)
_validate_question_before_update(question, question_update)

params = question_update.dict(exclude_unset=True)
return await question.update(db, **params)
Expand Down
6 changes: 6 additions & 0 deletions src/argilla_server/schemas/v1/questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,19 @@ class RankingQuestionSettingsUpdate(UpdateSchema):
type: Literal[QuestionType.ranking]


class SpanQuestionSettingsUpdate(UpdateSchema):
type: Literal[QuestionType.span]
options: Optional[conlist(item_type=OptionSettings)]


QuestionSettingsUpdate = Annotated[
Union[
TextQuestionSettingsUpdate,
RatingQuestionSettingsUpdate,
LabelSelectionSettingsUpdate,
MultiLabelSelectionQuestionSettingsUpdate,
RankingQuestionSettingsUpdate,
SpanQuestionSettingsUpdate,
],
Field(..., discriminator="type"),
]
Expand Down
2 changes: 2 additions & 0 deletions tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ class SpanQuestionFactory(QuestionFactory):
{"value": "label-b", "text": "Label B", "description": "Label B description"},
{"value": "label-c", "text": "Label C", "description": "Label C description"},
],
"allow_overlapping": False,
"allow_character_annotation": True,
}


Expand Down
54 changes: 54 additions & 0 deletions tests/unit/api/v1/test_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
MultiLabelSelectionQuestionFactory,
RankingQuestionFactory,
RatingQuestionFactory,
SpanQuestionFactory,
TextQuestionFactory,
UserFactory,
)
Expand Down Expand Up @@ -177,6 +178,31 @@
],
},
),
(
SpanQuestionFactory,
{
"settings": {
"type": "span",
"field": "field-a",
"options": [
{"value": "label-b", "text": "Label B", "description": "Label B description"},
{"value": "label-a", "text": "Label A", "description": "Label A description"},
{"value": "label-c", "text": "Label C", "description": "Label C description"},
],
}
},
{
"type": "span",
"field": "field-a",
"options": [
{"value": "label-b", "text": "Label B", "description": "Label B description"},
{"value": "label-a", "text": "Label A", "description": "Label A description"},
{"value": "label-c", "text": "Label C", "description": "Label C description"},
],
"allow_overlapping": False,
"allow_character_annotation": True,
},
),
],
)
@pytest.mark.parametrize("role", [UserRole.owner])
Expand Down Expand Up @@ -213,6 +239,7 @@ async def test_update_question(
}

question = await db.get(Question, question.id)

assert question.title == title
assert question.description == description
assert question.settings == expected_settings
Expand Down Expand Up @@ -322,6 +349,33 @@ async def test_update_question_with_invalid_description(
}
},
),
(
SpanQuestionFactory,
{
"settings": {
"type": "span",
"field": "field-a",
"options": [
{"value": "label-b", "text": "Label B", "description": "Label B description"},
{"value": "label-c", "text": "Label C", "description": "Label C description"},
],
}
},
),
(
SpanQuestionFactory,
{
"settings": {
"type": "span",
"field": "field-a",
"options": [
{"value": "label-a", "text": "Label A", "description": "Label A description"},
{"value": "label-b", "text": "Label B", "description": "Label B description"},
{"value": "label-d", "text": "Label D", "description": "Label D description"},
],
}
},
),
],
)
@pytest.mark.asyncio
Expand Down

0 comments on commit 975ca82

Please sign in to comment.