Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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))
@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 (
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):
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)
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)