type_inference Package

type_inference Package

deferred Module

numba.type_inference.deferred.create_deferred(type_inferer, node, deferred_cls)

Create a deferred type for an AST node

numba.type_inference.deferred.create_deferred_call(type_inferer, arg_types, call_node)

Set the ast.Call as uninferable for now

infer Module

class numba.type_inference.infer.TypeInferer(context, func, ast, closure_scope=None, **kwds)

Bases: numba.visitors.NumbaTransformer

Type inference. Initialize with a minivect context, a Python ast, and a function type with a given or absent, return type.

Infers and checks types and inserts type coercion nodes.

See transform.py for an overview of AST transformations.

add_resolved_parents(unvisited, start_points, strongly_connected)

Check for immediate resolved parents

analyse = True
analyse_assignments()

Analyze all variable assignments and phis.

assert_assignable(dst_type, src_type)
assert_index(type, node)
assert_resolveable(start_point)

Assert a type in the type graph can be resolved

assert_resolved(start_point)

Assert a type in the type graph is resolved somewhere down the line

assign(lhs_node, rhs_node, rhs_var=None)
candidates(unvisited)

Types with in-degree zero

error_unresolved_types(unvisited)

Raise an exception for a circular dependence we can’t resolve

extattr_mangle(attr_name, type)
get_resolved_type(type)
getvar(name_node)
handle_NameAssignment(assignment_node)
handle_locals(arg_types)

Process entries in the locals={...} dict

handle_phi(node)
infer_types()

Infer types for the function.

init_global(name_node)
init_locals()

Populate symbol table for local variables and constants.

initialize_argtypes(arg_types)

Initialize argument types

initialize_constants()
initialize_ssa()

Propagate argument types to first rename of the variable in the block

is_object(type)
is_resolved(t)
is_store(ctx)
is_trivial_cycle(type)

Return whether the type directly refers to itself

process_unvisited(unvisited)

Find and resolve any final reduced self-referential portions in the graph

promote(v1, v2)
promote_types(type1, type2)
promote_types_numeric(t1, t2)

Type promotion but demote objects to numeric types

remove_resolved_type(start_point)

Remove a resolved type from the type graph

resolve_variable_types()

Resolve the types for all variable assignments. We run type inference on each assignment which builds a type graph in case of dependencies. The dependencies are resolved after type inference completes.

type_from_pyval(pyval)
update_visited(start_point, visited, unvisited)
visit(node)
visit_Assign(node)
visit_Attribute(node, visitchildren=True)
visit_BinOp(node)
visit_BoolOp(node)

and/or expression

visit_Call(node, visitchildren=True)
visit_CastNode(node)
visit_ClosureScopeLoadNode(node)
visit_Compare(node)
visit_Dict(node)
visit_Ellipsis(node)
visit_ExtSlice(node)
visit_For(node)
visit_FuncDefExprNode(node)
visit_FunctionDef(node)
visit_Global(node)
visit_If(node)
visit_IfExp(node)
visit_Index(node)

Normal index

visit_List(node)
visit_MaybeUnusedNode(node)
visit_Name(node)
visit_Num(node)
visit_PhiNode(node)
visit_Return(node)
visit_Slice(node)
visit_Str(node)
visit_Subscript(node, visitchildren=True)
visit_Tuple(node)
visit_UnaryOp(node)
visit_UntypedCoercion(node)
visit_UserNode(node)
visit_While(node)
visit_With(node)
visit_booltest(node)
visit_long(value)
class numba.type_inference.infer.TypeSettingVisitor(context, func, ast, locals=None, func_signature=None, nopython=0, symtab=None, **kwargs)

Bases: numba.visitors.NumbaTransformer

Set node.type for all AST nodes after type inference from node.variable. Allows for deferred coercions (may be removed in the future).

handle_phi(node)
resolve(variable)

Resolve any resolved types, and resolve any final disconnected type graphs that haven’t been simplified. This can be the case if the type of a variable does not depend on the type of a sub-expression which may be unresolved, e.g.:

y = 0 for i in range(...):

x = int(y + 4) # y is unresolved here, so we have
# promote(deferred(y), int)

y += 1

visit(node)
visit_DeferredCoercionNode(node)

Resolve deferred coercions

visit_ExtSlice(node)
visit_FunctionDef(node)
visit_Name(node)
numba.type_inference.infer.lookup_global(env, name, position_node)
numba.type_inference.infer.no_keywords(node)

infer_call Module

numba.type_inference.infer_call.infer_typefunc(context, call_node, func_type, default_node)
numba.type_inference.infer_call.parse_signature(node, func_type)
numba.type_inference.infer_call.resolve_function(func_variable)

Get a function object given a function name

module_type_inference Module

Support for type functions for external code.

See modules/numpy*.py for type inference for NumPy.

class numba.type_inference.module_type_inference.ModuleTypeInfererRegistry

Bases: object

Builds the module type inferers for the modules we can handle

get_inferer(value, func_type=None)
is_registered(value, func_type=None)
lookup_module_attribute(value)

Return the module (or None) to which a registered value belongs

register_inferer(module, attr, inferer, **kwds)

Register an type function (a type inferer) for a known function value.

E.g. np.add() can be mapped as follows:

module=np, attr=’add’, inferrer=my_inferer
register_unbound_dotted(module, attr, dotted_path, inferer, **kwds)

Register an type function for a dotted attribute path of a value,

E.g. my_module.my_obj.foo.bar() can be mapped as follows:

module=my_module, attr=’my_obj’, dotted_path=’foo.bar’, inferrer=my_inferer
register_unbound_dotted_value(value, dotted_path, inferer, **kwds)
register_unbound_method(module, attr, method_name, inferer, **kwds)

Register an unbound method or dotted attribute path (allow for transience).

E.g. np.add.reduce() can be mapped as follows:

module=np, attr=’add’, method_name=’reduce’, inferrer=my_inferer
register_value(value, inferer, pass_in_types=True, pass_in_callnode=False, can_handle_deferred_types=False)
exception numba.type_inference.module_type_inference.UnmatchedTypeError(node, msg=None, *args, **kwds)

Bases: numba.error.NumbaError

Raised when no matching specialization is found for a registered signature (register_callable).

exception numba.type_inference.module_type_inference.ValueAlreadyRegistered(node, msg=None, *args, **kwds)

Bases: numba.error.NumbaError

Raised when a type inferer is registered multiple times for the same value.

numba.type_inference.module_type_inference.can_handle_deferred(py_func)

Return whether the type function can handle deferred argument types

numba.type_inference.module_type_inference.dispatch_on_value(context, call_node, func_type)

Dispatch a call of a module attribute by value.

For instance, a method

def empty(shape, dtype, order):
...

would be called with those arguments. Parameters not present as arguments in user code are None.

Returns the result type, or None

numba.type_inference.module_type_inference.module_attribute_type(obj)

See if the object is registered to any module which might handle type inference on the object.

numba.type_inference.module_type_inference.parse_args(call_node, arg_names)

Parse positional and keyword arguments.

numba.type_inference.module_type_inference.register(module, **kws)

@register(module) def my_type_function(arg1, ..., argN):

...
numba.type_inference.module_type_inference.register_callable(signature)

signature := function | typeset(signature *)

@register_callable(signature) def my_function(...):

...
numba.type_inference.module_type_inference.resolve_call(context, call_node, obj_call_node, func_type)

Find the right type inferrer function for a call to an attribute of a certain module.

call_node: the original ast.Call node that we need to resolve
the type for
obj_call_node: the nodes.ObjectCallNode that would replace the
ast.Call unless we override that with another node.
func_type: module_attribute
|__________> module: Python module |__________> attr: Attribute name |__________> value: Attribute value

Returns a new AST node that should replace the ast.Call node.

numba.type_inference.module_type_inference.resolve_call_or_none(context, call_node, func_type)