def test_named_tuple(self): self.assertEqual( NamedTuple(x=int, y=int, z=OneOf(10, 20)).ElementTypes, (Int64(), Int64(), OneOf(10, 20))) self.assertEqual( NamedTuple(x=int, y=int, z=OneOf(10, 20)).ElementNames, ('x', 'y', 'z'))
def test_tupleOf(self): self.assertEqual(TupleOf(int), TupleOf(Int64())) self.assertEqual(TupleOf(int).ElementType, Int64()) self.assertEqual(TupleOf(float).ElementType, Float64()) self.assertEqual(TupleOf(OneOf(10, 20)).ElementType, OneOf(10, 20)) self.assertEqual( TupleOf(object).ElementType.__typed_python_category__, "PythonObjectOfType") self.assertEqual( TupleOf(10).ElementType.__typed_python_category__, "Value")
def convert_to_self(self, context, expr): if expr.expr_type.typeRepresentation == Int64(): return context.push( self, lambda targetSlot: targetSlot.expr.store( runtime_functions.int_to_pyobj.call(expr.nonref_expr))) return super().convert_to_self(context, expr)
def convert_to_type(self, context, expr, target_type): if target_type.typeRepresentation == Int64(): return context.pushPod( target_type, runtime_functions.pyobj_to_int.call(expr.nonref_expr)) return super().convert_to_type(context, expr, target_type)
def test_create_simple_function(self): @TypedFunction def f(x: int) -> int: return x + 1 self.assertEqual(f(2), 3) with self.assertRaises(TypeError): f(3.5) self.assertEqual(len(f.overloads), 1) o = f.overloads[0] self.assertEqual(o.returnType, Int64()) self.assertEqual(len(o.args), 1) self.assertEqual(o.args[0].name, "x") self.assertEqual(o.args[0].typeFilter, Int64()) self.assertEqual(o.args[0].defaultValue, None) self.assertEqual(o.args[0].isStarArg, False) self.assertEqual(o.args[0].isKwarg, False)
def test_create_function_with_kwargs_and_star_args_and_defaults(self): @TypedFunction def f(x: int, y = 30, z: None=None, *args: TupleOf(float), **kwargs: ConstDict(str, float)) -> int: return x + 1 self.assertEqual(len(f.overloads), 1) o = f.overloads[0] self.assertEqual(len(o.args), 5) self.assertEqual([a.name for a in o.args], ['x','y','z','args','kwargs']) self.assertEqual([a.typeFilter for a in o.args], [Int64(), None, NoneType(), TupleOf(float), ConstDict(str,float)]) self.assertEqual([a.defaultValue for a in o.args], [None, (30,), (None,), None, None]) self.assertEqual([a.isStarArg for a in o.args], [False, False, False, True, False]) self.assertEqual([a.isKwarg for a in o.args], [False, False, False, False, True])
def test_alternatives(self): X = Alternative("X", Left={ 'x': int, 'y': str }, Right={ 'x': lambda: X, 'val': int }) _types.resolveForwards(X) self.assertEqual(len(X.__typed_python_alternatives__), 2) Left, Right = X.__typed_python_alternatives__ self.assertEqual(Left.Index, 0) self.assertEqual(Right.Index, 1) self.assertEqual(Left.ElementType.ElementNames, ("x", "y")) self.assertEqual(Left.ElementType.ElementTypes, (Int64(), String())) self.assertEqual(Right.ElementType.ElementNames, ('x', 'val')) self.assertEqual(Right.ElementType.ElementTypes, (X, Int64()))
def toInt64(self): return self.expr_type.convert_to_type(self.context, self, typeWrapper(Int64()))
def test_const_dict(self): self.assertEqual(ConstDict(str, int).KeyType, String()) self.assertEqual(ConstDict(str, int).ValueType, Int64())
def test_tuple(self): self.assertEqual( Tuple(int, int, OneOf(10, 20)).ElementTypes, (Int64(), Int64(), OneOf(10, 20)))
def test_object_bytecounts(self): self.assertEqual(_types.bytecount(NoneType()), 0) self.assertEqual(_types.bytecount(Int8()), 1) self.assertEqual(_types.bytecount(Int64()), 8)