How to use the runtype.__init__.Iterable 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
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)


class Tuple(Iterable):

    def __init__(self, item):
        super(Tuple, self).__init__(item, iterable_type=tuple)


class Set(Iterable):

    def __init__(self, item):
        super(Set, self).__init__(item, iterable_type=set)


def is_type(value, argtype):
    """
    Like isinstance, but also supports runtype's Or and And psuedotypes.
    """
    if isinstance(argtype, BaseType):
github madisonmay / Runtype / runtype / __init__.py View on Github external
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)


class Tuple(Iterable):

    def __init__(self, item):
        super(Tuple, self).__init__(item, iterable_type=tuple)


class Set(Iterable):

    def __init__(self, item):
        super(Set, self).__init__(item, iterable_type=set)
github madisonmay / Runtype / runtype / __init__.py View on Github external
)


class List(Iterable):

    def __init__(self, item):
        super(List, self).__init__(item, iterable_type=list)


class Tuple(Iterable):

    def __init__(self, item):
        super(Tuple, self).__init__(item, iterable_type=tuple)


class Set(Iterable):

    def __init__(self, item):
        super(Set, self).__init__(item, iterable_type=set)


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)