Beispiel #1
0
def __remove_not_subtype_from_union_implementation(if_test, type_, lineno,
                                                   col_offset):
    """
    Implements the call to remove_not_subtype_from_union when the idiom is recognized
    :param if_test:
    :param type_:
    :param lineno:
    :param col_offset:
    :return:
    """
    param = __get_idiom_type_param(if_test)
    if type(param) is ast.Name:
        # obj_type, obj_var = stypy_functions.create_get_type_of(param.id, lineno, col_offset)
        remove_type_call = functions.create_call(
            core_language.create_Name("remove_not_subtype_from_union"),
            [type_, if_test.args[1]],
            line=lineno,
            column=col_offset)
        set_type = stypy_functions.create_set_type_of(param.id,
                                                      remove_type_call, lineno,
                                                      col_offset)

        return stypy_functions.flatten_lists(set_type)  # obj_type, set_type)
    if type(param) is ast.Attribute:
        # try:
        # Get the owner of the attribute
        if type(param.value) is ast.Name:
            obj_type_stmts, obj_var = stypy_functions.create_get_type_of(
                param.value.id, lineno, col_offset)
        else:
            obj_type_stmts, obj_var = stypy_functions.create_get_type_of_member(
                param.value, param.value.attr, lineno, col_offset)
        # except Exception as ex:
        #     print ex

        # Get the current type of the owner of the attribute
        att_type_stmts, att_var = stypy_functions.create_get_type_of_member(
            obj_var, param.attr, lineno, col_offset)
        remove_type_call = functions.create_call(
            core_language.create_Name("remove_not_subtype_from_union"),
            [type_, if_test.args[1]],
            line=lineno,
            column=col_offset)
        set_member = stypy_functions.create_set_type_of_member(
            obj_var, param.attr, remove_type_call, lineno, col_offset)
        return stypy_functions.flatten_lists(obj_type_stmts, att_type_stmts,
                                             set_member)

    return []
def create_type_for_lambda_function(function_name, lambda_call, lineno,
                                    col_offset):
    """
    Creates a variable to store a lambda function definition

    :param function_name: Name of the lambda function
    :param lambda_call: Lambda function
    :param lineno: Line
    :param col_offset: Column
    :return: Statements to create the lambda function type
    """

    call_arg = core_language.create_Name(lambda_call)

    set_type_stmts = stypy_functions.create_set_type_of(
        function_name, call_arg, lineno, col_offset)

    # return stypy_functions.flatten_lists(assign, set_type_stmts)
    return stypy_functions.flatten_lists(set_type_stmts)
def create_context_unset():
    """
    Creates an AST Node that model the call to the type_store.unset_context method

    :return: An AST Expr node
    """

    # Generate code for pop the function context.
    comment = stypy_functions.create_src_comment("Destroy the current context")

    # Code to pop a stack trace once the function finishes.
    attribute = core_language.create_attribute(
        stypy_functions.default_module_type_store_var_name,
        "close_function_context")
    context_unset_call = create_call(attribute, [])

    left_hand_side = core_language.create_Name(
        stypy_functions.default_module_type_store_var_name, False)
    assign_statement = ast.Assign([left_hand_side], context_unset_call)

    return stypy_functions.flatten_lists(comment, assign_statement)
Beispiel #4
0
def __set_type_implementation(if_test, type_, lineno, col_offset):
    """
    Assigns the type of the condition when the idiom is recognized
    :param if_test:
    :param type_:
    :param lineno:
    :param col_offset:
    :return:
    """
    param = __get_idiom_type_param(if_test)
    type_ = functions.create_call(type_, [], line=lineno, column=col_offset)
    if type(param) is ast.Name:
        return stypy_functions.create_set_type_of(param.id, type_, lineno,
                                                  col_offset)
    if type(param) is ast.Attribute:
        obj_type, obj_var = stypy_functions.create_get_type_of(
            param.value.id, lineno, col_offset)
        set_member = stypy_functions.create_set_type_of_member(
            obj_var, param.attr, type_, lineno, col_offset)
        return stypy_functions.flatten_lists(obj_type, set_member)

    return []