Skip to content

Commit

Permalink
Delete annotation if None is passed to set_annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorjerse committed Nov 22, 2023
1 parent 7717784 commit 1f22257
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/resdk/resources/sample.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Sample resource."""
import logging
from typing import TYPE_CHECKING, Any, Dict
from typing import TYPE_CHECKING, Any, Dict, Optional

from resdk.shortcuts.sample import SampleUtilsMixin

Expand Down Expand Up @@ -261,13 +261,21 @@ def get_annotation(self, full_path: str) -> "AnnotationValue":
field__name=field_name, field__group__name=group_name
)

def set_annotation(self, full_path: str, value) -> "AnnotationValue":
"""Create/update annotation value."""
def set_annotation(self, full_path: str, value) -> Optional["AnnotationValue"]:
"""Create/update annotation value.
If value is None the annotatioe is deleted and None is returned.
"""
try:
annotation_value = self.get_annotation(full_path)
if value is None:
annotation_value.delete()
return None
annotation_value.value = value
annotation_value.save()
except LookupError:
if value is None:
return None
field = self.resolwe.annotation_field.from_path(full_path)
annotation_value = self.resolwe.annotation_value.create(
sample=self, field=field, value=value
Expand Down

0 comments on commit 1f22257

Please sign in to comment.