def assignment_to_tuple_declares_variables_in_tuple(): first_definition_node = nodes.ref("x") second_definition_node = nodes.ref("y") node = nodes.assign([nodes.tuple_literal([first_definition_node, second_definition_node])], nodes.none()) declarations = find_declarations(node) assert_equal("x", declarations.declaration("x").name) assert_equal("y", declarations.declaration("y").name) assert isinstance(declarations.declaration("x"), name_declaration.VariableDeclarationNode) assert isinstance(declarations.declaration("y"), name_declaration.VariableDeclarationNode)
def assignment_to_tuple_unpacks_tuple_type(): node = nodes.assign( [nodes.tuple_literal([nodes.ref("x"), nodes.ref("y")])], nodes.ref("value") ) context = update_context(node, type_bindings={ "value": types.tuple_type(types.int_type, types.str_type), }) assert_equal(types.int_type, context.lookup_name("x")) assert_equal(types.str_type, context.lookup_name("y"))
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")
def test_tuple_assignment(self): _assert_transform( nodes.assign( [nodes.tuple_literal([nodes.ref("x"), nodes.ref("y")])], nodes.ref("z") ), """ var __nope_u_tmp0 = z x = __nope_u_tmp0.__getitem__(0) y = __nope_u_tmp0.__getitem__(1) """ )
def assignment_to_tuple_must_have_correct_length_tuple(): tuple_node = nodes.tuple_literal([nodes.ref("x"), nodes.ref("y")]) node = nodes.assign( [tuple_node], nodes.ref("value") ) try: update_context(node, type_bindings={ "value": types.tuple_type(types.int_type), }) assert False, "Expected error" except errors.UnpackError as error: assert_equal(tuple_node, error.node) assert_equal("need 2 values to unpack, but only have 1" , str(error))
def assignment_to_tuple_must_have_tuple_value(): tuple_node = nodes.tuple_literal([nodes.ref("x")]) node = nodes.assign( [tuple_node], nodes.ref("value") ) try: update_context(node, type_bindings={ "value": types.list_type(types.int_type), }) assert False, "Expected error" except errors.CanOnlyUnpackTuplesError as error: assert_equal(tuple_node, error.node) assert_equal("only tuples can be unpacked" , str(error))
def test_parse_tuple_literal(): _assert_expression_parse( nodes.tuple_literal([nodes.str_literal("hello"), nodes.int_literal(4)]), "('hello', 4)" )
def variable_in_tuple_is_definitely_bound_after_assignment(): target_node = nodes.ref("x") node = nodes.assign([nodes.tuple_literal([target_node])], nodes.tuple_literal([nodes.none()])) bindings = _updated_bindings(node) assert_equal(True, bindings.is_definitely_bound(target_node))
def tuple_containing_literal_is_literal(self): assert is_literal(nodes.tuple_literal([nodes.none()]))
def tuple_containing_reference_is_not_literal(self): assert not is_literal(nodes.tuple_literal([nodes.ref("x")]))
def empty_tuple_is_literal(self): assert is_literal(nodes.tuple_literal([]))
def test_transform(self): _assert_transform( nodes.tuple_literal([nodes.none()]), cc.tuple_literal([cc.none]) )
def can_infer_type_of_list_of_two_tuple(): assert_equal( types.tuple_type(types.int_type, types.str_type), infer(nodes.tuple_literal([nodes.int_literal(1), nodes.str_literal("42")])) )