def binary_operation_has_child_names_resolved(): _assert_children_resolved( lambda ref: nodes.add(ref, nodes.none()), ) _assert_children_resolved( lambda ref: nodes.add(nodes.none(), ref), )
def return_type_of_add_can_differ_from_original_type(): cls = types.class_type("Addable", {}) cls.attrs.add("__add__", types.func([types.object_type], types.object_type)) type_bindings = {"x": cls, "y": cls} addition = nodes.add(nodes.ref("x"), nodes.ref("y")) assert_equal(types.object_type, infer(addition, type_bindings=type_bindings))
def type_of_add_method_argument_allows_super_type(): cls = types.class_type("Addable", {}) cls.attrs.add("__add__", types.func([types.object_type], cls)) type_bindings = {"x": cls, "y": cls} addition = nodes.add(nodes.ref("x"), nodes.ref("y")) assert_equal(cls, infer(addition, type_bindings=type_bindings))
def operands_of_add_operation_must_support_add(): type_bindings = {"x": types.none_type, "y": types.none_type} addition = nodes.add(nodes.ref("x"), nodes.ref("y")) try: infer(addition, type_bindings=type_bindings) assert False, "Expected error" except errors.NoSuchAttributeError as error: assert_equal(nodes.attr(addition.left, "__add__"), error.node) assert_equal(addition.left, ephemeral.root_node(error.node))
def right_hand_operand_must_be_sub_type_of_formal_argument(): type_bindings = {"x": types.int_type, "y": types.object_type} addition = nodes.add(nodes.ref("x"), nodes.ref("y")) try: infer(addition, type_bindings=type_bindings) assert False, "Expected error" except errors.UnexpectedValueTypeError as error: assert_equal(addition.right, error.node) assert_equal(types.int_type, error.expected) assert_equal(types.object_type, error.actual)
def cannot_add_int_and_str(): type_bindings = {"x": types.int_type, "y": types.str_type} addition = nodes.add(nodes.ref("x"), nodes.ref("y")) try: infer(addition, type_bindings=type_bindings) assert False, "Expected error" except errors.UnexpectedValueTypeError as error: assert_equal(addition.right, error.node) assert_equal(types.int_type, error.expected) assert_equal(types.str_type, error.actual)
def add_method_should_only_accept_one_argument(): cls = types.class_type("NotAddable", {}) cls.attrs.add("__add__", types.func([types.object_type, types.object_type], cls)) type_bindings = {"x": cls, "y": cls} addition = nodes.add(nodes.ref("x"), nodes.ref("y")) try: infer(addition, type_bindings=type_bindings) assert False, "Expected error" except errors.TypeCheckError as error: assert_equal(addition, ephemeral.root_node(error.node))
def can_infer_type_of_addition_operation(): type_bindings = {"x": types.int_type, "y": types.int_type} addition = nodes.add(nodes.ref("x"), nodes.ref("y")) assert_equal(types.int_type, infer(addition, type_bindings=type_bindings))
def test_parse_addition(): expected = nodes.add(nodes.ref("x"), nodes.ref("y")) _assert_expression_parse(expected, "x + y")
def test_transform_binary_operation_is_converted_to_call_on_class(self): _assert_transform( nodes.add(nodes.ref("x"), nodes.ref("y")), cc.call(cc.attr(cc.ref("x"), "__add__"), [cc.ref("y")]) )