Skip to content

Commit 3eebb61

Browse files
committed
AL-3578: Added range validation
1 parent 5599dea commit 3eebb61

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

labelbox/data/mixins.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
from typing import Optional
22

3-
from pydantic import BaseModel
3+
from pydantic import BaseModel, validator
44

55
from labelbox.exceptions import ConfidenceNotSupportedException
66

77

88
class ConfidenceMixin(BaseModel):
99
confidence: Optional[float] = None
1010

11+
@validator('confidence')
12+
def confidence_valid_float(cls, value):
13+
if not isinstance(value, (int, float)) or not 0 <= value <= 1:
14+
raise ValueError('must be float within [0,1] range')
15+
return value
16+
1117
def dict(self, *args, **kwargs):
1218
res = super().dict(*args, **kwargs)
1319
if 'confidence' in res and res['confidence'] is None:

tests/data/annotation_types/test_annotation.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def test_video_annotations():
6565

6666
VideoObjectAnnotation(value=line, name=name, keyframe=True, frame=2)
6767

68+
6869
def test_confidence_for_video_is_not_supported():
6970
with pytest.raises(ConfidenceNotSupportedException):
7071
VideoObjectAnnotation(
@@ -81,10 +82,23 @@ def test_confidence_for_video_is_not_supported():
8182
x=70.0,
8283
y=26.5),
8384
end=Point(extra={},
84-
x=561.0,
85-
y=348.0)),
85+
x=561.0,
86+
y=348.0)),
8687
classifications=[],
8788
frame=24,
88-
keyframe=False,
89+
keyframe=False,
8990
confidence=0.3434
9091
),
92+
93+
94+
def test_confidence_value_range_validation():
95+
name = "line_feature"
96+
line = Line(points=[Point(x=1, y=2), Point(x=2, y=2)])
97+
98+
with pytest.raises(ValueError) as e:
99+
ObjectAnnotation(
100+
value=line,
101+
name=name,
102+
confidence=14
103+
)
104+
assert e.value.errors()[0]['msg'] == 'must be float within [0,1] range'

0 commit comments

Comments
 (0)