def test_merge_union_types(self): int_var = int def fun(): pass class Foo: def method(self): pass def method_2(self): pass class_var = Foo method_var = Foo.method instance_var = Foo() import math module_var = math union1 = UnionType.create_from_type_list([int_var, fun, class_var]) union2 = UnionType.create_from_type_list( [method_var, instance_var, module_var]) compare_types(union1, [int_var, fun, class_var]) compare_types(union2, [method_var, instance_var, module_var]) fused_union = UnionType.add(union1, union2) compare_types( fused_union, [int_var, fun, class_var, method_var, instance_var, module_var]) clone = UnionType.create_from_type_list( [int_var, fun, class_var, method_var, instance_var, module_var]) compare_types(fused_union, clone) method2_var = Foo.method_2 UnionType.add(clone, types) UnionType.add(clone, method2_var) compare_types( fused_union, [int_var, fun, class_var, method_var, instance_var, module_var]) compare_types(clone, [ int_var, fun, class_var, method_var, instance_var, module_var, method2_var, types ]) clone2 = UnionType.create_from_type_list([ int_var, fun, class_var, method_var, instance_var, module_var, method2_var, types ]) self.assertFalse(fused_union == clone) compare_types(clone2, clone)
def assign_ssa_finally_branch_types_of_members_for_object( obj, branch_rest, branch_finally): """ Assign all the types contained on the dictionary branch2 to the dictionary branch1, overwriting them if the same name exist in both branches. This is needed for the implementation of the finally branch, that do not follow the same behaviour as the other branches when implementing the SSA algorithm. :param obj: :param branch_rest: :param branch_finally: :return: """ for name in branch_finally: if isinstance(branch_finally[name], UnionType): types = branch_finally[name].get_types() types_without_undefined = filter(lambda t: t is not UndefinedType, types) if len(types_without_undefined) < len( types): # There were undefined types union_without_undefined = UnionType.create_from_type_list( types_without_undefined) branch_rest[name] = UnionType.add(branch_rest[name], union_without_undefined) continue # if obj not in branch_rest: # branch_rest[obj] = dict() branch_rest[name] = branch_finally[name] return branch_rest
def test_create_union_type_with_mixed_types(self): int_var = int def fun(): pass class Foo: def method(self): pass def method_2(self): pass class_var = Foo method_var = Foo.method instance_var = Foo() import math module_var = math union = UnionType.create_from_type_list( [int_var, fun, class_var, method_var, instance_var, module_var]) compare_types( union, [int_var, fun, class_var, method_var, instance_var, module_var]) clone = UnionType.create_from_type_list( [int_var, fun, class_var, method_var, instance_var, module_var]) compare_types(union, clone) method2_var = Foo.method_2 UnionType.add(clone, types) UnionType.add(clone, method2_var) compare_types( union, [int_var, fun, class_var, method_var, instance_var, module_var]) compare_types(clone, [ int_var, fun, class_var, method_var, instance_var, module_var, method2_var, types ])
def test_call_union_type(self): abs_func = self.context.get_type_of(self.localization, 'abs') union_valid = UnionType.create_from_type_list([True, complex()]) union_mixed = UnionType.create_from_type_list([int(), list()]) union_wrong = UnionType.create_from_type_list([dict(), list()]) # Two valid types ret = invoke(self.localization, abs_func, union_valid) compare_types(ret, [int(), float()]) # Two types: one valid, one invalid ret = invoke(self.localization, abs_func, union_mixed) compare_types(type(ret), int) assert len(TypeWarning.get_warning_msgs()) == 1 # Two invalid types ret = invoke(self.localization, abs_func, union_wrong) compare_types(type(ret), StypyTypeError) assert len(StypyTypeError.get_error_msgs()) == 1
def test_call_simple_union_type_param(self): union_param = UnionType.create_from_type_list([int(), str(), float()]) import math math_module = math sqrt_func = self.type_store.get_type_of_member(self.loc, math_module, "sqrt") ret = invoke(self.loc, sqrt_func, union_param) self.assertTrue(len(TypeWarning.get_warning_msgs()) == 1) compare_types(ret, float())
import types from stypy.types.union_type import UnionType from stypy.types.undefined_type import UndefinedType test_types = { '__main__': { 'fabs': UnionType.create_from_type_list( [types.BuiltinFunctionType, UndefinedType]), 'cos': UnionType.create_from_type_list( [types.BuiltinFunctionType, UndefinedType]), 'ex': UnionType.create_from_type_list([Exception, UndefinedType]), 'r': float, 'kos': UndefinedType, 'r2': UndefinedType, 'sin': UndefinedType, }, }
from testing.code_generation_testing.codegen_testing_common import instance_of_class_name import numpy from stypy.types.union_type import UnionType from stypy.invokation.type_rules.type_groups.type_groups import UndefinedType test_types = { '__main__': { 'a': instance_of_class_name("ndarray"), 'b': instance_of_class_name("ndarray"), 'r2': UnionType.create_from_type_list([numpy.int32, numpy.ndarray]), '__builtins__': instance_of_class_name("module"), '__file__': instance_of_class_name("str"), '__package__': instance_of_class_name("NoneType"), 'r': UnionType.create_from_type_list([numpy.int32, numpy.ndarray]), 'np': instance_of_class_name("module"), '__name__': instance_of_class_name("str"), '__doc__': instance_of_class_name("NoneType"), }, }
import numpy from stypy.types.union_type import UnionType from stypy.invokation.type_rules.type_groups.type_groups import UndefinedType test_types = { '__main__': { 'r4': instance_of_class_name("bool"), '__builtins__': instance_of_class_name("module"), '__file__': instance_of_class_name("str"), '__name__': instance_of_class_name("str"), 'rf3': UnionType.create_from_type_list( [numpy.float32, numpy.float64, UndefinedType]), 'rf2': UnionType.create_from_type_list([int, long, UndefinedType]), 'rf1': UnionType.create_from_type_list([int, long, UndefinedType]), 'rf5': UnionType.create_from_type_list( [numpy.float32, numpy.float64, UndefinedType]), 'r2': instance_of_class_name("float64"), 'rf4': UnionType.create_from_type_list( [numpy.float32, numpy.float64, UndefinedType]), 'r3': instance_of_class_name("bool"), '__package__':
from testing.code_generation_testing.codegen_testing_common import instance_of_class_name import numpy from stypy.types.union_type import UnionType from stypy.invokation.type_rules.type_groups.type_groups import UndefinedType test_types = { '__main__': { 'a': UnionType.create_from_type_list([numpy.ndarray, UndefinedType]), 'b': instance_of_class_name("ndarray"), 'r2': instance_of_class_name("ndarray"), '__builtins__': instance_of_class_name("module"), '__file__': instance_of_class_name("str"), '__package__': instance_of_class_name("NoneType"), 'fa': UnionType.create_from_type_list([numpy.ndarray, UndefinedType]), 'r': instance_of_class_name("NoneType"), 'np': instance_of_class_name("module"), '__name__': instance_of_class_name("str"), '__doc__': instance_of_class_name("NoneType"), }, }
from testing.code_generation_testing.codegen_testing_common import instance_of_class_name import numpy from stypy.types.union_type import UnionType from stypy.invokation.type_rules.type_groups.type_groups import UndefinedType test_types = { '__main__': { 'index': instance_of_class_name("tuple"), 'r2': UnionType.create_from_type_list([tuple, UndefinedType]), '__builtins__': instance_of_class_name("module"), '__file__': instance_of_class_name("str"), 'value': instance_of_class_name("long"), '__package__': instance_of_class_name("NoneType"), 'r': UnionType.create_from_type_list([tuple, UndefinedType]), 'np': instance_of_class_name("module"), '__name__': instance_of_class_name("str"), 'Z': instance_of_class_name("ndarray"), '__doc__': instance_of_class_name("NoneType"), }, }
from testing.code_generation_testing.codegen_testing_common import instance_of_class_name import numpy from stypy.types.union_type import UnionType from stypy.invokation.type_rules.type_groups.type_groups import UndefinedType test_types = { '__main__': { 'a': instance_of_class_name("int"), 'phi': UnionType.create_from_type_list([numpy.ndarray, UndefinedType]), '__builtins__': instance_of_class_name("module"), '__file__': instance_of_class_name("str"), 'y_int': UnionType.create_from_type_list([numpy.ndarray, UndefinedType]), '__package__': instance_of_class_name("NoneType"), 'np': instance_of_class_name("module"), 'r': UnionType.create_from_type_list([numpy.ndarray, UndefinedType]), '__name__': instance_of_class_name("str"), 'x': UnionType.create_from_type_list([numpy.ndarray, UndefinedType]), 'y': UnionType.create_from_type_list([numpy.ndarray, UndefinedType]), 'r_int': instance_of_class_name("ndarray"), 'x_int': UnionType.create_from_type_list([numpy.ndarray, UndefinedType]), 'dr': UnionType.create_from_type_list([numpy.ndarray, UndefinedType]), '__doc__': instance_of_class_name("NoneType"), }, }
from testing.code_generation_testing.codegen_testing_common import instance_of_class_name import numpy from stypy.types.union_type import UnionType from stypy.invokation.type_rules.type_groups.type_groups import UndefinedType test_types = { '__main__': { 'r4': instance_of_class_name("tuple"), '__builtins__': instance_of_class_name("module"), '__file__': instance_of_class_name("str"), 'row_r2': instance_of_class_name("ndarray"), 'row_r1': UnionType.create_from_type_list([numpy.int32, numpy.ndarray]), 'r7': instance_of_class_name("ndarray"), '__doc__': instance_of_class_name("NoneType"), '__name__': instance_of_class_name("str"), 'a': instance_of_class_name("ndarray"), 'r5': instance_of_class_name("ndarray"), 'r6': instance_of_class_name("tuple"), 'col_r2': instance_of_class_name("ndarray"), 'r1': UnionType.create_from_type_list([numpy.int32, numpy.ndarray]), 'r2': UnionType.create_from_type_list([tuple, tuple]), 'r3': instance_of_class_name("ndarray"), 'r8': instance_of_class_name("tuple"), '__package__': instance_of_class_name("NoneType"), 'np': instance_of_class_name("module"), 'col_r1': instance_of_class_name("ndarray"), }, }
from testing.code_generation_testing.codegen_testing_common import instance_of_class_name import numpy from stypy.types.union_type import UnionType from stypy.invokation.type_rules.type_groups.type_groups import DynamicType test_types = { '__main__': { 'b2': instance_of_class_name("ndarray"), 'a': instance_of_class_name("ndarray"), 'r28': instance_of_class_name("ndarray"), 'r29': UnionType.create_from_type_list([numpy.int32, numpy.ndarray]), 'b3': instance_of_class_name("ndarray"), 'r27': UnionType.create_from_type_list([numpy.float64, numpy.ndarray]), 'r26': UnionType.create_from_type_list([numpy.float64, numpy.ndarray]), 'r25': instance_of_class_name("ndarray"), 'r24': UnionType.create_from_type_list([numpy.int32, numpy.ndarray]), 'r23': UnionType.create_from_type_list([numpy.int32, numpy.ndarray]), 'r22': instance_of_class_name("ndarray"), 'r21': UnionType.create_from_type_list([numpy.int32, numpy.ndarray]), 'r20': UnionType.create_from_type_list([DynamicType, numpy.ndarray]), '__package__': instance_of_class_name("NoneType"), 'np': instance_of_class_name("module"), 'b': instance_of_class_name("ndarray"), '__doc__': instance_of_class_name("NoneType"), 'r16': instance_of_class_name("ndarray"), 'r17': UnionType.create_from_type_list([numpy.int32, numpy.ndarray]), 'r14': UnionType.create_from_type_list([numpy.int32, numpy.ndarray]), 'r15': UnionType.create_from_type_list([numpy.int32, numpy.ndarray]), 'r12': UnionType.create_from_type_list([numpy.int32, numpy.ndarray]), 'r13': instance_of_class_name("ndarray"), 'r10': UnionType.create_from_type_list([numpy.int32, numpy.ndarray]),
from testing.code_generation_testing.codegen_testing_common import instance_of_class_name from stypy.types.union_type import UnionType import numpy test_types = { '__main__': { 'idx': UnionType.create_from_type_list([numpy.ndarray, numpy.ndarray]), 'uZ': numpy.ndarray, '__builtins__': instance_of_class_name("module"), '__file__': instance_of_class_name("str"), '__package__': instance_of_class_name("NoneType"), 'T': instance_of_class_name("ndarray"), 'np': instance_of_class_name("module"), '__name__': instance_of_class_name("str"), 'Z': instance_of_class_name("ndarray"), '__doc__': instance_of_class_name("NoneType"), '_': UnionType.create_from_type_list([numpy.ndarray, numpy.ndarray]), }, }
from testing.code_generation_testing.codegen_testing_common import instance_of_class_name import numpy from stypy.types.union_type import UnionType from stypy.invokation.type_rules.type_groups.type_groups import UndefinedType test_types = { '__main__': { '__builtins__': instance_of_class_name("module"), '__file__': instance_of_class_name("str"), 'spacing': UnionType.create_from_type_list([numpy.ndarray, numpy.float64]), '__name__': instance_of_class_name("str"), 'r4': UnionType.create_from_type_list([numpy.ndarray, numpy.float64]), 'r5': UnionType.create_from_type_list([numpy.ndarray, numpy.float64]), 'r6': UnionType.create_from_type_list([numpy.ndarray, numpy.float64]), 'r2': instance_of_class_name("ndarray"), 'r3': instance_of_class_name("ndarray"), 'samples3': UnionType.create_from_type_list([numpy.ndarray, numpy.float64]), 'samples2': UnionType.create_from_type_list([numpy.ndarray, numpy.float64]), '__package__': instance_of_class_name("NoneType"), 'r': instance_of_class_name("ndarray"), 'samples': UnionType.create_from_type_list([numpy.ndarray, numpy.float64]), 'np': instance_of_class_name("module"), '__doc__': instance_of_class_name("NoneType"), }, }
from testing.code_generation_testing.codegen_testing_common import instance_of_class_name from stypy.types.union_type import UnionType from stypy.types.undefined_type import UndefinedType import numpy test_types = { '__main__': { 'F': UnionType.create_from_type_list( [numpy.int32, numpy.ndarray, numpy.ndarray]), '__builtins__': instance_of_class_name("module"), 'h': instance_of_class_name("int"), '__file__': instance_of_class_name("str"), '__package__': instance_of_class_name("NoneType"), 'I': UnionType.create_from_type_list([numpy.ndarray, UndefinedType]), 'r': UnionType.create_from_type_list([numpy.ndarray, UndefinedType]), 'w': instance_of_class_name("int"), 'np': instance_of_class_name("module"), '__name__': instance_of_class_name("str"), 'n': instance_of_class_name("int"), '__doc__':