Browse Source

Add non negative constraint to span offset

pull/1832/head
Hironsan 3 years ago
parent
commit
fe02b2bcb3
2 changed files with 7 additions and 3 deletions
  1. 6
      backend/data_import/pipeline/label.py
  2. 4
      backend/data_import/tests/test_label.py

6
backend/data_import/pipeline/label.py

@ -2,7 +2,7 @@ import abc
import uuid
from typing import Any, Optional
from pydantic import UUID4, BaseModel, validator
from pydantic import UUID4, BaseModel, NonNegativeInt, validator
from .label_types import LabelTypes
from examples.models import Example
@ -70,8 +70,8 @@ class CategoryLabel(Label):
class SpanLabel(Label):
label: str
start_offset: int
end_offset: int
start_offset: NonNegativeInt
end_offset: NonNegativeInt
def __lt__(self, other):
return self.start_offset < other.start_offset

4
backend/data_import/tests/test_label.py

@ -82,6 +82,10 @@ class TestSpanLabel(TestLabel):
self.assertEqual(span.start_offset, 0)
self.assertEqual(span.end_offset, 1)
def test_invalid_negative_offset(self):
with self.assertRaises(ValueError):
SpanLabel(label="A", start_offset=-1, end_offset=1, example_uuid=uuid.uuid4())
def test_parse_invalid_dict(self):
example_uuid = uuid.uuid4()
with self.assertRaises(ValueError):

Loading…
Cancel
Save