def test_parse_with_statement_with_multiple_context_managers(): expected_node = nodes.with_( nodes.ref("x"), nodes.ref("x2"), [ nodes.with_( nodes.ref("y"), None, [nodes.ret(nodes.ref("z"))], ) ] ) _assert_statement_parse(expected_node, "with x as x2, y:\n return z")
def test_parse_with_statement_single_context_manager_with_target(): expected_node = nodes.with_( nodes.ref("x"), nodes.ref("x2"), [nodes.ret(nodes.ref("y"))], ) _assert_statement_parse(expected_node, "with x as x2:\n return y")
def assigned_variables_in_with_statement_body_are_unbound_after_exit_if_exit_method_does_not_return_none(): context_manager_type = context_manager_class(exit_type=types.bool_type) _assert_name_is_not_definitely_bound(lambda generate: nodes.with_( generate.bound_ref("manager", context_manager_type), None, [generate.assignment()] ), )
def context_manager_of_with_statement_must_have_exit_method(): cls = types.class_type("Manager", [types.attr("__enter__", enter_method())]) context_manager_node = nodes.ref("x") node = nodes.with_(context_manager_node, None, []) try: update_context(node, type_bindings={"x": cls}) assert False, "Expected error" except errors.NoSuchAttributeError as error: assert_equal(nodes.attr(context_manager_node, "__exit__"), error.node)
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))])) )
def children_of_with_statement_are_checked(): context_manager_type = context_manager_class(exit_type=types.none_type) _assert_child_expression_is_checked(lambda generate: nodes.with_( generate.unbound_ref(), None, [] ), ) _assert_child_expression_is_checked(lambda generate: nodes.with_( generate.bound_ref("manager", context_manager_type), nodes.attr(generate.unbound_ref(), "blah"), [] ), ) _assert_child_statement_is_checked(lambda generate: nodes.with_( generate.bound_ref("manager", context_manager_type), None, [generate.unbound_ref_statement()] ), )
def test_transform_with_statement_with_target(self): _assert_transform( nodes.with_(nodes.ref("manager"), nodes.ref("value"), [nodes.ret(nodes.ref("x"))]), """ var __nope_u_exception0 var __nope_u_manager1 = manager var __nope_u_exit2 = __nope_u_manager1.__exit__ var __nope_u_has_exited3 = False value = __nope_u_manager1.__enter__() try: return x except $builtins.Exception as __nope_u_exception0: __nope_u_has_exited3 = True if not $builtins.bool(__nope_u_exit2($builtins.type(__nope_u_exception0), __nope_u_exception0, None)): raise finally: if not __nope_u_has_exited3: __nope_u_exit2(None, None, None) """ )
def with_statement_target_can_be_none(): node = nodes.with_(nodes.ref("manager"), None, []) declarations = find_declarations(node)
def with_statement_target_is_declared(): node = nodes.with_(nodes.ref("manager"), nodes.ref("target"), []) declarations = find_declarations(node) assert_equal("target", declarations.declaration("target").name) assert isinstance(declarations.declaration("target"), name_declaration.VariableDeclarationNode)
def target_can_be_supertype_of_return_type_of_enter_method(): node = nodes.with_(nodes.ref("x"), nodes.ref("y"), []) type_bindings = {"x": context_manager_class(types.int_type), "y": types.any_type} assert_statement_type_checks(node, type_bindings=type_bindings)
def context_manager_of_with_statement_is_type_checked(): assert_expression_is_type_checked(lambda bad_expr: nodes.with_(bad_expr, None, []))
def body_of_with_expression_is_type_checked(): assert_statement_is_type_checked( lambda bad_statement: nodes.with_(nodes.ref("x"), None, [bad_statement]), type_bindings={"x": context_manager_class()}, )