def try_statement_has_child_names_resolved():
    _assert_children_resolved(
        lambda ref: nodes.try_([nodes.expression_statement(ref)]),
    )
    _assert_children_resolved(
        lambda ref: nodes.try_([], handlers=[
            nodes.except_(None, None, [nodes.expression_statement(ref)])
        ]),
    )
    _assert_children_resolved(
        lambda ref: nodes.try_([], handlers=[
            nodes.except_(ref, None, [])
        ]),
    )
    _assert_children_resolved(
        lambda ref: nodes.try_([], handlers=[
            nodes.except_(nodes.ref("Exception"), ref, [])
        ]),
        other_names=["Exception"],
    )
    _assert_children_resolved(
        lambda ref: nodes.try_([], finally_body=[
            nodes.expression_statement(ref)
        ]),
    )
Beispiel #2
0
def test_parse_try_finally():
    expected = nodes.try_(
        [nodes.expression_statement(nodes.ref("x"))],
        finally_body=[nodes.expression_statement(nodes.ref("y"))],
    )
    
    _assert_statement_parse(expected, "try:\n  x\nfinally:\n  y")
def while_loop_has_child_names_resolved():
    _assert_children_resolved(
        lambda ref: nodes.while_(ref, [], []),
    )
    _assert_children_resolved(
        lambda ref: nodes.while_(nodes.bool_literal(True), [nodes.expression_statement(ref)], []),
    )
    _assert_children_resolved(
        lambda ref: nodes.while_(nodes.bool_literal(True), [], [nodes.expression_statement(ref)]),
    )
Beispiel #4
0
def test_parse_try_except_that_catches_all_exceptions():
    expected = nodes.try_(
        [nodes.expression_statement(nodes.ref("x"))],
        handlers=[
            nodes.except_(None, None, [
                nodes.expression_statement(nodes.ref("y"))
            ]),
        ]
    )
    
    _assert_statement_parse(expected, "try:\n  x\nexcept:\n  y")
Beispiel #5
0
def test_parse_try_except_with_specific_type_and_identifier():
    expected = nodes.try_(
        [nodes.expression_statement(nodes.ref("x"))],
        handlers=[
            nodes.except_(nodes.ref("AssertionError"), "error", [
                nodes.expression_statement(nodes.ref("y"))
            ]),
        ]
    )
    
    _assert_statement_parse(expected, "try:\n  x\nexcept AssertionError as error:\n  y")
def for_loop_has_child_names_resolved():
    _assert_children_resolved(
        lambda ref: nodes.for_(ref, nodes.list_literal([]), [], []),
    )
    _assert_children_resolved(
        lambda ref: nodes.for_(nodes.ref("target"), ref, [], []),
        other_names=["target"]
    )
    _assert_children_resolved(
        lambda ref: nodes.for_(nodes.ref("target"), nodes.list_literal([]), [nodes.expression_statement(ref)], []),
        other_names=["target"]
    )
    _assert_children_resolved(
        lambda ref: nodes.for_(nodes.ref("target"), nodes.list_literal([]), [], [nodes.expression_statement(ref)]),
        other_names=["target"]
    )
def class_definition_bodies_can_access_variables_from_outer_scope():
    ref = nodes.ref("x")
    node = nodes.class_("User", [nodes.expression_statement(ref)])
    
    declarations = _create_declarations(["x", "User"])
    
    references = resolve(node, declarations)
    assert_is(declarations.declaration("x"), references.referenced_declaration(ref))
Beispiel #8
0
 def test_statements_in_module_body_are_transformed(self):
     module_node = nodes.module([nodes.expression_statement(nodes.ref("value"))], is_executable=True)
     module_type = types.module("blah", [])
     _assert_transform(
         module_node,
         cc.module([cc.expression_statement(cc.ref("value"))], is_executable=True, exported_names=[]),
         type_lookup=[(module_node, module_type)]
     )
Beispiel #9
0
def with_statement_target_is_definitely_bound_in_body():
    manager_ref = nodes.ref("manager")
    target_ref = nodes.ref("target")
    var_ref = nodes.ref("target")
    statement = nodes.with_(manager_ref, target_ref, [nodes.expression_statement(var_ref)])
    
    _updated_bindings(
        statement,
        is_definitely_bound={"manager": True},
        type_lookup=TypeLookup(NodeDict([(manager_ref, context_manager_class(exit_type=types.none_type))]))
    )
Beispiel #10
0
def function_cannot_be_referenced_before_definition():
    f = nodes.func("f", type=None, args=nodes.Arguments([]), body=[])
    ref_node = nodes.ref("f")
    
    try:
        _updated_bindings(nodes.module([
            nodes.expression_statement(ref_node),
            f,
        ]))
        assert False, "Expected error"
    except errors.UnboundLocalError as error:
        assert_equal(ref_node, error.node)
Beispiel #11
0
def except_handler_updates_type_of_error_target():
    node = nodes.try_([], handlers=[
        nodes.except_(
            nodes.ref("Exception"),
            "error",
            [nodes.expression_statement(nodes.ref("error"))]
        ),
    ])
    type_bindings = {
        "Exception": types.meta_type(types.exception_type)
    }
    context = update_context(node, type_bindings=type_bindings)
    assert_equal(types.exception_type, context.lookup_name("error"))
def names_in_module_are_defined_and_resolved():
    target_node = nodes.ref("x")
    ref_node = nodes.ref("x")
    
    node = nodes.module([
        nodes.assign([target_node], nodes.none()),
        nodes.expression_statement(ref_node),
    ])
    
    declarations = _create_declarations(["x"])
    
    references = resolve(node, declarations)
    assert_is(references.referenced_declaration(ref_node), references.referenced_declaration(target_node))
Beispiel #13
0
def class_cannot_be_referenced_if_method_dependencies_are_not_bound():
    g_ref = nodes.ref("g")
    f = nodes.func("f", type=None, args=nodes.Arguments([]), body=[
        nodes.ret(nodes.call(g_ref, []))
    ])
    g = nodes.func("g", type=None, args=nodes.Arguments([]), body=[])
    
    try:
        _updated_bindings(nodes.module([
            nodes.class_("a", [f]),
            nodes.expression_statement(nodes.ref("a")),
            g,
        ]))
        assert False, "Expected error"
    except errors.UnboundLocalError as error:
        assert_equal(g_ref, error.node)
Beispiel #14
0
def function_cannot_be_referenced_before_definition_of_dependencies():
    g_ref = nodes.ref("g")
    f = nodes.func("f", type=None, args=nodes.Arguments([]), body=[
        nodes.ret(nodes.call(g_ref, []))
    ])
    g = nodes.func("g", type=None, args=nodes.Arguments([]), body=[])
    
    try:
        _updated_bindings(nodes.module([
            f,
            nodes.expression_statement(nodes.ref("f")),
            g,
        ]))
        assert False, "Expected error"
    except errors.UnboundLocalError as error:
        assert_equal(g_ref, error.node)
Beispiel #15
0
def test_parse_expression_statement():
    expected = nodes.expression_statement(nodes.ref("x"))
    _assert_statement_parse(expected, "x")
Beispiel #16
0
def assert_statement_is_type_checked(create_node, type_bindings=None):
    assert_expression_is_type_checked(
        lambda bad_expression: create_node(nodes.expression_statement(bad_expression)),
        type_bindings=type_bindings,
    )
Beispiel #17
0
def function_can_be_referenced_immediately_after_definition():
    f = nodes.func("f", type=None, args=nodes.Arguments([]), body=[])
    _updated_bindings(nodes.module([f, nodes.expression_statement(nodes.ref("f"))]))
Beispiel #18
0
def variables_from_outer_scope_remain_bound():
    ref = nodes.ref("x")
    func_node = nodes.func("f", nodes.arguments([]), [nodes.expression_statement(ref)], type=None)
    
    _updated_bindings(func_node, is_definitely_bound={"x": True})
def expression_statement_has_child_names_resolved():
    _assert_children_resolved(
        lambda ref: nodes.expression_statement(ref),
    )
Beispiel #20
0
def function_definitions_in_statement_lists_can_be_defined_out_of_order():
    f = nodes.func("f", type=None, args=nodes.Arguments([]), body=[
        nodes.ret(nodes.call(nodes.ref("g"), []))
    ])
    g = nodes.func("g", type=None, args=nodes.Arguments([]), body=[])
    _update_context([f, g, nodes.expression_statement(nodes.ref("f"))])
Beispiel #21
0
def arguments_of_function_are_definitely_bound():
    arg = nodes.arg("x")
    arg_ref = nodes.ref("x")
    func_node = nodes.func("f", nodes.arguments([arg]), [nodes.expression_statement(arg_ref)], type=None)
    
    _updated_bindings(func_node)
Beispiel #22
0
 def unbound_ref_statement(self):
     return nodes.expression_statement(self.unbound_ref())