From 1f22257ea7551664c5756173e83b8b8ad4665e9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gregor=20Jer=C5=A1e?= Date: Wed, 22 Nov 2023 12:25:33 +0100 Subject: [PATCH] Delete annotation if None is passed to set_annotation --- src/resdk/resources/sample.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/resdk/resources/sample.py b/src/resdk/resources/sample.py index daa438a4..902824fb 100644 --- a/src/resdk/resources/sample.py +++ b/src/resdk/resources/sample.py @@ -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 @@ -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