def impl_numba_typeref_ctor(cls): """ Defines ``Dict()``, the type-inferred version of the dictionary ctor. Parameters ---------- cls : TypeRef Expecting a TypeRef of a precise DictType. See also: `redirect_type_ctor` in numba/cpython/bulitins.py """ dict_ty = cls.instance_type if not isinstance(dict_ty, types.DictType): msg = "expecting a DictType but got {}".format(dict_ty) return # reject # Ensure the dictionary is precisely typed. if not dict_ty.is_precise(): msg = "expecting a precise DictType but got {}".format(dict_ty) raise errors.LoweringError(msg) key_type = types.TypeRef(dict_ty.key_type) value_type = types.TypeRef(dict_ty.value_type) def impl(cls): # Simply call .empty() with the key/value types from *cls* return Dict.empty(key_type, value_type) return impl
def redirect_type_ctor(context, builder, sig, args): """Redirect constructor implementation to `numba_typeref_ctor(cls, *args)`, which should be overloaded by the type's implementation. For example: d = Dict() `d` will be typed as `TypeRef[DictType]()`. Thus, it will call into this implementation. We need to redirect the lowering to a function named ``numba_typeref_ctor``. """ cls = sig.return_type def call_ctor(cls, *args): return numba_typeref_ctor(cls, *args) # Pack arguments into a tuple for `*args` ctor_args = types.Tuple.from_types(sig.args) # Make signature T(TypeRef[T], *args) where T is cls sig = typing.signature(cls, types.TypeRef(cls), ctor_args) if len(ctor_args) > 0: args = ( context.get_dummy_value(), # Type object has no runtime repr. context.make_tuple(builder, ctor_args, args)) else: args = ( context.get_dummy_value(), # Type object has no runtime repr. context.make_tuple(builder, ctor_args, ())) return context.compile_internal(builder, call_ctor, sig, args)
def _typeof_nb_type(val, c): if isinstance(val, types.BaseFunction): return val elif isinstance(val, (types.Number, types.Boolean)): return types.NumberClass(val) else: return types.TypeRef(val)
def overload_classmethod(typ, attr, **kwargs): """ A decorator marking the decorated function as typing and implementing classmethod *attr* for the given Numba type in nopython mode. Similar to ``overload_method``. Here is an example implementing a classmethod on the Array type to call ``np.arange()``:: @overload_classmethod(types.Array, "make") def ov_make(cls, nitems): def impl(cls, nitems): return np.arange(nitems) return impl The above code will allow the following to work in jit-compiled code:: @njit def foo(n): return types.Array.make(n) """ return _overload_method_common(types.TypeRef(typ), attr, **kwargs)
def impl_numba_typeref_ctor(cls): """ Defines ``List()``, the type-inferred version of the list ctor. Parameters ---------- cls : TypeRef Expecting a TypeRef of a precise ListType. See also: `redirect_type_ctor` in numba/cpython/bulitins.py """ list_ty = cls.instance_type if not isinstance(list_ty, types.ListType): msg = "expecting a ListType but got {}".format(list_ty) return # reject # Ensure the list is precisely typed. if not list_ty.is_precise(): msg = "expecting a precise ListType but got {}".format(list_ty) raise errors.LoweringError(msg) item_type = types.TypeRef(list_ty.item_type) def impl(cls): # Simply call .empty_list with the item types from *cls* return List.empty_list(item_type) return impl
def test_issue_typeref_key(self): # issue https://github.com/numba/numba/issues/6336 class NoUniqueNameType(types.Dummy): def __init__(self, param): super(NoUniqueNameType, self).__init__('NoUniqueNameType') self.param = param @property def key(self): return self.param no_unique_name_type_1 = NoUniqueNameType(1) no_unique_name_type_2 = NoUniqueNameType(2) for ty1 in (no_unique_name_type_1, no_unique_name_type_2): for ty2 in (no_unique_name_type_1, no_unique_name_type_2): self.assertIs( types.TypeRef(ty1) == types.TypeRef(ty2), ty1 == ty2)
def impl_numba_typeref_ctor(cls, *args): """Defines lowering for ``List()`` and ``List(iterable)``. This defines the lowering logic to instantiate either an empty typed-list or a typed-list initialised with values from a single iterable argument. Parameters ---------- cls : TypeRef Expecting a TypeRef of a precise ListType. args: tuple A tuple that contains a single iterable (optional) Returns ------- impl : function An implementation suitable for lowering the constructor call. See also: `redirect_type_ctor` in numba/cpython/bulitins.py """ list_ty = cls.instance_type if not isinstance(list_ty, types.ListType): return # reject # Ensure the list is precisely typed. if not list_ty.is_precise(): msg = "expecting a precise ListType but got {}".format(list_ty) raise LoweringError(msg) item_type = types.TypeRef(list_ty.item_type) if args: # special case 0d Numpy arrays if isinstance(args[0], types.Array) and args[0].ndim == 0: def impl(cls, *args): # Instatiate an empty list and populate it with the single # value from the array. r = List.empty_list(item_type) r.append(args[0].item()) return r else: def impl(cls, *args): # Instatiate an empty list and populate it with values from the # iterable. r = List.empty_list(item_type) for i in args[0]: r.append(i) return r else: def impl(cls, *args): # Simply call .empty_list with the item type from *cls* return List.empty_list(item_type) return impl
def typeof_type(val, c): """ Type various specific Python types. """ if issubclass(val, BaseException): return types.ExceptionClass(val) if issubclass(val, tuple) and hasattr(val, "_asdict"): return types.NamedTupleClass(val) if issubclass(val, np.generic): return types.NumberClass(numpy_support.from_dtype(val)) from numba.typed import Dict if issubclass(val, Dict): return types.TypeRef(types.DictType) from numba.typed import List if issubclass(val, List): return types.TypeRef(types.ListType)