Ejemplo n.º 1
0
def assign_single_target(target, assigned_type, static_value, context):
    target_token = expr.get_token(target)
    if target_token == 'Name':
        old_symbol = context.get(target.id)
        new_symbol = Symbol(target.id, assigned_type, static_value)
        context.add(new_symbol)
        return (target.id, old_symbol, new_symbol)
    elif target_token == 'Subscript':
        _ = expr.expression_type(target.value, context)
        # TODO: implement this
        return ('Subscript', None, None)
    elif target_token == 'Attribute':
        instance = expr.expression_type(target.value, context)
        if not isinstance(instance, Instance):
            return (target.attr, None, None)
        else:
            old_symbol = instance.attributes.get(target.attr)
            new_symbol = Symbol(target.attr, assigned_type, static_value)
            instance.attributes.add(new_symbol)
            return (target.attr, old_symbol, new_symbol)
    elif target_token == 'Tuple':
        # TODO: implement this
        return ('Tuple', None, None)
    else:
        raise RuntimeError('Unrecognized assignment target: ' + target_token)
Ejemplo n.º 2
0
def assign_single_target(target, assigned_type, static_value, context):
    target_token = expr.get_token(target)
    if target_token == 'Name':
        old_symbol = context.get(target.id)
        new_symbol = Symbol(target.id, assigned_type, static_value)
        context.add(new_symbol)
        return (target.id, old_symbol, new_symbol)
    elif target_token == 'Subscript':
        _ = expr.expression_type(target.value, context)
        # TODO: implement this
        return ('Subscript', None, None)
    elif target_token == 'Attribute':
        instance = expr.expression_type(target.value, context)
        if not isinstance(instance, Instance):
            return (target.attr, None, None)
        else:
            old_symbol = instance.attributes.get(target.attr)
            new_symbol = Symbol(target.attr, assigned_type, static_value)
            instance.attributes.add(new_symbol)
            return (target.attr, old_symbol, new_symbol)
    elif target_token == 'Tuple':
        # TODO: implement this
        return ('Tuple', None, None)
    else:
        raise RuntimeError('Unrecognized assignment target: ' + target_token)
Ejemplo n.º 3
0
def assign(target, value, context, warnings, generator=False):
    value_type = expr.visit_expression(value, Unknown(), context, warnings)
    static_value = static_evaluate(value, context)
    if generator:
        if isinstance(value_type, (List, Set)):
            assign_type = value_type.item_type
        elif isinstance(value_type, Tuple):
            assign_type = Unknown()       # TODO
        else:
            assign_type = Unknown()
    else:
        assign_type = value_type

    target_token = expr.get_token(target)
    if target_token in ('Tuple', 'List'):
        values = static_value if isinstance(static_value,
                                            (Tuple, List)) else []
        assign_values = chain(values, repeat(UnknownValue()))
        if isinstance(assign_type, Tuple):
            assign_types = chain(assign_type.item_types, repeat(Unknown()))
        elif isinstance(assign_type, (List, Set)):
            assign_types = repeat(assign_type.item_type)
        else:
            assign_types = repeat(Unknown())
        return [
            assign_single_target(target, assign_type, static_value, context)
            for target, assign_type, static_value
            in zip(target.elts, assign_types, assign_values)]
    else:
        return [assign_single_target(target, assign_type,
                                     static_value, context)]
Ejemplo n.º 4
0
def assign(target, value, context, warnings, generator=False):
    value_type = expr.visit_expression(value, Unknown(), context, warnings)
    static_value = static_evaluate(value, context)
    if generator:
        if isinstance(value_type, (List, Set)):
            assign_type = value_type.item_type
        elif isinstance(value_type, Tuple):
            assign_type = Unknown()  # TODO
        else:
            assign_type = Unknown()
    else:
        assign_type = value_type

    target_token = expr.get_token(target)
    if target_token in ('Tuple', 'List'):
        values = static_value if isinstance(static_value,
                                            (Tuple, List)) else []
        assign_values = chain(values, repeat(UnknownValue()))
        if isinstance(assign_type, Tuple):
            assign_types = chain(assign_type.item_types, repeat(Unknown()))
        elif isinstance(assign_type, (List, Set)):
            assign_types = repeat(assign_type.item_type)
        else:
            assign_types = repeat(Unknown())
        return [
            assign_single_target(target, assign_type, static_value, context)
            for target, assign_type, static_value in zip(
                target.elts, assign_types, assign_values)
        ]
    else:
        return [
            assign_single_target(target, assign_type, static_value, context)
        ]