Exemplo n.º 1
0
def assignment_to_list_does_not_allow_supertype():
    target_sequence_node = nodes.ref("x")
    value_node = nodes.ref("y")
    node = nodes.assign([nodes.subscript(target_sequence_node, nodes.int_literal(0))], value_node)
    type_bindings = {
        "x": types.list_type(types.str_type),
        "y": types.object_type,
    }
    try:
        update_context(node, type_bindings=type_bindings)
        assert False, "Expected error"
    except errors.UnexpectedTargetTypeError as error:
        assert_equal(target_sequence_node, ephemeral.root_node(error.node))
        assert_equal(
            ephemeral.FormalArg(ephemeral.attr(target_sequence_node, "__setitem__"), 1),
            ephemeral.underlying_node(error.node)
        )
        assert_equal(types.object_type, error.value_type)
        assert_equal(types.str_type, error.target_type)
Exemplo n.º 2
0
def for_statement_target_cannot_be_strict_subtype_of_iterable_element_type():
    target_sequence_node = nodes.ref("ys")
    target_node = nodes.subscript(target_sequence_node, nodes.int_literal(0))
    iterable_node = nodes.ref("xs")
    node = nodes.for_(target_node, iterable_node, [])
    
    try:
        update_context(node, type_bindings={
            "xs": types.list_type(types.object_type),
            "ys": types.list_type(types.int_type),
        })
        assert False, "Expected error"
    except errors.UnexpectedTargetTypeError as error:
        assert_equal(target_sequence_node, ephemeral.root_node(error.node))
        assert_equal(
            ephemeral.FormalArg(ephemeral.attr(target_sequence_node, "__setitem__"), 1),
            ephemeral.underlying_node(error.node)
        )
        assert_equal(types.object_type, error.value_type)
        assert_equal(types.int_type, error.target_type)
Exemplo n.º 3
0
def for_statement_requires_iterable_getitem_method_to_accept_integers():
    cls = types.class_type("Blah")
    cls.attrs.add("__getitem__", types.func([types.str_type], types.str_type))
    
    ref_node = nodes.ref("xs")
    node = nodes.for_(nodes.ref("x"), ref_node, [])
    
    type_bindings = {
        "xs": cls,
    }
    
    try:
        update_context(node, type_bindings=type_bindings)
        assert False, "Expected error"
    except errors.UnexpectedTargetTypeError as error:
        assert_equal(ref_node, ephemeral.root_node(error.node))
        assert_equal(
            ephemeral.FormalArg(ephemeral.attr(ref_node, "__getitem__"), 0),
            ephemeral.underlying_node(error.node)
        )
        assert_equal(types.int_type, error.value_type)
        assert_equal(types.str_type, error.target_type)