Skip to content

Commit d4d9896

Browse files
committed
Refactored FeatureSchemaAttribute to be a dataclass
1 parent 6082463 commit d4d9896

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

libs/labelbox/src/labelbox/schema/ontology.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
from labelbox.schema.tool_building.tool_type_mapping import (
2626
map_tool_type_to_tool_cls,
2727
)
28-
from labelbox.schema.tool_building.types import FeatureSchemaAttributes
28+
from labelbox.schema.tool_building.types import (
29+
FeatureSchemaAttribute,
30+
FeatureSchemaAttributes,
31+
)
2932
import warnings
3033

3134

@@ -119,7 +122,15 @@ def from_dict(cls, dictionary: Dict[str, Any]) -> Dict[str, Any]:
119122
for c in dictionary["classifications"]
120123
],
121124
color=dictionary["color"],
122-
attributes=dictionary.get("attributes", None),
125+
attributes=[
126+
FeatureSchemaAttribute(
127+
attributeName=attr["attributeName"],
128+
attributeValue=attr["attributeValue"],
129+
)
130+
for attr in dictionary.get("attributes", []) or []
131+
]
132+
if dictionary.get("attributes")
133+
else None,
123134
)
124135

125136
def asdict(self) -> Dict[str, Any]:
@@ -133,7 +144,9 @@ def asdict(self) -> Dict[str, Any]:
133144
],
134145
"schemaNodeId": self.schema_id,
135146
"featureSchemaId": self.feature_schema_id,
136-
"attributes": self.attributes,
147+
"attributes": [a.asdict() for a in self.attributes]
148+
if self.attributes is not None
149+
else None,
137150
}
138151

139152
def add_classification(self, classification: Classification) -> None:

libs/labelbox/src/labelbox/schema/tool_building/classification.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from labelbox.schema.tool_building.types import (
99
FeatureSchemaId,
1010
FeatureSchemaAttributes,
11+
FeatureSchemaAttribute,
1112
)
1213

1314

@@ -112,7 +113,15 @@ def from_dict(cls, dictionary: Dict[str, Any]) -> "Classification":
112113
schema_id=dictionary.get("schemaNodeId", None),
113114
feature_schema_id=dictionary.get("featureSchemaId", None),
114115
scope=cls.Scope(dictionary.get("scope", cls.Scope.GLOBAL)),
115-
attributes=dictionary.get("attributes", None),
116+
attributes=[
117+
FeatureSchemaAttribute(
118+
attributeName=attr["attributeName"],
119+
attributeValue=attr["attributeValue"],
120+
)
121+
for attr in dictionary.get("attributes", []) or []
122+
]
123+
if dictionary.get("attributes")
124+
else None,
116125
)
117126

118127
def asdict(self, is_subclass: bool = False) -> Dict[str, Any]:
@@ -128,7 +137,7 @@ def asdict(self, is_subclass: bool = False) -> Dict[str, Any]:
128137
"options": [o.asdict() for o in self.options],
129138
"schemaNodeId": self.schema_id,
130139
"featureSchemaId": self.feature_schema_id,
131-
"attributes": self.attributes
140+
"attributes": [a.asdict() for a in self.attributes]
132141
if self.attributes is not None
133142
else None,
134143
}

libs/labelbox/src/labelbox/schema/tool_building/types.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@
33
from typing import TypedDict
44

55

6-
class FeatureSchemaAttribute(TypedDict):
6+
from dataclasses import dataclass
7+
8+
9+
@dataclass
10+
class FeatureSchemaAttribute:
711
attributeName: str
812
attributeValue: str
913

14+
def asdict(self):
15+
return {
16+
"attributeName": self.attributeName,
17+
"attributeValue": self.attributeValue,
18+
}
19+
1020

11-
FeatureSchemaAttriubte = Annotated[FeatureSchemaAttribute, Field()]
21+
FeatureSchemaAttribute = Annotated[FeatureSchemaAttribute, Field()]
1222

1323
FeatureSchemaId = Annotated[str, Field(min_length=25, max_length=25)]
1424
SchemaId = Annotated[str, Field(min_length=25, max_length=25)]

libs/labelbox/tests/unit/test_unit_ontology.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@
3636
"color": "#FF0000",
3737
"tool": "rectangle",
3838
"attributes": [
39-
FeatureSchemaAttribute(
40-
attriubteName="auto-ocr", attributeValue="true"
41-
)
39+
{
40+
"attributeName": "auto-ocr",
41+
"attributeValue": "true",
42+
}
4243
],
4344
"classifications": [
4445
{
@@ -77,10 +78,10 @@
7778
},
7879
],
7980
"attributes": [
80-
FeatureSchemaAttribute(
81-
attributeName="requires-connection",
82-
attributeValue="true",
83-
)
81+
{
82+
"attributeName": "requires-connection",
83+
"attributeValue": "true",
84+
}
8485
],
8586
},
8687
{

0 commit comments

Comments
 (0)