Skip to content

Commit 5c74d7f

Browse files
Vaghinak BasentsyanVaghinak Basentsyan
authored andcommitted
Added dynamic behaviour to schemas
1 parent 8bc6819 commit 5c74d7f

File tree

6 files changed

+49
-35
lines changed

6 files changed

+49
-35
lines changed

src/superannotate/lib/app/interface/sdk_interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3656,7 +3656,7 @@ def validate_annotations(
36563656
"""
36573657
with open(annotations_json) as file:
36583658
annotation_data = json.loads(file.read())
3659-
response = controller.validate_annotations(project_type, annotation_data)
3659+
response = controller.validate_annotations(project_type, annotation_data, allow_extra=False)
36603660
if response.errors:
36613661
raise AppException(response.errors)
36623662
is_valid, _ = response.data

src/superannotate/lib/core/entities/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ def enum_error_handling(self) -> str:
2727

2828

2929
class BaseModel(PyDanticBaseModel):
30+
3031
class Config:
32+
# extra = "forbid"
3133
use_enum_values = True
3234
error_msg_templates = {
3335
"type_error.integer": "integer type expected",

src/superannotate/lib/core/usecases/images.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3154,12 +3154,17 @@ def execute(self):
31543154

31553155
class ValidateAnnotationUseCase(BaseUseCase):
31563156
def __init__(
3157-
self, project_type: str, annotation: dict, validators: BaseAnnotationValidator
3157+
self,
3158+
project_type: str,
3159+
annotation: dict,
3160+
validators: BaseAnnotationValidator,
3161+
allow_extra: bool = True
31583162
):
31593163
super().__init__()
31603164
self._project_type = project_type
31613165
self._annotation = annotation
31623166
self._validators = validators
3167+
self._allow_extra = allow_extra
31633168

31643169
def execute(self) -> Response:
31653170
validator = None
@@ -3172,7 +3177,7 @@ def execute(self) -> Response:
31723177
elif self._project_type.lower() == constances.ProjectType.DOCUMENT.name.lower():
31733178
validator = self._validators.get_document_validator()
31743179
if validator:
3175-
validator = validator(self._annotation)
3180+
validator = validator(self._annotation, allow_extra=self._allow_extra)
31763181
if validator.is_valid():
31773182
self._response.data = True, validator.data
31783183
else:

src/superannotate/lib/core/validators.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1+
import copy
12
from abc import ABCMeta
23
from abc import abstractmethod
34
from typing import Any
45
from typing import Type
56

67
from pydantic import BaseModel
8+
from pydantic import Extra
79

810

911
class BaseValidator(metaclass=ABCMeta):
1012
MODEL: BaseModel()
1113

12-
def __init__(self, data: Any):
14+
def __init__(self, data: Any, allow_extra: bool = True):
1315
self.data = data
1416
self._validation_output = None
17+
self._extra = Extra.ignore if allow_extra else Extra.forbid
1518

1619
@classmethod
17-
def validate(cls, data: Any):
20+
def validate(cls, data: Any, extra=True):
1821
return cls.MODEL(**data)
1922

2023
def _validate(self):
21-
self.data = self.validate(self.data).dict(by_alias=True, exclude_none=True)
24+
model = copy.deepcopy(self.MODEL)
25+
model.Config.extra = self._extra
26+
self.data = model(**self.data).dict(by_alias=True, exclude_none=True)
2227

2328
@abstractmethod
2429
def is_valid(self) -> bool:

src/superannotate/lib/infrastructure/controller.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,8 +1642,10 @@ def delete_annotations(
16421642
)
16431643
return use_case.execute()
16441644

1645-
def validate_annotations(self, project_type: str, annotation: dict):
1645+
def validate_annotations(self, project_type: str, annotation: dict, allow_extra: bool = False):
16461646
use_case = usecases.ValidateAnnotationUseCase(
1647-
project_type, annotation, validators=self.annotation_validators
1647+
project_type, annotation,
1648+
validators=self.annotation_validators,
1649+
allow_extra=allow_extra
16481650
)
16491651
return use_case.execute()

tests/unit/test_validators.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
"width": 1024,
1717
"height": 683,
1818
"status": "Completed",
19-
"pinned": false,
20-
"isPredicted": null,
21-
"projectId": null,
22-
"annotatorEmail": null,
23-
"qaEmail": null
19+
"pinned": False,
20+
"isPredicted": None,
21+
"projectId": None,
22+
"annotatorEmail": None,
23+
"qaEmail": None
2424
},
2525
"instances": [
2626
{
@@ -35,8 +35,8 @@
3535
},
3636
"groupId": 0,
3737
"pointLabels": {},
38-
"locked": false,
39-
"visible": false,
38+
"locked": False,
39+
"visible": False,
4040
"attributes": [
4141
{
4242
"id": 117845,
@@ -46,12 +46,12 @@
4646
}
4747
],
4848
"trackingId": "aaa97f80c9e54a5f2dc2e920fc92e5033d9af45b",
49-
"error": null,
50-
"createdAt": null,
51-
"createdBy": null,
52-
"creationType": null,
53-
"updatedAt": null,
54-
"updatedBy": null,
49+
"error": None,
50+
"createdAt": None,
51+
"createdBy": None,
52+
"creationType": None,
53+
"updatedAt": None,
54+
"updatedBy": None,
5555
"className": "Personal vehicle"
5656
}
5757
]
@@ -112,11 +112,11 @@ class TestTypeHandling(TestCase):
112112
"width": 1024,
113113
"height": 683,
114114
"status": "Completed",
115-
"pinned": false,
116-
"isPredicted": null,
117-
"projectId": null,
118-
"annotatorEmail": null,
119-
"qaEmail": null
115+
"pinned": False,
116+
"isPredicted": None,
117+
"projectId": None,
118+
"annotatorEmail": None,
119+
"qaEmail": None
120120
},
121121
"instances": [
122122
{
@@ -131,8 +131,8 @@ class TestTypeHandling(TestCase):
131131
},
132132
"groupId": 0,
133133
"pointLabels": {},
134-
"locked": false,
135-
"visible": false,
134+
"locked": False,
135+
"visible": False,
136136
"attributes": [
137137
{
138138
"id": 117845,
@@ -142,12 +142,12 @@ class TestTypeHandling(TestCase):
142142
}
143143
],
144144
"trackingId": "aaa97f80c9e54a5f2dc2e920fc92e5033d9af45b",
145-
"error": null,
146-
"createdAt": null,
147-
"createdBy": null,
148-
"creationType": null,
149-
"updatedAt": null,
150-
"updatedBy": null,
145+
"error": None,
146+
"createdAt": None,
147+
"createdBy": None,
148+
"creationType": None,
149+
"updatedAt": None,
150+
"updatedBy": None,
151151
"className": "Personal vehicle"
152152
}
153153
]
@@ -171,4 +171,4 @@ def test_validate_annotation_with_wrong_bbox(self):
171171
"instances[0]invalidtype,validtypesisbbox,"
172172
"template,cuboid,polygon,point,polyline,ellipse,rbbox",
173173
out.getvalue().strip().replace(" ", "")
174-
)
174+
)

0 commit comments

Comments
 (0)