示例#1
0
def list_comprehension_has_child_names_resolved():
    _assert_children_resolved(
        lambda ref: nodes.list_comprehension(ref, nodes.none(), nodes.none()),
    )
    _assert_children_resolved(
        lambda ref: nodes.list_comprehension(nodes.none(), nodes.attr(ref, "name"), nodes.none()),
    )
    _assert_children_resolved(
        lambda ref: nodes.list_comprehension(nodes.none(), nodes.none(), ref),
    )
示例#2
0
def list_comprehension_target_is_definitely_bound():
    node = nodes.list_comprehension(
        nodes.ref("x"),
        nodes.ref("x"),
        nodes.list_literal([]),
    )
    
    _updated_bindings(node)
示例#3
0
def list_comprehension_generator_is_not_in_same_scope_as_element():
    target = nodes.ref("target")
    ref = nodes.ref("target")
    iterable = nodes.ref("target")
    node = nodes.list_comprehension(target, ref, iterable)
    
    declarations = _create_declarations(["target"])
    references = resolve(node, declarations)
    assert_is_not(references.referenced_declaration(target), references.referenced_declaration(iterable))
示例#4
0
def list_comprehension_adds_target_names_to_body_context():
    target = nodes.ref("target")
    ref = nodes.ref("target")
    node = nodes.list_comprehension(target, ref, nodes.none())
    
    declarations = _create_declarations([])
    references = resolve(node, declarations)
    assert not declarations.is_declared("target")
    assert_is(references.referenced_declaration(target), references.referenced_declaration(ref))
示例#5
0
def test_parse_single_list_comprehension():
    _assert_expression_parse(
        nodes.list_comprehension(
            nodes.call(nodes.ref("f"), [nodes.ref("x")]),
            nodes.ref("x"),
            nodes.ref("xs"),
        ),
        "[f(x) for x in xs]"
    )
def declarations_in_list_comprehension_are_variable_reference_targets():
    node = nodes.list_comprehension(
        nodes.none(),
        nodes.tuple_literal([nodes.ref("target"), nodes.attr(nodes.ref("other"), "name")]),
        nodes.ref("iterable")
    )
    
    declarations = _declarations_in(node)
    assert isinstance(declarations.declaration("target"), name_declaration.VariableDeclarationNode)
    assert not declarations.is_declared("other")
    assert not declarations.is_declared("iterable")
示例#7
0
def can_infer_type_of_list_comprehension_over_list():
    type_bindings = {"xs": types.list_type(types.str_type)}
    node = nodes.list_comprehension(
        nodes.int_literal(1),
        nodes.ref("x"),
        nodes.ref("xs"),
    )
    assert_equal(
        types.list_type(types.int_type),
        infer(node, type_bindings=type_bindings)
    )
示例#8
0
def target_of_comprehension_is_available_in_element():
    type_bindings = {"xs": types.list_type(types.str_type)}
    node = nodes.list_comprehension(
        nodes.ref("x"),
        nodes.ref("x"),
        nodes.ref("xs"),
    )
    assert_equal(
        types.list_type(types.str_type),
        infer(node, type_bindings=type_bindings)
    )
示例#9
0
def children_of_list_comprehension_are_checked():
    _assert_child_expression_is_checked(lambda generate:
        nodes.list_comprehension(
            generate.unbound_ref(),
            generate.bound_ref("x", types.int_type),
            nodes.list_literal([]),
        )
    )
    _assert_child_expression_is_checked(lambda generate:
        nodes.list_comprehension(
            nodes.none(),
            nodes.attr(generate.unbound_ref(), "x"),
            nodes.list_literal([]),
        )
    )
    _assert_child_expression_is_checked(lambda generate:
        nodes.list_comprehension(
            nodes.none(),
            generate.bound_ref("x", types.int_type),
            generate.unbound_ref(),
        )
    )
示例#10
0
 def test_list_comprehension_is_transformed_as_with_generator_expression_but_wrapped_in_list_call(self):
     _assert_transform(
         nodes.list_comprehension(
             nodes.ref("y"),
             nodes.ref("x"),
             nodes.ref("xs")
         ),
         cc.call(
             cc.internal("iterator_to_list"),
             [
                 cc.call(
                     cc.internal("generator_expression"),
                     [
                         cc.function_expression([cc.arg("x")], [cc.ret(cc.ref("y"))]),
                         cc.ref("xs")
                     ]
                 )
             ]
         )
     )