def test_out_both_if_nested_else(self):
        self.context.set_type_of(self.localization, "var1", long)
        self.context.set_type_of(self.localization, "var2", long)

        self.context = self.context.open_ssa_context("if")
        self.context.set_type_of(self.localization, "var1", int)
        self.context.set_type_of(self.localization, "var2", float)
        self.context.open_ssa_branch("else")  # else
        self.context.set_type_of(self.localization, "var1", str)
        self.context.set_type_of(self.localization, "var2", str)

        self.context = self.context.open_ssa_context("if")
        self.context.open_ssa_branch("else")  # else
        self.context.set_type_of(self.localization, "var1", complex)
        self.context.set_type_of(self.localization, "var2", complex)

        self.context = self.context.join_ssa_context()
        self.context = self.context.join_ssa_context()

        compare_types(self.context.get_type_of(self.localization, "var1"),
                      [int, str, complex])
        compare_types(self.context.get_type_of(self.localization, "var2"),
                      [float, str, complex])
    def test_out_and_nested_nested_else_some_branches(self):
        self.context.set_type_of(self.localization, "var1", int)
        self.context.set_type_of(self.localization, "var2", float)

        self.context = self.context.open_ssa_context("if")
        self.context.open_ssa_branch("else")  # else
        self.context = self.context.open_ssa_context("if")
        self.context.open_ssa_branch("else")  # else
        self.context.set_type_of(self.localization, "var1", complex)
        self.context.set_type_of(self.localization, "var2", complex)

        self.context = self.context.open_ssa_context("if")
        self.context.open_ssa_branch("else")  # else
        self.context.set_type_of(self.localization, "var1", list)

        self.context = self.context.join_ssa_context()
        self.context = self.context.join_ssa_context()
        self.context = self.context.join_ssa_context()

        compare_types(self.context.get_type_of(self.localization, "var1"),
                      [int, complex, list])
        compare_types(self.context.get_type_of(self.localization, "var2"),
                      [float, complex])
    def test_context_store(self):
        self.context.set_type_of(self.localization, "var1", int)
        self.context.set_type_of(self.localization, "var2", float)
        self.context.set_type_of(self.localization, "var3", str)

        compare_types(self.context.get_type_of(self.localization, "var1"), int)
        compare_types(self.context.get_type_of(self.localization, "var2"),
                      float)
        compare_types(self.context.get_type_of(self.localization, "var3"), str)
    def test_both_if_nested_nested_else_some_branches(self):
        temp1 = self.context.get_type_of(self.localization, "var1")

        self.context = self.context.open_ssa_context("if")
        self.context.set_type_of_member(self.localization, temp1, "instance_attribute", int)
        self.context.set_type_of_member(self.localization, temp1, "instance_attribute2", float)
        self.context.open_ssa_branch("else")  # else
        self.context = self.context.open_ssa_context("if")
        self.context.open_ssa_branch("else")  # else
        self.context.set_type_of_member(self.localization, temp1, "instance_attribute", complex)
        self.context.set_type_of_member(self.localization, temp1, "instance_attribute2", complex)

        self.context = self.context.open_ssa_context("if")
        self.context.open_ssa_branch("else")  # else
        self.context.set_type_of_member(self.localization, temp1, "instance_attribute", list)

        self.context = self.context.join_ssa_context()
        self.context = self.context.join_ssa_context()
        self.context = self.context.join_ssa_context()

        compare_types(self.context.get_type_of_member(self.localization, temp1, "instance_attribute"),
                      [int, complex, list, UndefinedType])
        compare_types(self.context.get_type_of_member(self.localization, temp1, "instance_attribute2"),
                      [float, complex, UndefinedType])
    def test_only_nested_nested_else(self):
        self.context = self.context.open_ssa_context("if")
        self.context.open_ssa_branch("else")  # else
        self.context.set_type_of(self.localization, "var1", str)
        self.context.set_type_of(self.localization, "var2", str)

        self.context = self.context.open_ssa_context("if")
        self.context.open_ssa_branch("else")  # else
        self.context.set_type_of(self.localization, "var1", complex)
        self.context.set_type_of(self.localization, "var2", complex)

        self.context = self.context.open_ssa_context("if")
        self.context.open_ssa_branch("else")  # else
        self.context.set_type_of(self.localization, "var1", list)
        self.context.set_type_of(self.localization, "var2", list)

        self.context = self.context.join_ssa_context()
        self.context = self.context.join_ssa_context()
        self.context = self.context.join_ssa_context()

        compare_types(self.context.get_type_of(self.localization, "var1"),
                      [UndefinedType, str, complex, list])
        compare_types(self.context.get_type_of(self.localization, "var2"),
                      [UndefinedType, str, complex, list])
Esempio n. 6
0
    def test_union_invoke_return_types(self):
        class Foo:
            def method(self, localization):
                return True

            def method_2(self, localization):
                return str()

        class Foo2:
            def method(self, localization):
                return False

            def method_2(self, localization):
                return int()

        # Access a member that can be provided only by some of the types in the union
        union = UnionType.add(Foo(), Foo2())

        method = self.type_store.get_type_of_member(self.loc, union,
                                                    "method_2")
        res = invoke(self.loc, method)
        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 0)

        compare_types(res, [str(), int()])
    def test_out_both_if_maxdepth_nested_else(self):
        temp1 = self.context.get_type_of(self.localization, "var1")

        self.context.set_type_of_member(self.localization, temp1, "class_attribute", long)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute2", long)

        self.context = self.context.open_ssa_context("if")
        self.context.set_type_of_member(self.localization, temp1, "class_attribute", int)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute2", float)
        self.context.open_ssa_branch("else")  # else
        self.context = self.context.open_ssa_context("if")
        self.context.open_ssa_branch("else")  # else
        self.context = self.context.open_ssa_context("if")
        self.context.open_ssa_branch("else")  # else
        self.context.set_type_of_member(self.localization, temp1, "class_attribute", list)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute2", complex)

        self.context = self.context.join_ssa_context()
        self.context = self.context.join_ssa_context()
        self.context = self.context.join_ssa_context()

        compare_types(self.context.get_type_of_member(self.localization, temp1, "class_attribute"), [int, list, long])
        compare_types(self.context.get_type_of_member(self.localization, temp1, "class_attribute2"),
                      [float, complex, long])
    def test_out_and_nested_nested_if_some_branches(self):
        temp1 = self.context.get_type_of(self.localization, "var1")

        self.context.set_type_of_member(self.localization, temp1, "class_attribute", int)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute2", float)

        self.context = self.context.open_ssa_context("if")

        self.context = self.context.open_ssa_context("if")

        self.context.set_type_of_member(self.localization, temp1, "class_attribute", complex)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute2", complex)

        self.context = self.context.open_ssa_context("if")

        self.context.set_type_of_member(self.localization, temp1, "class_attribute", list)

        self.context = self.context.join_ssa_context()
        self.context = self.context.join_ssa_context()
        self.context = self.context.join_ssa_context()

        compare_types(self.context.get_type_of_member(self.localization, temp1, "class_attribute"),
                      [int, complex, list])
        compare_types(self.context.get_type_of_member(self.localization, temp1, "class_attribute2"), [float, complex])
    def test_nested_both_if_else(self):
        temp1 = self.context.get_type_of(self.localization, "var1")

        self.context = self.context.open_ssa_context("if")
        self.context.set_type_of_member(self.localization, temp1, "class_attribute", int)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute2", float)

        # Either if or else will be executed. Therefore the type of the variables will be overwritten no matter what
        self.context = self.context.open_ssa_context("if")
        self.context.set_type_of_member(self.localization, temp1, "class_attribute", list)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute2", list)
        self.context.open_ssa_branch("else")  # else
        self.context.set_type_of_member(self.localization, temp1, "class_attribute", dict)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute2", dict)
        self.context = self.context.join_ssa_context()

        self.context.open_ssa_branch("else")  # else
        self.context.set_type_of_member(self.localization, temp1, "class_attribute", str)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute2", str)

        self.context = self.context.join_ssa_context()

        compare_types(self.context.get_type_of_member(self.localization, temp1, "class_attribute"), [str, list, dict])
        compare_types(self.context.get_type_of_member(self.localization, temp1, "class_attribute2"), [str, list, dict])
    def test_out_nested_both_if_both_else(self):
        temp1 = self.context.get_type_of(self.localization, "var1")

        self.context.set_type_of_member(self.localization, temp1, "class_attribute", long)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute2", long)
        self.context = self.context.open_ssa_context("if")
        self.context.set_type_of_member(self.localization, temp1, "class_attribute", int)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute2", float)

        self.context = self.context.open_ssa_context("if")
        self.context.set_type_of_member(self.localization, temp1, "class_attribute", list)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute2", list)
        self.context.open_ssa_branch("else")  # else
        self.context.set_type_of_member(self.localization, temp1, "class_attribute", dict)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute2", dict)
        self.context = self.context.join_ssa_context()

        self.context.open_ssa_branch("else")  # else

        self.context.set_type_of_member(self.localization, temp1, "class_attribute", str)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute2", str)

        self.context = self.context.open_ssa_context("if")
        self.context.set_type_of_member(self.localization, temp1, "class_attribute", set)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute2", set)
        self.context.open_ssa_branch("else")  # else
        self.context.set_type_of_member(self.localization, temp1, "class_attribute", bytearray)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute2", bytearray)
        self.context = self.context.join_ssa_context()

        self.context = self.context.join_ssa_context()

        compare_types(self.context.get_type_of_member(self.localization, temp1, "class_attribute"),
                      [list, dict, set, bytearray])
        compare_types(self.context.get_type_of_member(self.localization, temp1, "class_attribute2"),
                      [list, dict, set, bytearray])
    def test_context_store(self):
        temp1 = self.context.get_type_of(self.localization, "var1")

        self.context.set_type_of_member(self.localization, temp1, "class_attribute", int)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute2", float)
        self.context.set_type_of_member(self.localization, temp1, "class_attribute3", str)

        compare_types(self.context.get_type_of_member(self.localization, temp1, "class_attribute"), int)
        compare_types(self.context.get_type_of_member(self.localization, temp1, "class_attribute2"), float)
        compare_types(self.context.get_type_of_member(self.localization, temp1, "class_attribute3"), str)
Esempio n. 12
0
    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_wrong_call_builtin_module_class(self):
        type_ = self.context.get_type_of(self.localization, "list")
        compare_types(type_, list)

        # Wrong type
        ret = invoke(self.localization, type_, int())
        compare_types(type(ret), StypyTypeError)

        # Wrong number of parameters
        ret = invoke(self.localization, type_, str(), int())
        compare_types(type(ret), StypyTypeError)
    def test_wrong_call_builtin_module_function_simple_types(self):
        func = self.context.get_type_of(self.localization, "abs")
        compare_types(type(func), types.BuiltinFunctionType)

        # Wrong param type
        ret = invoke(self.localization, func, [list()])
        compare_types(type(ret), StypyTypeError)

        # Wrong param number
        ret = invoke(self.localization, func, [int(), int()])
        compare_types(type(ret), StypyTypeError)
Esempio n. 15
0
    def test_create_union_type_with_vars(self):
        union = UnionType.add(int, str)
        compare_types(union, [int, str])

        union2 = UnionType(union, list)
        compare_types(union, [int, str])
        compare_types(union2, [int, str, list])

        self.assertFalse(union == union2)

        clone = UnionType.add(int, str)
        self.assertTrue(union == clone)
    def test_local_variable_turned_global_nested_context(self):
        self.type_store.declare_global(self.loc, "global_var")
        self.type_store.set_type_of(self.loc, "global_var", int)
        assert len(Advice.get_advice_msgs()) == 1

        self.type_store = self.type_store.open_function_context("nested_func")
        num_warnings_before = len(TypeWarning.get_warning_msgs())
        compare_types(self.type_store.get_type_of(Localization(__file__, 1, 1), "global_var"), types.IntType)
        num_warnings_after = len(TypeWarning.get_warning_msgs())

        assert num_warnings_after == num_warnings_before + 1

        self.type_store.declare_global(Localization(__file__, 2, 2), "global_var")
        assert len(Advice.get_advice_msgs()) == 2

        self.type_store.declare_global(Localization(__file__, 3, 3), "non_existing")
        assert len(Advice.get_advice_msgs()) == 3

        assert_if_not_error(self.type_store.get_type_of(self.loc, "non_existing"))

        self.type_store = self.type_store.open_function_context("other_func")

        self.type_store.set_type_of(Localization(__file__, 4, 4), "foo", int)
        self.type_store.declare_global(Localization(__file__, 5, 5), "foo")

        assert len(Advice.get_advice_msgs()) == 4

        self.type_store.declare_global(Localization(__file__, 6, 6), "foo_func")
        self.type_store.set_type_of(Localization(__file__, 6, 6), "foo_func", types.FunctionType)

        assert len(Advice.get_advice_msgs()) == 5

        self.type_store = self.type_store.close_function_context()

        compare_types(self.type_store.get_type_of(Localization(__file__, 1, 1), "foo"), types.IntType)
        self.type_store.set_type_of(Localization(__file__, 1, 1), "foo", types.FloatType)

        assert len(StypyTypeError.get_error_msgs()) == 2

        self.type_store = self.type_store.close_function_context()

        compare_types(self.type_store.get_type_of(self.loc, "foo"), types.IntType)
        compare_types(self.type_store.get_type_of(self.loc, "foo_func"), types.FunctionType)
    def test_wrong_call_builtin_module_class_method(self):
        type_ = self.context.get_type_of(self.localization, "list")
        compare_types(type_, list)

        method = self.context.get_type_of_member(self.localization, type_,
                                                 "__getitem__")
        ret = invoke(self.localization, method, [list()])
        compare_types(type(ret), StypyTypeError)

        method = self.context.get_type_of_member(self.localization, type_,
                                                 "__getitem__")
        ret = invoke(self.localization, method, [int(), int()])
        compare_types(type(ret), StypyTypeError)
    def test_existing_global_var_usage(self):
        self.type_store.set_type_of(self.loc, "global_var", int)
        self.type_store.set_type_of(self.loc, "global_func", types.FunctionType)

        self.type_store = self.type_store.open_function_context("nested_func")
        self.type_store.declare_global(self.loc, "global_var")
        self.type_store.declare_global(self.loc, "global_func")

        num_warnings_before = len(TypeWarning.get_warning_msgs())
        compare_types(self.type_store.get_type_of(self.loc, "global_var"), types.IntType)
        compare_types(self.type_store.get_type_of(self.loc, "global_func"), types.FunctionType)
        num_warnings_after = len(TypeWarning.get_warning_msgs())

        # TypeWarning.print_warning_msgs()
        assert num_warnings_after == num_warnings_before

        res = self.type_store.set_type_of(self.loc, "global_var", str)
        res = self.type_store.set_type_of(self.loc, "global_func", types.NoneType)
        assert res is None
        self.type_store = self.type_store.close_function_context()
        compare_types(self.type_store.get_type_of(self.loc, "global_var"), types.StringType)
        compare_types(self.type_store.get_type_of(self.loc, "global_func"), types.NoneType)
Esempio n. 19
0
    def test_create_union_type_with_modules(self):
        import math
        import sys
        import types

        union = UnionType.add(math, sys)
        compare_types(union, [math, sys])

        clone = UnionType.add(math, sys)
        compare_types(union, clone.types)

        union2 = UnionType.add(clone, types)
        compare_types(union2, [math, sys, types])

        self.assertFalse(union == union2)
Esempio n. 20
0
    def test_create_union_type_with_funcs(self):
        def foo():
            pass

        union = UnionType.add(foo, range)
        compare_types(union, [foo, range])

        union2 = UnionType(union, getattr)
        compare_types(union, [foo, range])
        compare_types(union2, [foo, range, getattr])

        self.assertFalse(union == union2)

        clone = UnionType.add(foo, range)
        self.assertTrue(union == clone)
    def test_try_except_finally(self):
        self.context = self.context.open_ssa_context("try")
        self.context.set_type_of(self.localization, "var1", int)
        self.context.set_type_of(self.localization, "var2", float)
        self.context.open_ssa_branch("except")  # except
        self.context.set_type_of(self.localization, "var1", str)
        self.context.set_type_of(self.localization, "var2", str)
        self.context.open_ssa_branch("finally")  # except
        self.context.set_type_of(self.localization, "var1", list)
        self.context.set_type_of(self.localization, "var2", list)
        self.context.set_type_of(self.localization, "var3", list)
        self.context = self.context.join_ssa_context()

        compare_types(self.context.get_type_of(self.localization, "var1"), list)
        compare_types(self.context.get_type_of(self.localization, "var2"), list)
        compare_types(self.context.get_type_of(self.localization, "var3"), list)
    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_load_var_parent_context(self):
        self.type_store.set_type_of(self.loc, "var", int())

        def fun():
            pass

        self.type_store.set_type_of(self.loc, "func", fun)

        class Foo:
            pass

        self.type_store.set_type_of(self.loc, "Foo", Foo)

        self.type_store.set_type_of(self.loc, "Foo_instance", Foo())

        import math

        self.type_store.set_type_of(self.loc, "math", math)

        self.type_store = self.type_store.open_function_context("new_func")

        compare_types(type(self.type_store.get_type_of(self.loc, "var")), types.IntType)
        compare_types(type(self.type_store.get_type_of(self.loc, "func")), types.FunctionType)
        compare_types(type(self.type_store.get_type_of(self.loc, "Foo")), types.ClassType)
        compare_types(type(self.type_store.get_type_of(self.loc, "Foo_instance")), types.InstanceType)
        compare_types(type(self.type_store.get_type_of(self.loc, "math")), types.ModuleType)

        self.type_store.set_type_of(self.loc, "other_var", float())

        self.type_store = self.type_store.open_function_context("nested_func")

        compare_types(type(self.type_store.get_type_of(self.loc, "var")), types.IntType)
        compare_types(type(self.type_store.get_type_of(self.loc, "func")), types.FunctionType)
        compare_types(type(self.type_store.get_type_of(self.loc, "Foo")), types.ClassType)
        compare_types(type(self.type_store.get_type_of(self.loc, "Foo_instance")), types.InstanceType)
        compare_types(type(self.type_store.get_type_of(self.loc, "math")), types.ModuleType)

        self.type_store.set_type_of(self.loc, "even_other_var", list())
        compare_types(type(self.type_store.get_type_of(self.loc, "other_var")), types.FloatType)

        self.type_store = self.type_store.open_function_context("other_nested_func")

        compare_types(type(self.type_store.get_type_of(self.loc, "var")), types.IntType)
        compare_types(type(self.type_store.get_type_of(self.loc, "func")), types.FunctionType)
        compare_types(type(self.type_store.get_type_of(self.loc, "Foo")), types.ClassType)
        compare_types(type(self.type_store.get_type_of(self.loc, "Foo_instance")), types.InstanceType)
        compare_types(type(self.type_store.get_type_of(self.loc, "math")), types.ModuleType)

        compare_types(type(self.type_store.get_type_of(self.loc, "even_other_var")), types.ListType)

        self.type_store = self.type_store.close_function_context()  # End other_nested_func
        self.type_store = self.type_store.close_function_context()  # End nested_func

        res = self.type_store.get_type_of(self.loc, "even_other_var")
        assert_if_not_error(res)

        self.type_store = self.type_store.close_function_context()  # End new_func

        res = self.type_store.get_type_of(self.loc, "other_var")
        assert_if_not_error(res)
    def test_load_builtin(self):
        res = self.type_store.get_type_of(self.loc, "list")
        compare_types(res, types.ListType)

        res = self.type_store.get_type_of(self.loc, "bool")
        compare_types(res, types.BooleanType)

        res = self.type_store.get_type_of(self.loc, "None")
        compare_types(res, types.NoneType)

        res = self.type_store.get_type_of(self.loc, "dict")
        compare_types(res, types.DictType)

        res = self.type_store.get_type_of(self.loc, "ArithmeticError")
        compare_types(res, ArithmeticError)

        res = self.type_store.get_type_of(self.loc, "range")
        compare_types(type(res), types.BuiltinFunctionType)
Esempio n. 25
0
    def test_change_base_types(self):
        # Set a member on non-modifiable types
        union1 = UnionType.add(int, str)

        union1.__bases__ = (int, )

        self.assertTrue(len(StypyTypeError.get_error_msgs()) == 1)

        class Foo:
            def method(self):
                pass

            def method_2(self):
                pass

        class Foo2:
            def method(self):
                pass

            def method_2(self):
                pass

        # Set a member with some of the types of the union able to be modified
        union2 = UnionType.add(Foo(), str)

        union2.__bases__ = (Foo2, )
        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 1)

        # TypeWarning.print_warning_msgs()
        compare_types(union2.__bases__.types[0].contained_types, Foo2)
        compare_types(union2.__bases__.types[1].contained_types,
                      str.__bases__[0])

        TypeWarning.reset_warning_msgs()

        instance1 = Foo()
        union3 = UnionType.add(instance1, str)

        instance2 = Foo2()
        union3 = UnionType.add(instance2, union3)

        union3.__bases__ = (Foo2, )

        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 1)

        compare_types(union3.__bases__.types[0].contained_types, Foo2)
        compare_types(union3.__bases__.types[1].contained_types,
                      str.__bases__[0])

        #compare_types(union3.__bases__, [(Foo2,), str.__bases__])

        TypeWarning.reset_warning_msgs()
        StypyTypeError.reset_error_msgs()

        # Set a member using all-modifiable types
        union4 = UnionType.add(Foo(), Foo())

        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 0)

        union4.__bases__ = (Foo2, )
        self.assertTrue(len(StypyTypeError.get_error_msgs()) == 0)
        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 0)

        ret = union4.__bases__
        compare_types(ret, (Foo2, ))
Esempio n. 26
0
    def test_change_type(self):
        # Set a member on non-modifiable types
        union1 = UnionType.add(int, str)

        union1.__class__ = int

        self.assertTrue(len(StypyTypeError.get_error_msgs()) == 1)

        class Foo:
            def method(self):
                pass

            def method_2(self):
                pass

        class Foo2:
            def method(self):
                pass

            def method_2(self):
                pass

        # Set a member with some of the types of the union able to be modified
        union2 = UnionType.add(Foo(), str)

        union2.__class__ = Foo2
        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 1)

        # TypeWarning.print_warning_msgs()
        union_types = map(lambda x: type(x), union2.get_types())
        compare_types(union_types, [type(Foo2()), types.TypeType])

        TypeWarning.reset_warning_msgs()

        instance1 = Foo()
        union3 = UnionType.add(instance1, str)

        instance2 = Foo2()
        union3 = UnionType.add(instance2, union3)

        union3.__class__ = Foo2

        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 1)

        union_types = map(lambda x: type(x), union2.get_types())
        compare_types(union_types, [type(Foo2()), types.TypeType])

        TypeWarning.reset_warning_msgs()
        StypyTypeError.reset_error_msgs()

        # Set a member using all-modifiable types
        foo1 = Foo()
        foo2 = Foo()
        union4 = UnionType.add(foo1, foo2)

        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 0)

        self.assertTrue(len(StypyTypeError.get_error_msgs()) == 0)
        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 0)

        ret = union4
        compare_types(ret, foo1)
Esempio n. 27
0
    def test_set_type_of_member(self):
        # Set a member on non-modifiable types
        union1 = UnionType.add(int, str)

        union1.foo = int

        self.assertTrue(len(StypyTypeError.get_error_msgs()) == 1)

        class Foo:
            def method(self):
                pass

            def method_2(self):
                pass

        class Foo2:
            def method(self):
                pass

            def method_2(self):
                pass

        # Set a member with some of the types of the union able to be modified
        union2 = UnionType.add(Foo, str)

        union2.member = str
        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 1)

        # TypeWarning.print_warning_msgs()
        compare_types(union2.member, str)
        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 2)

        TypeWarning.reset_warning_msgs()

        instance1 = Foo()
        union3 = UnionType.add(instance1, str)

        instance2 = Foo2()
        union3 = UnionType.add(instance2, union3)

        union3.member = str

        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 1)

        compare_types(union3.member, str)

        TypeWarning.reset_warning_msgs()
        StypyTypeError.reset_error_msgs()

        # Set a member using all-modifiable types
        union4 = UnionType.add(Foo, Foo2)

        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 0)

        union4.member = float
        self.assertTrue(len(StypyTypeError.get_error_msgs()) == 0)
        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 0)

        ret = union4.member
        compare_types(ret, float)

        def obj_func(cls):
            pass

        union4.class_method = types.MethodType(obj_func, union4)

        ret = union4.class_method
        compare_types(ret, types.MethodType(obj_func, union4))
Esempio n. 28
0
    def test_get_type_of_member(self):
        context = Context(None, __file__)
        # Access a member that none of the stored types has
        union1 = UnionType.add(int, str)

        ret = union1.foo

        assert_if_not_error(ret)

        class Foo:
            att1 = int

            def method(self):
                pass

            def method_2(self):
                pass

        class Foo2:
            att1 = float

            def method(self):
                pass

            def method_2(self):
                pass

        # Access a member that can be provided only by some of the types in the union
        union2 = UnionType.add(Foo, str)

        ret = union2.method
        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 1)

        TypeWarning.reset_warning_msgs()
        compare_types(ret, Foo.method)

        instance1 = Foo()
        union3 = UnionType.add(instance1, str)

        instance2 = Foo2()
        union3 = UnionType.add(instance2, union3)

        ret = union3.method
        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 1)

        compare_types(ret, [
            instance1.method,
            instance2.method,
        ])

        TypeWarning.reset_warning_msgs()

        # Access a member that can be provided by all the types in the union
        union4 = UnionType.add(Foo, Foo2)

        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 0)

        ret = union4.method
        compare_types(ret, [Foo.method, Foo2.method])

        ret = union4.att1
        compare_types(ret, [int, float])

        # Access a member that can be provided by all the types in the union (using only Python types)
        union5 = UnionType.add(int, str)

        self.assertTrue(len(TypeWarning.get_warning_msgs()) == 0)

        context.set_type_of(self.loc, "union5", union5)

        # "__str__" is a special method so we have to use the context to access to it
        ret = context.get_type_of_member(self.loc, union5, "__str__")
        compare_types(ret, [int.__str__, str.__str__])
Esempio n. 29
0
    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 test_call_varargs(self):
        func = self.context.get_type_of(self.localization, "min")
        compare_types(type(func), types.BuiltinFunctionType)

        ret = invoke(self.localization, func, int(), int(), float(), list())
        compare_types(ret, [int(), float(), list()])