Пример #1
0
def subscript_has_child_names_resolved():
    _assert_children_resolved(
        lambda ref: nodes.subscript(ref, nodes.none()),
    )
    _assert_children_resolved(
        lambda ref: nodes.subscript(nodes.none(), ref),
    )
Пример #2
0
def can_infer_type_of_subscript_using_getitem():
    cls = types.class_type("Blah", [
        types.attr("__getitem__", types.func([types.int_type], types.str_type)),
    ])
    type_bindings = {"x": cls}
    node = nodes.subscript(nodes.ref("x"), nodes.int_literal(4))
    assert_equal(types.str_type, infer(node, type_bindings=type_bindings))
Пример #3
0
 def test_transform_setitem_subscript(self):
     _assert_transform(
         nodes.assign([nodes.subscript(nodes.ref("x"), nodes.ref("y"))], nodes.ref("z")),
         """
             var __nope_u_tmp0 = z
             x.__setitem__(y, __nope_u_tmp0)
         """
     )
Пример #4
0
def for_statement_target_can_be_supertype_of_iterable_element_type():
    ref_node = nodes.ref("xs")
    node = nodes.for_(nodes.subscript(nodes.ref("ys"), nodes.int_literal(0)), ref_node, [])
    
    update_context(node, type_bindings={
        "xs": types.list_type(types.int_type),
        "ys": types.list_type(types.object_type),
    })
Пример #5
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)
Пример #6
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)
Пример #7
0
def can_infer_type_of_subscript_of_list():
    type_bindings = {"x": types.list_type(types.str_type)}
    node = nodes.subscript(nodes.ref("x"), nodes.int_literal(4))
    assert_equal(types.str_type, infer(node, type_bindings=type_bindings))
Пример #8
0
def test_missing_subscript_slice_values_are_treated_as_none():
    expected = nodes.subscript(nodes.ref("x"), nodes.slice(nodes.none(), nodes.none(), nodes.none()))
    _assert_expression_parse(expected, "x[:]")
Пример #9
0
def test_parse_subscript_with_slice():
    expected = nodes.subscript(
        nodes.ref("x"),
        nodes.slice(nodes.ref("a"), nodes.ref("b"), nodes.ref("c")),
    )
    _assert_expression_parse(expected, "x[a:b:c]")
Пример #10
0
def test_parse_subscript_with_index():
    expected = nodes.subscript(nodes.ref("x"), nodes.ref("y"))
    _assert_expression_parse(expected, "x[y]")
Пример #11
0
def assignment_to_list_allows_subtype():
    node = nodes.assign([nodes.subscript(nodes.ref("x"), nodes.int_literal(0))], nodes.str_literal("Hello"))
    type_bindings = {"x": types.list_type(types.object_type)}
    update_context(node, type_bindings=type_bindings)
Пример #12
0
 def test_transform_getitem(self):
     _assert_transform(
         nodes.subscript(nodes.ref("x"), nodes.ref("y")),
         cc.call(cc.attr(cc.ref("x"), "__getitem__"), [cc.ref("y")])
     )