Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def Tuple(of_types):
return Collection(Traits([TupleTrait(of_types), LenTrait, SliceTrait]),
Integer(), TypeVariable(), TypeVariable())
def rec_tr(t, env):
if isinstance(t, typing.TypeVar):
if t in env:
return env[t]
else:
env[t] = TypeVariable()
return env[t]
elif t is typing.Any:
return TypeVariable()
elif isinstance(t, NoneType_):
return NoneType
elif t is bool:
return Bool()
elif issubclass(t, slice):
return Slice
elif issubclass(t, (complex, complexfloating)):
return Complex()
if isinstance(t, TypeVariable):
return
if any(isinstance(tp, TypeVariable) for tp in ts):
return
if any(len(tp.types) != len(t.types) for tp in ts):
return
for i, tt in enumerate(t.types):
its = [prune(tp.types[i]) for tp in ts]
if any(isinstance(it, TypeVariable) for it in its):
continue
it0 = its[0]
it0ntypes = len(it0.types)
if all(((it.name == it0.name) and
(len(it.types) == it0ntypes))
for it in its):
ntypes = [TypeVariable() for _ in range(it0ntypes)]
new_tt = TypeOperator(it0.name, ntypes)
new_tt.__class__ = it0.__class__
unify(tt, new_tt)
try_unify(prune(tt), [prune(it) for it in its])
try_unify(b, types)
for value_type in value_types:
unify(Function([value_type], Bool()),
tr(MODULES['__builtin__']['bool']))
return_type = TypeVariable()
prev_type = value_types[0]
for value_type in value_types[1:]:
unify(Function([prev_type, value_type], return_type), op_type)
prev_type = value_type
return return_type
elif isinstance(node, (gast.And, gast.Or)):
x_type = TypeVariable()
return MultiType([
Function([x_type, x_type], x_type),
Function([TypeVariable(), TypeVariable()], TypeVariable()),
])
raise RuntimeError("Unhandled syntax node {0}".format(type(node)))
def Iterable(of_type, dim):
return Collection(Traits([TypeVariable(), LenTrait, SliceTrait]),
AnyType,
AnyType,
Iterable(of_type, dim - 1) if dim > 1 else of_type)
def check_specs(specs, types):
'''
Does nothing but raising PythranSyntaxError if specs
are incompatible with the actual code
'''
from pythran.types.tog import unify, clone, tr
from pythran.types.tog import Function, TypeVariable, InferenceError
for fname, signatures in specs.functions.items():
ftype = types[fname]
for signature in signatures:
sig_type = Function([tr(p) for p in signature], TypeVariable())
try:
unify(clone(sig_type), clone(ftype))
except InferenceError:
raise PythranSyntaxError(
"Specification for `{}` does not match inferred type:\n"
"expected `{}`\n"
"got `Callable[[{}], ...]`".format(
fname,
ftype,
", ".join(map(str, sig_type.types[:-1])))
)
def freshrec(tp):
p = prune(tp)
if isinstance(p, TypeVariable):
if is_generic(p, non_generic):
if p not in mappings:
mappings[p] = TypeVariable()
return mappings[p]
else:
return p
elif isinstance(p, dict):
return p # module
elif isinstance(p, Collection):
return Collection(*[freshrec(x) for x in p.types])
elif isinstance(p, Scalar):
return Scalar([freshrec(x) for x in p.types])
elif isinstance(p, TypeOperator):
return TypeOperator(p.name, [freshrec(x) for x in p.types])
elif isinstance(p, MultiType):
return MultiType([freshrec(x) for x in p.types])
else:
assert False, "missing freshrec case {}".format(type(p))
return Tuple([rec_tr(tp, env) for tp in t.__args__])
elif isinstance(t, typing.NDArray):
return Array(rec_tr(t.__args__[0], env), len(t.__args__[1:]))
elif isinstance(t, typing.Pointer):
return Array(rec_tr(t.__args__[0], env), 1)
elif isinstance(t, typing.Union):
return MultiType([rec_tr(ut, env) for ut in t.__args__])
elif t is typing.File:
return File()
elif isinstance(t, typing.Iterable):
return Collection(TypeVariable(), TypeVariable(), TypeVariable(),
rec_tr(t.__args__[0], env))
elif t is typing.Sized:
return Collection(
Traits([TypeVariable(), LenTrait, TypeVariable()]),
TypeVariable(), TypeVariable(), TypeVariable()
)
elif isinstance(t, typing.Fun):
return Function([rec_tr(at, env) for at in t.__args__[:-1]],
rec_tr(t.__args__[-1], env))
else:
raise NotImplementedError(t)
new_type = ExceptionType
non_generic.add(new_type)
if node.name.id in env:
unify(env[node.name.id], new_type)
else:
env[node.name.id] = new_type
analyse_body(node.body, env, non_generic)
return env
elif isinstance(node, gast.Assert):
if node.msg:
analyse(node.msg, env, non_generic)
analyse(node.test, env, non_generic)
return env
elif isinstance(node, gast.UnaryOp):
operand_type = analyse(node.operand, env, non_generic)
return_type = TypeVariable()
op_type = analyse(node.op, env, non_generic)
unify(Function([operand_type], return_type), op_type)
return return_type
elif isinstance(node, gast.Invert):
return MultiType([Function([Bool()], Integer()),
Function([Integer()], Integer())])
elif isinstance(node, gast.Not):
return tr(MODULES['__builtin__']['bool'])
elif isinstance(node, gast.BoolOp):
op_type = analyse(node.op, env, non_generic)
value_types = [analyse(value, env, non_generic)
for value in node.values]
for value_type in value_types:
unify(Function([value_type], Bool()),
tr(MODULES['__builtin__']['bool']))