Skip to content

__get_dtype fails when type is list #205

@beasteers

Description

@beasteers

This is relevant for scaper's schema e.g.:

    "pitch_shift": {
      "type": ["number", "null"]
    },

It should be handled similar to oneOf:

def __get_dtype(typespec):
    '''Get the dtype associated with a jsonschema type definition
    Parameters
    ----------
    typespec : dict
        The schema definition
    Returns
    -------
    dtype : numpy.dtype
        The associated dtype
    '''

    if 'type' in typespec:
        if isinstance(typespec['type'], (list, tuple)):
            # get dtype for each type in list
            types = [__TYPE_MAP__.get(t, np.object_) for t in typespec['type']]

            # If they're not all equal, return object
            if all([t == types[0] for t in types]):
                return types[0]
            return np.object_
        else:
            return __TYPE_MAP__.get(typespec['type'], np.object_)

    elif 'enum' in typespec:
        # Enums map to objects
        return np.object_

    elif 'oneOf' in typespec:
        # Recurse
        types = [__get_dtype(v) for v in typespec['oneOf']]

        # If they're not all equal, return object
        if all([t == types[0] for t in types]):
            return types[0]

    return np.object_

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