-
Notifications
You must be signed in to change notification settings - Fork 7
Description
All PolymorphicSerializable implement a get_type(data) class method that returns the underlying type given serialized data. In general, data has some type field with the name of the class, which is matched against a known list of types. Here are two different example implementations:
@classmethod
def get_type(cls, data) -> Type['Processor']:
return {
'Grid': GridProcessor,
'Enumerated': EnumeratedProcessor
}[data['config']['type']]
@classmethod
def get_type(cls, data) -> Type['Predictor']:
type_dict = {
"Simple": SimpleMLPredictor
}
typ = type_dict.get(data['config']['type'])
if typ is not None:
return typ
else:
raise ValueError(
'{} is not a valid predictor type. '
'Must be in {}.'.format(data['config']['type'], type_dict.keys())
)
Both implementations have a "type dictionary" and return the value in the type dictionary corresponding to a key that is pulled from data. But the first implementation does not gracefully throw an exception if the type is not in the type dictionary, and neither catches the possibility that data could be malformed (e.g., if data['config']['type'] does not exist).
Moving some of this logic and error checking to the abstract PolymorhpicSerializable class would lead to code deduplication and standardize exceptions across all implementations of PolymorphicSerializable.