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)
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)
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)
def test_root_node_of_ephemeral_attr_is_left_value_if_left_value_is_not_ephemeral(): ref_node = nodes.ref("x") assert_is(ref_node, ephemeral.root_node(ephemeral.attr(ref_node, "__len__")))
def test_root_node_of_ephemeral_node_recurses_up_tree_until_non_ephemeral_node(): ref_node = nodes.ref("x") node = ephemeral.attr(ephemeral.attr(ref_node, "__call__"), "__call__") assert_is(ref_node, ephemeral.root_node(node))