Esempio n. 1
0
def can_infer_type_of_call_with_keyword_arguments():
    type_bindings = {
        "f": types.func(
            args=[types.func_arg("name", types.str_type), types.func_arg("hats", types.int_type)],
            return_type=types.bool_type,
        )
    }
    node = nodes.call(nodes.ref("f"), [], {"name": nodes.str_literal("Bob"), "hats": nodes.int_literal(42)})
    assert_equal(types.bool_type, infer(node, type_bindings=type_bindings))
Esempio n. 2
0
def can_infer_type_of_call_with_specified_optional_argument_after_unspecified_optional_argument():
    type_bindings = {
        "f": types.func(
            args=[
                types.func_arg("x", types.str_type, optional=True),
                types.func_arg("y", types.int_type, optional=True),
            ],
            return_type=types.bool_type,
        )
    }
    node = nodes.call(nodes.ref("f"), [], {"y": nodes.int_literal(42)})
    assert_equal(types.bool_type, infer(node, type_bindings=type_bindings))
Esempio n. 3
0
def if_positional_has_name_then_that_name_is_used_in_missing_argument_message():
    node = _create_call([])
    try:
        _infer_function_call(types.func([types.func_arg("message", types.str_type)], types.int_type), node)
        assert False, "Expected error"
    except errors.ArgumentsError as error:
        assert_is(node, error.node)
        assert_equal("missing argument 'message'", str(error))
Esempio n. 4
0
 def test_transform_call_with_keyword_arguments(self):
     func_node = nodes.ref("f")
     type_lookup = [
         (func_node, types.func(
             [
                 types.func_arg("first", types.str_type),
                 types.func_arg("second", types.str_type),
             ],
             types.none_type
         ))
     ]
     
     _assert_transform(
         nodes.call(func_node, [], {"first": nodes.ref("x"), "second": nodes.ref("y")}),
         cc.call(cc.ref("f"), [cc.ref("x"), cc.ref("y")]),
         type_lookup=type_lookup,
     )
Esempio n. 5
0
def can_infer_type_of_call_with_optional_argument_specified():
    type_bindings = {
        "f": types.func(
            args=[types.func_arg(None, types.str_type, optional=True)],
            return_type=types.bool_type,
        )
    }
    node = nodes.call(nodes.ref("f"), [nodes.str_literal("blah")])
    assert_equal(types.bool_type, infer(node, type_bindings=type_bindings))
Esempio n. 6
0
def error_if_argument_is_passed_both_by_position_and_keyword():
    node = _create_call([nodes.str_literal("Jim")], {"name": nodes.str_literal("Bob")})
    
    func_type = types.func(
        args=[types.func_arg("name", types.str_type)],
        return_type=types.bool_type,
    )
    try:
        _infer_function_call(func_type, node)
        assert False, "Expected error"
    except errors.ArgumentsError as error:
        assert_is(node, error.node)
        assert_equal("multiple values for argument 'name'", str(error))
Esempio n. 7
0
 def test_transform_call_with_optional_positional_argument(self):
     func_node = nodes.ref("f")
     type_lookup = [
         (func_node, types.func(
             [types.str_type, types.func_arg(None, types.str_type, optional=True)],
             types.none_type
         ))
     ]
     
     _assert_transform(
         nodes.call(func_node, [nodes.ref("x")]),
         cc.call(cc.ref("f"), [cc.ref("x"), cc.none]),
         type_lookup=type_lookup,
     )
def can_infer_type_of_function_with_optional_arg():
    signature = nodes.signature(
        args=[
            nodes.signature_arg(nodes.ref("int"), optional=True),
        ],
        returns=nodes.ref("none")
    )
    args = nodes.arguments([
        nodes.argument("x", optional=True),
    ])
    node = nodes.func("f", args=args, body=[], type=signature)
    assert_equal(
        types.func([types.func_arg(None, types.int_type, optional=True)], types.none_type),
        _infer_func_type(node)
    )
def can_infer_type_of_function_with_named_arg():
    signature = nodes.signature(
        args=[
            nodes.signature_arg("message", nodes.ref("int")),
        ],
        returns=nodes.ref("none")
    )
    args = nodes.arguments([
        nodes.argument("message"),
    ])
    node = nodes.func("f", args=args, body=[], type=signature)
    assert_equal(
        types.func([types.func_arg("message", types.int_type)], types.none_type),
        _infer_func_type(node)
    )
Esempio n. 10
0
def type_of_keyword_arguments_must_match():
    node = nodes.call(nodes.ref("f"), [], {"name": nodes.str_literal("Bob"), "hats": nodes.int_literal(42)})
    
    type_bindings = {
        "f": types.func(
            args=[types.func_arg("name", types.str_type)],
            return_type=types.bool_type,
        )
    }
    arg_node = nodes.int_literal(4)
    node = nodes.call(nodes.ref("f"), [], {"name": arg_node})
    assert_type_mismatch(
        lambda: infer(node, type_bindings=type_bindings),
        expected=types.str_type,
        actual=types.int_type,
        node=arg_node,
    )
Esempio n. 11
0
 def func_type_is_sub_type_if_argument_of_super_type_has_no_name_and_sub_type_has_name(self):
     first_func_type = types.func([types.func_arg(None, int_type)], str_type)
     second_func_type = types.func([types.func_arg("y", int_type)], str_type)
     
     assert types.is_sub_type(first_func_type, second_func_type)
     assert not types.is_sub_type(second_func_type, first_func_type)
Esempio n. 12
0
 def func_type_is_not_sub_type_if_argument_has_different_name(self):
     first_func_type = types.func([types.func_arg("x", int_type)], str_type)
     second_func_type = types.func([types.func_arg("y", int_type)], str_type)
     
     assert not types.is_sub_type(first_func_type, second_func_type)
     assert not types.is_sub_type(second_func_type, first_func_type)