Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def compute_typename_from_object(self, obj: object):
"""return the backend type name"""
transonic_type = typeof(obj)
return format_type_as_backend_type(transonic_type, self.type_formatter)
def make_tuple_code(self, types, **kwargs):
strings = [
format_type_as_backend_type(type_, self, **kwargs) for type_ in types
]
return f"({', '.join(strings)})"
name_start = Name("*", ast.Param())
fdef.args.defaults = [name_start] * len(fdef.args.defaults)
for name in fdef.args.args:
name.annotation = None
if annotations:
ttype = annotations[name.id]
name_cython_type = get_name_cython_type(ttype)
else:
name_cython_type = "object"
name.id = f"{name_cython_type} {name.id}"
if locals_types is not None and locals_types:
# note: np.ndarray not supported by Cython in "locals"
# TODO: thus, fused types not supported here
locals_types = ", ".join(
f"{k}={format_type_as_backend_type(v, self.type_formatter, memview=True)}"
for k, v in locals_types.items()
)
signatures_func.append(f"@cython.locals({locals_types})")
if returns is not None:
ttype = returns
name_cython_type = get_name_cython_type(ttype)
returns = name_cython_type + " "
else:
returns = ""
def_keyword = "cpdef"
signatures_func.append(
f"{def_keyword} {inline}{returns}{unparse(fdef).strip()[4:-1]}\n"
)
return signatures_func
def _format_types_as_backend_types(types, backend_type_formatter, **kwargs):
"""Compute a list of Pythran/Cython/... types
"""
backend_types = []
for type_ in types:
backend_types.append(
format_type_as_backend_type(type_, backend_type_formatter, **kwargs)
)
# TODO: handle this with an exception
if "_empty" in backend_types:
raise ValueError(
"At least one annotation type lacking in a signature.\n"
f"types = {types}"
)
return backend_types
def get_name_cython_type(ttype):
ttype_name = get_ttype_name(ttype)
name_cython_type = f"__{fdef.name}__{ttype_name}"
if name_cython_type in cython_fused_types:
return name_cython_type
return format_type_as_backend_type(ttype, self.type_formatter)
def make_dict_code(self, type_keys, type_values, **kwargs):
key = format_type_as_backend_type(type_keys, self, **kwargs)
value = format_type_as_backend_type(type_values, self, **kwargs)
return f"{key}: {value} dict"
type_ = str2type(type_)
types.append(type_)
template_parameters = set()
for type_ in types:
if hasattr(type_, "get_template_parameters"):
template_parameters.update(type_.get_template_parameters())
if not template_parameters:
if "_empty" in types:
raise ValueError(
"At least one annotation type lacking in a signature.\n"
f"types = {types}"
)
str_types = [
format_type_as_backend_type(type_, backend_type_formatter)
for type_ in types
]
return (str_types,)
if not all(param.values for param in template_parameters):
raise ValueError(
f"{template_parameters}, {[param.values for param in template_parameters]}"
)
values_template_parameters = {}
for param in template_parameters:
values_template_parameters[param.__name__] = param.values
backend_types = []
names = values_template_parameters.keys()
for set_types in itertools.product(*values_template_parameters.values()):