def resolve___call__(self, classty): """ Resolve a number class's constructor (e.g. calling int(...)) Note: This is needed because of the limitation of the current type-system implementation. Specifically, the lack of a higher-order type (i.e. passing the ``DictType`` vs ``DictType(key_type, value_type)``) """ ty = classty.instance_type if isinstance(ty, type) and issubclass(ty, types.Type): # Redirect the typing to a: # @type_callable(ty) # def typeddict_call(context): # ... # For example, see numba/typed/typeddict.py # @type_callable(DictType) # def typeddict_call(context): def redirect(*args, **kwargs): return self.context.resolve_function_type(ty, args, kwargs) return types.Function( make_callable_template(key=ty, typer=redirect))
def resolve___call__(self, classty): """ Resolve a number class's constructor (e.g. calling int(...)) """ ty = classty.instance_type def typer(val): if isinstance(val, (types.BaseTuple, types.Sequence)): # Array constructor, e.g. np.int32([1, 2]) fnty = self.context.resolve_value_type(np.array) sig = fnty.get_call_type(self.context, (val, types.DType(ty)), {}) return sig.return_type elif isinstance(val, (types.Number, types.Boolean)): # Scalar constructor, e.g. np.int32(42) return ty else: if (isinstance(val, types.Array) and val.ndim == 0 and val.dtype == ty): # This is 0d array -> scalar degrading return ty else: # unsupported msg = f"Casting {val} to {ty} directly is unsupported." if isinstance(val, types.Array): # array casts are supported a different way. msg += f" Try doing '<array>.astype(np.{ty})' instead" raise errors.TypingError(msg) return types.Function(make_callable_template(key=ty, typer=typer))
def resolve___call__(self, classty): """ Resolve a number class's constructor (e.g. calling int(...)) """ ty = classty.instance_type def typer(val): if isinstance(val, (types.BaseTuple, types.Sequence)): # Array constructor, e.g. np.int32([1, 2]) sig = self.context.resolve_function_type( np.array, (val, ), {'dtype': types.DType(ty)}) return sig.return_type else: # Scalar constructor, e.g. np.int32(42) return ty return types.Function(make_callable_template(key=ty, typer=typer))
def resolve___call__(self, classty): """ Resolve a number class's constructor (e.g. calling int(...)) """ ty = classty.instance_type def typer(val): if isinstance(val, (types.BaseTuple, types.Sequence)): # Array constructor, e.g. np.int32([1, 2]) fnty = self.context.resolve_value_type(np.array) sig = fnty.get_call_type(self.context, (val, types.DType(ty)), {}) return sig.return_type elif isinstance( val, (types.Number, types.Boolean, types.IntEnumMember)): # Scalar constructor, e.g. np.int32(42) return ty elif isinstance(val, (types.NPDatetime, types.NPTimedelta)): # Constructor cast from datetime-like, e.g. # > np.int64(np.datetime64("2000-01-01")) if ty.bitwidth == 64: return ty else: msg = (f"Cannot cast {val} to {ty} as {ty} is not 64 bits " "wide.") raise errors.TypingError(msg) else: if (isinstance(val, types.Array) and val.ndim == 0 and val.dtype == ty): # This is 0d array -> scalar degrading return ty else: # unsupported msg = f"Casting {val} to {ty} directly is unsupported." if isinstance(val, types.Array): # array casts are supported a different way. msg += f" Try doing '<array>.astype(np.{ty})' instead" raise errors.TypingError(msg) return types.Function(make_callable_template(key=ty, typer=typer))