From c683d2964772358ab5b7e82059d68152077b1b93 Mon Sep 17 00:00:00 2001 From: David Nicholson Date: Wed, 16 Jul 2025 23:06:31 -0400 Subject: [PATCH] Fix Annotation.__init__ to handle length-zero Sequence, fixes #290 --- src/crowsetta/annotation.py | 4 ++-- tests/test_annotation.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/crowsetta/annotation.py b/src/crowsetta/annotation.py index 8e0146e6..f840df82 100644 --- a/src/crowsetta/annotation.py +++ b/src/crowsetta/annotation.py @@ -88,7 +88,7 @@ def __init__( if seq is not None and bboxes is not None: raise ValueError("an Annotation can have either a ``seq``" "or ``bboxes``, but not both.") - if seq: + if seq is not None: if not ( isinstance(seq, crowsetta.Sequence) or (isinstance(seq, list) and all([isinstance(seq_, crowsetta.Sequence) for seq_ in seq])) @@ -96,7 +96,7 @@ def __init__( raise TypeError(f"``seq`` should be a crowsetta.Sequence or list of Sequences but was: {type(seq)}") self.seq = seq - if bboxes: + if bboxes is not None: if not isinstance(bboxes, list): raise ValueError("``bboxes`` should be a list") if not all([isinstance(bbox, BBox) for bbox in bboxes]): diff --git a/tests/test_annotation.py b/tests/test_annotation.py index 5f6a9932..8c76c6a2 100644 --- a/tests/test_annotation.py +++ b/tests/test_annotation.py @@ -70,3 +70,16 @@ def bbox_not_list_raises(): def bbox_not_list_of_bboxes_raises(): with pytest.raises(ValueError): crowsetta.Annotation(annot_path="./an/annnot.csv", notated_path=None, bboxes=[_ for _ in range(10)]) + + +def test_seq_with_no_segments(): + """Test that an :class:`crowsetta.Annotation` + instantiated with a :class:`crowsetta.Sequence` + that has no segments / a length of zero + has a `seq` attribute that points to that Sequence. + """ + # see https://github.com/vocalpy/crowsetta/issues/290 + seq = crowsetta.Sequence.from_keyword(labels=[], onsets_s=[], offsets_s=[]) + annot = crowsetta.Annotation(seq=seq, annot_path="./path.csv") + assert hasattr(annot, "seq") + assert annot.seq == seq