Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return 'Array[{}d, {}]'.format(n, t)
if type_trait == 'gen':
return 'Generator[{}]'.format(self.types[2])
return super(Collection, self).__str__()
def TupleTrait(of_types):
return TypeOperator('tuple', of_types)
ListTrait = TypeOperator('list', [])
SetTrait = TypeOperator('set', [])
DictTrait = TypeOperator('dict', [])
StrTrait = TypeOperator('str', [])
FileTrait = TypeOperator('file', [])
ArrayTrait = TypeOperator('array', [])
GenerableTrait = TypeOperator('gen', [])
LenTrait = TypeOperator("len", [])
NoLenTrait = TypeOperator("no_len", [])
SliceTrait = TypeOperator("slice", [])
NoSliceTrait = TypeOperator("no_slice", [])
def File():
return Collection(Traits([FileTrait, NoLenTrait, NoSliceTrait]),
InvalidKey, Str(), Str())
def List(of_type):
return Collection(Traits([ListTrait, LenTrait, SliceTrait]),
Integer(), of_type, of_type)
def TupleTrait(of_types):
return TypeOperator('tuple', of_types)
ListTrait = TypeOperator('list', [])
SetTrait = TypeOperator('set', [])
DictTrait = TypeOperator('dict', [])
StrTrait = TypeOperator('str', [])
FileTrait = TypeOperator('file', [])
ArrayTrait = TypeOperator('array', [])
GenerableTrait = TypeOperator('gen', [])
LenTrait = TypeOperator("len", [])
NoLenTrait = TypeOperator("no_len", [])
SliceTrait = TypeOperator("slice", [])
NoSliceTrait = TypeOperator("no_slice", [])
def File():
return Collection(Traits([FileTrait, NoLenTrait, NoSliceTrait]),
InvalidKey, Str(), Str())
def List(of_type):
return Collection(Traits([ListTrait, LenTrait, SliceTrait]),
Integer(), of_type, of_type)
def Set(of_type):
return Collection(Traits([SetTrait, LenTrait, NoSliceTrait]),
InvalidKey, of_type, of_type)
def TupleTrait(of_types):
return TypeOperator('tuple', of_types)
if type_trait == 'gen':
return 'Generator[{}]'.format(self.types[2])
return super(Collection, self).__str__()
def TupleTrait(of_types):
return TypeOperator('tuple', of_types)
ListTrait = TypeOperator('list', [])
SetTrait = TypeOperator('set', [])
DictTrait = TypeOperator('dict', [])
StrTrait = TypeOperator('str', [])
FileTrait = TypeOperator('file', [])
ArrayTrait = TypeOperator('array', [])
GenerableTrait = TypeOperator('gen', [])
LenTrait = TypeOperator("len", [])
NoLenTrait = TypeOperator("no_len", [])
SliceTrait = TypeOperator("slice", [])
NoSliceTrait = TypeOperator("no_slice", [])
def File():
return Collection(Traits([FileTrait, NoLenTrait, NoSliceTrait]),
InvalidKey, Str(), Str())
def List(of_type):
return Collection(Traits([ListTrait, LenTrait, SliceTrait]),
Integer(), of_type, of_type)
return super(Collection, self).__str__()
def TupleTrait(of_types):
return TypeOperator('tuple', of_types)
ListTrait = TypeOperator('list', [])
SetTrait = TypeOperator('set', [])
DictTrait = TypeOperator('dict', [])
StrTrait = TypeOperator('str', [])
FileTrait = TypeOperator('file', [])
ArrayTrait = TypeOperator('array', [])
GenerableTrait = TypeOperator('gen', [])
LenTrait = TypeOperator("len", [])
NoLenTrait = TypeOperator("no_len", [])
SliceTrait = TypeOperator("slice", [])
NoSliceTrait = TypeOperator("no_slice", [])
def File():
return Collection(Traits([FileTrait, NoLenTrait, NoSliceTrait]),
InvalidKey, Str(), Str())
def List(of_type):
return Collection(Traits([ListTrait, LenTrait, SliceTrait]),
Integer(), of_type, of_type)
def Set(of_type):
"""A binary type constructor which builds function types"""
return TypeOperator('fun', list(from_types) + [to_type])
def OptionType(of_type):
return TypeOperator("option", [of_type])
def Traits(of_types):
return TypeOperator("traits", of_types)
ExceptionType = TypeOperator("exception", [])
# Basic types are constructed with a null type constructor
IntegerTrait = TypeOperator("int", []) # any integer
FloatTrait = TypeOperator("float", []) # any float
ComplexTrait = TypeOperator("complex", [])
BoolTrait = TypeOperator("bool", [])
InvalidKey = TypeOperator("invalid-key", []) # for non-indexable collection
NoneType = TypeOperator("none", [])
AnyType = TypeOperator("any", [])
InvalidType = TypeOperator("invalid-type", [])
Slice = TypeOperator("slice", []) # slice
def is_none(t):
pt = prune(t)
return isinstance(pt, TypeOperator) and pt.name == "none"
def is_option_type(t):
def Traits(of_types):
return TypeOperator("traits", of_types)
ExceptionType = TypeOperator("exception", [])
# Basic types are constructed with a null type constructor
IntegerTrait = TypeOperator("int", []) # any integer
FloatTrait = TypeOperator("float", []) # any float
ComplexTrait = TypeOperator("complex", [])
BoolTrait = TypeOperator("bool", [])
InvalidKey = TypeOperator("invalid-key", []) # for non-indexable collection
NoneType = TypeOperator("none", [])
AnyType = TypeOperator("any", [])
InvalidType = TypeOperator("invalid-type", [])
Slice = TypeOperator("slice", []) # slice
def is_none(t):
pt = prune(t)
return isinstance(pt, TypeOperator) and pt.name == "none"
def is_option_type(t):
pt = prune(t)
return isinstance(pt, TypeOperator) and pt.name == "option"
def maybe_array_type(t):
pt = prune(t)
def Function(from_types, to_type):
"""A binary type constructor which builds function types"""
return TypeOperator('fun', list(from_types) + [to_type])
def __str__(self):
num_types = len(self.types)
if num_types == 0:
return self.name
elif self.name == 'fun':
return 'Callable[[{0}], {1}]'.format(
', '.join(map(str, self.types[:-1])), self.types[-1])
elif self.name == 'option':
return 'Option[{0}]'.format(self.types[0])
else:
return "{0}[{1}]" .format(self.name.capitalize(),
', '.join(map(str, self.types)))
class Collection(TypeOperator):
def __init__(self, holder_type, key_type, value_type, iter_type):
super(Collection, self).__init__("collection",
[holder_type, key_type, value_type,
iter_type])
def __str__(self):
t0 = prune(self.types[0])
if isinstance(t0, TypeVariable):
if isinstance(prune(self.types[1]), TypeVariable):
return 'Iterable[{}]'.format(self.types[3])
else:
return 'Collection[{}, {}]'.format(self.types[1],
self.types[2])
if isinstance(t0, TypeOperator) and t0.name == 'traits':
if all(isinstance(prune(t), TypeVariable) for t in t0.types):
def OptionType(of_type):
return TypeOperator("option", [of_type])
def Traits(of_types):
return TypeOperator("traits", of_types)
ExceptionType = TypeOperator("exception", [])
# Basic types are constructed with a null type constructor
IntegerTrait = TypeOperator("int", []) # any integer
FloatTrait = TypeOperator("float", []) # any float
ComplexTrait = TypeOperator("complex", [])
BoolTrait = TypeOperator("bool", [])
InvalidKey = TypeOperator("invalid-key", []) # for non-indexable collection
NoneType = TypeOperator("none", [])
AnyType = TypeOperator("any", [])
InvalidType = TypeOperator("invalid-type", [])
Slice = TypeOperator("slice", []) # slice
def is_none(t):
pt = prune(t)
return isinstance(pt, TypeOperator) and pt.name == "none"
def is_option_type(t):
pt = prune(t)
return isinstance(pt, TypeOperator) and pt.name == "option"