def test_local_variable_turned_global_before(self):
        self.type_store.declare_global(Localization(__file__, 1, 1), "global_var")
        self.type_store.set_type_of(Localization(__file__, 2, 2), "global_var", int)

        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__, 3, 3), "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__, 4, 4), "global_var")
        assert len(Advice.get_advice_msgs()) == 2

        self.type_store.declare_global(self.loc, "non_existing")

        assert_if_not_error(self.type_store.get_type_of(Localization(__file__, 5, 5), "non_existing"))

        self.type_store.declare_global(Localization(__file__, 6, 6), "foo")
        self.type_store.set_type_of(Localization(__file__, 7, 7), "foo", int)
        assert len(Advice.get_advice_msgs()) == 4

        self.type_store.declare_global(Localization(__file__, 8, 8), "foo_func")
        self.type_store.set_type_of(Localization(__file__, 9, 9), "foo_func", types.FunctionType)
        assert len(Advice.get_advice_msgs()) == 5

        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)
        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_use_global_in_global_context_after_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.declare_global(Localization(__file__, 1, 1), "global_var")
        self.type_store.declare_global(Localization(__file__, 2, 2), "global_func")

        compare_types(self.type_store.get_type_of(Localization(__file__, 3, 3), "global_var"), types.IntType)
        compare_types(self.type_store.get_type_of(Localization(__file__, 4, 4), "global_func"), types.FunctionType)

        assert len(Advice.get_advice_msgs()) == 2
    def test_unexisting_global_variable_in_func(self):
        self.type_store = self.type_store.open_function_context("nested_func")

        self.type_store.declare_global(self.loc, "non_existing")
        assert len(Advice.get_advice_msgs()) == 1

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

        self.type_store = self.type_store.close_function_context()
    def stypy____setattr__(localization, proxy_obj, arguments):
        attr_name = arguments[0]
        attr_value = arguments[1]

        if attr_name is str():
            Advice(localization,
                   "Called __setattr__ without a value in the member name parameter: the operation cannot be checked")
            return types.NoneType

        return set_member(localization, get_self(proxy_obj), attr_name, attr_value)
    def stypy____delattr__(localization, proxy_obj, arguments):
        attr_name = arguments[0]

        if attr_name is str():
            Advice(localization,
                   "Called __delattr__ without a value in the member name parameter: the operation cannot be checked")
            return types.NoneType
        try:
            del_member(localization, get_self(proxy_obj), attr_name)
        except Exception as exc:
            return StypyTypeError.member_cannot_be_deleted_error(localization, get_self(proxy_obj), attr_name, str(exc))

        return types.NoneType
    def test_wrong_global_keyword_in_func(self):
        self.type_store.declare_global(Localization(__file__, 1, 1), "global_var")
        self.type_store.declare_global(Localization(__file__, 2, 2), "global_func")

        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")
        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 + 2

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

        self.type_store.declare_global(Localization(__file__, 4, 4), "global_func")
        assert len(Advice.get_advice_msgs()) == 4

        self.type_store = self.type_store.close_function_context()
    def test_use_global_in_global_context(self):
        num_warnings_before = len(TypeWarning.get_warning_msgs())
        self.type_store.declare_global(Localization(__file__, 1, 1), "global_var")
        self.type_store.declare_global(Localization(__file__, 2, 2), "global_func")

        self.type_store.set_type_of(Localization(__file__, 3, 3), "global_var", int)
        self.type_store.set_type_of(Localization(__file__, 4, 4), "global_func", types.FunctionType)

        compare_types(self.type_store.get_type_of(Localization(__file__, 5, 5), "global_var"), types.IntType)
        compare_types(self.type_store.get_type_of(Localization(__file__, 6, 6), "global_func"), types.FunctionType)
        num_warnings_after = len(TypeWarning.get_warning_msgs())

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

        assert num_warnings_after == num_warnings_before
 def setUp(self):
     self.loc = Localization(__file__)
     self.type_store = Context(None, __file__)
     StypyTypeError.reset_error_msgs()
     TypeWarning.reset_warning_msgs()
     Advice.reset_advice_msgs()