How to use the runtype.__init__.BaseType function in runtype

To help you get started, we’ve selected a few runtype examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github madisonmay / Runtype / runtype / __init__.py View on Github external
except Exception:
                    raise ReturnTypeError(return_type, return_value)
            else:
                raise ReturnTypeError(return_type, return_value)
        return return_value
    return typed_fn


class BaseType(object):

    @abstractmethod
    def validate(self):
        raise NotImplementedError


class Any(BaseType):

    def __init__(self, *types):
        self.types = types

    def validate(self, value):
        return any((isinstance(value, _type) for _type in self.types))


class All(BaseType):

    def __init__(self, *types):
        self.types = types

    def validate(self, value):
        return all((isinstance(value, _type) for _type in self.types))
github madisonmay / Runtype / runtype / __init__.py View on Github external
    @abstractmethod
    def validate(self):
        raise NotImplementedError


class Any(BaseType):

    def __init__(self, *types):
        self.types = types

    def validate(self, value):
        return any((isinstance(value, _type) for _type in self.types))


class All(BaseType):

    def __init__(self, *types):
        self.types = types

    def validate(self, value):
        return all((isinstance(value, _type) for _type in self.types))


class Dict(BaseType):

    def __init__(self, key, value):
        self.key_type = key
        self.value_type = value

    def validate(self, value):
        return (
github madisonmay / Runtype / runtype / __init__.py View on Github external
self.types = types

    def validate(self, value):
        return any((isinstance(value, _type) for _type in self.types))


class All(BaseType):

    def __init__(self, *types):
        self.types = types

    def validate(self, value):
        return all((isinstance(value, _type) for _type in self.types))


class Dict(BaseType):

    def __init__(self, key, value):
        self.key_type = key
        self.value_type = value

    def validate(self, value):
        return (
            isinstance(value, dict) and
            all([is_type(k, self.key_type) for k in value.iterkeys()]) and
            all([is_type(v, self.value_type) for v in value.itervalues()])
        )


class Iterable(BaseType):

    def __init__(self, item, iterable_type=IterableType):
github madisonmay / Runtype / runtype / __init__.py View on Github external
class Dict(BaseType):

    def __init__(self, key, value):
        self.key_type = key
        self.value_type = value

    def validate(self, value):
        return (
            isinstance(value, dict) and
            all([is_type(k, self.key_type) for k in value.iterkeys()]) and
            all([is_type(v, self.value_type) for v in value.itervalues()])
        )


class Iterable(BaseType):

    def __init__(self, item, iterable_type=IterableType):
        self.item_type = item
        self.iterable_type = iterable_type

    def validate(self, value):
        return (
            isinstance(value, self.iterable_type) and
            all([is_type(i, self.item_type) for i in value])
        )


class List(Iterable):

    def __init__(self, item):
        super(List, self).__init__(item, iterable_type=list)
github madisonmay / Runtype / runtype / __init__.py View on Github external
def is_type(value, argtype):
    """
    Like isinstance, but also supports runtype's Or and And psuedotypes.
    """
    if isinstance(argtype, BaseType):
        return argtype.validate(value)
    else:
        return isinstance(value, argtype)