Skip to content

Commit ad9fe85

Browse files
authored
Merge pull request #556 from Labelbox/jt/al-2296
[AL-2296] Remove Scope key when turning Nested Classification to dict
2 parents ccecf77 + a1d3a61 commit ad9fe85

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

labelbox/schema/ontology.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def asdict(self) -> Dict[str, Any]:
6969
"featureSchemaId": self.feature_schema_id,
7070
"label": self.label,
7171
"value": self.value,
72-
"options": [o.asdict() for o in self.options]
72+
"options": [o.asdict(is_subclass=True) for o in self.options]
7373
}
7474

7575
def add_option(self, option: 'Classification') -> None:
@@ -159,29 +159,25 @@ def from_dict(cls, dictionary: Dict[str, Any]) -> Dict[str, Any]:
159159
feature_schema_id=dictionary.get("featureSchemaId", None),
160160
scope=cls.Scope(dictionary.get("scope", cls.Scope.GLOBAL)))
161161

162-
def asdict(self) -> Dict[str, Any]:
162+
def asdict(self, is_subclass: bool = False) -> Dict[str, Any]:
163163
if self.class_type in self._REQUIRES_OPTIONS \
164164
and len(self.options) < 1:
165165
raise InconsistentOntologyException(
166166
f"Classification '{self.instructions}' requires options.")
167-
return {
168-
"type":
169-
self.class_type.value,
170-
"instructions":
171-
self.instructions,
172-
"name":
173-
self.name,
174-
"required":
175-
self.required,
167+
classification = {
168+
"type": self.class_type.value,
169+
"instructions": self.instructions,
170+
"name": self.name,
171+
"required": self.required,
176172
"options": [o.asdict() for o in self.options],
177-
"schemaNodeId":
178-
self.schema_id,
179-
"featureSchemaId":
180-
self.feature_schema_id,
181-
"scope":
182-
self.scope.value
183-
if self.scope is not None else self.Scope.GLOBAL.value
173+
"schemaNodeId": self.schema_id,
174+
"featureSchemaId": self.feature_schema_id
184175
}
176+
if is_subclass:
177+
return classification
178+
classification[
179+
"scope"] = self.scope.value if self.scope is not None else self.Scope.GLOBAL.value
180+
return classification
185181

186182
def add_option(self, option: Option) -> None:
187183
if option.value in (o.value for o in self.options):
@@ -258,7 +254,9 @@ def asdict(self) -> Dict[str, Any]:
258254
"name": self.name,
259255
"required": self.required,
260256
"color": self.color,
261-
"classifications": [c.asdict() for c in self.classifications],
257+
"classifications": [
258+
c.asdict(is_subclass=True) for c in self.classifications
259+
],
262260
"schemaNodeId": self.schema_id,
263261
"featureSchemaId": self.feature_schema_id
264262
}

tests/unit/test_unit_ontology.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
"nested classification",
4747
"type":
4848
"radio",
49-
"scope":
50-
"global",
5149
"options": [{
5250
"schemaNodeId":
5351
None,
@@ -64,7 +62,6 @@
6462
"instructions": "nested nested text",
6563
"name": "nested nested text",
6664
"type": "text",
67-
"scope": "global",
6865
"options": []
6966
}]
7067
}, {
@@ -81,7 +78,6 @@
8178
"instructions": "nested text",
8279
"name": "nested text",
8380
"type": "text",
84-
"scope": "global",
8581
"options": []
8682
}]
8783
}, {
@@ -250,6 +246,5 @@ def test_option_add_option() -> None:
250246

251247

252248
def test_ontology_asdict() -> None:
253-
print(OntologyBuilder.from_dict(_SAMPLE_ONTOLOGY))
254249
assert OntologyBuilder.from_dict(
255250
_SAMPLE_ONTOLOGY).asdict() == _SAMPLE_ONTOLOGY

0 commit comments

Comments
 (0)