Skip to content

Code duplication and lack of error checking in get_type() #165

Description

@bfolie

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions