How to use the transonic.typing.format_type_as_backend_type function in transonic

To help you get started, we’ve selected a few transonic 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 fluiddyn / transonic / transonic / backends / base_jit.py View on Github external
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)
github fluiddyn / transonic / transonic / backends / typing.py View on Github external
def make_tuple_code(self, types, **kwargs):
        strings = [
            format_type_as_backend_type(type_, self, **kwargs) for type_ in types
        ]
        return f"({', '.join(strings)})"
github fluiddyn / transonic / transonic / backends / cython.py View on Github external
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
github fluiddyn / transonic / transonic / signatures.py View on Github external
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
github fluiddyn / transonic / transonic / backends / cython.py View on Github external
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)
github fluiddyn / transonic / transonic / backends / typing.py View on Github external
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"
github fluiddyn / transonic / transonic / signatures.py View on Github external
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()):