44
55from ...annotation_types .annotation import ClassificationAnnotation
66from ...annotation_types .classification import Checklist , ClassificationAnswer , Radio , Text , Dropdown
7+ from ...annotation_types .types import Cuid
78from .feature import LBV1Feature
89
910
@@ -24,7 +25,7 @@ def to_common(self):
2425 }))
2526
2627 @classmethod
27- def from_common (cls , radio : Radio , schema_id : str , ** extra ) -> "LBV1Radio" :
28+ def from_common (cls , radio : Radio , schema_id : Cuid , ** extra ) -> "LBV1Radio" :
2829 return cls (schema_id = schema_id ,
2930 answer = LBV1ClassificationAnswer (
3031 schema_id = radio .answer .schema_id ,
@@ -48,7 +49,7 @@ def to_common(self):
4849 ])
4950
5051 @classmethod
51- def from_common (cls , checklist : Checklist , schema_id : str ,
52+ def from_common (cls , checklist : Checklist , schema_id : Cuid ,
5253 ** extra ) -> "LBV1Checklist" :
5354 return cls (schema_id = schema_id ,
5455 answers = [
@@ -69,22 +70,14 @@ def to_common(self):
6970 return Text (answer = self .answer )
7071
7172 @classmethod
72- def from_common (cls , text : Text , schema_id : str , ** extra ) -> "LBV1Text" :
73+ def from_common (cls , text : Text , schema_id : Cuid , ** extra ) -> "LBV1Text" :
7374 return cls (schema_id = schema_id , answer = text .answer , ** extra )
7475
7576
76- classification_mapping = {
77- Text : LBV1Text ,
78- Dropdown : LBV1Checklist ,
79- Checklist : LBV1Checklist ,
80- Radio : LBV1Radio
81- }
82-
83-
8477class LBV1Classifications (BaseModel ):
8578 classifications : List [Union [LBV1Radio , LBV1Checklist , LBV1Text ]] = []
8679
87- def to_common (self ):
80+ def to_common (self ) -> List [ ClassificationAnnotation ] :
8881 classifications = [
8982 ClassificationAnnotation (value = classification .to_common (),
9083 classifications = [],
@@ -103,7 +96,7 @@ def from_common(
10396 ) -> "LBV1Classifications" :
10497 classifications = []
10598 for annotation in annotations :
106- classification = classification_mapping . get ( type ( annotation . value ) )
99+ classification = cls . lookup_classification ( annotation )
107100 if classification is not None :
108101 classifications .append (
109102 classification .from_common (annotation .value ,
@@ -112,3 +105,14 @@ def from_common(
112105 else :
113106 raise TypeError (f"Unexpected type { type (annotation .value )} " )
114107 return cls (classifications = classifications )
108+
109+ @staticmethod
110+ def lookup_classification (
111+ annotation : ClassificationAnnotation
112+ ) -> Union [LBV1Text , LBV1Checklist , LBV1Radio ]:
113+ return {
114+ Text : LBV1Text ,
115+ Dropdown : LBV1Checklist ,
116+ Checklist : LBV1Checklist ,
117+ Radio : LBV1Radio
118+ }.get (type (annotation .value ))
0 commit comments