def import_from_alias_name_is_declared(): alias_node = nodes.import_alias("x", "y") node = nodes.import_from(["."], [alias_node]) declarations = find_declarations(node) assert_equal("y", declarations.declaration("y").name) assert isinstance(declarations.declaration("y"), name_declaration.ImportDeclarationNode) assert not declarations.is_declared("x")
def can_parse_import_alias(): source = """ from . import message as m """ module_node = parser.parse(source) expected_node = nodes.import_from(["."], [nodes.import_alias("message", "m")]) assert_equal(expected_node, module_node.body[0])
def can_parse_import_from_in_current_package_and_one_name(): source = """ from . import message """ module_node = parser.parse(source) expected_node = nodes.import_from(["."], [nodes.import_alias("message", None)]) assert_equal(expected_node, module_node.body[0])
def value_in_package_can_have_same_name_as_module_if_it_is_that_module(): value_node = nodes.import_from(["."], [nodes.import_alias("x", None)]) node = nodes.module([value_node], is_executable=False) _check_module(LocalModule("root/__init__.py", node), { (".",): module({}), (".", "x"): module({}), })
def can_import_module_using_import_from_syntax(): node = nodes.import_from(["."], [nodes.import_alias("message", None)]) message_module = module_type("message", [types.attr("value", types.str_type)]) context = _update_blank_context(node, { (".", "message"): message_module, }) assert_equal(types.str_type, context.lookup_name("message").attrs.type_of("value"))
def can_import_value_from_relative_module_using_import_from_syntax(): node = nodes.import_from([".", "message"], [nodes.import_alias("value", None)]) context = _update_blank_context(node, { (".", "message"): module_type("message", [types.attr("value", types.str_type)]) }) assert_equal(types.str_type, context.lookup_name("value")) assert_raises(errors.UnboundLocalError, lambda: context.lookup_name("message"))
def test_importing_module_from_package_references_module_directly(self): module_resolver = FakeModuleResolver({ (("x", ), "y"): ResolvedImport(["x", "y"], _stub_module, None) }) _assert_transform( nodes.import_from(["x"], [nodes.import_alias("y", None)]), cc.assign(cc.ref("y"), cc.module_ref(["x", "y"])), module_resolver=module_resolver, )
def can_parse_import_from_with_relative_import_of_child_module(): source = """ from .x.y import message """ module_node = parser.parse(source) expected_node = nodes.import_from([".", "x", "y"], [ nodes.import_alias("message", None), ]) assert_equal(expected_node, module_node.body[0])
def can_parse_absolute_import_from(): source = """ from x.y import message """ module_node = parser.parse(source) expected_node = nodes.import_from(["x", "y"], [ nodes.import_alias("message", None), ]) assert_equal(expected_node, module_node.body[0])
def test_multiple_imported_names_in_one_statement_generates_multiple_assignments(self): _assert_transform( nodes.import_from(["."], [ nodes.import_alias("x", None), nodes.import_alias("y", None), ]), cc.statements([ cc.assign(cc.ref("x"), cc.attr(cc.module_ref(["."]), "x")), cc.assign(cc.ref("y"), cc.attr(cc.module_ref(["."]), "y")), ]), )
def import_from_aliases_are_resolved(): declarations = _create_declarations(["x"]) alias_node = nodes.import_alias("x", None) node = nodes.import_from(["."], [alias_node]) references = resolve(node, declarations) assert_equal(declarations.declaration("x"), references.referenced_declaration(alias_node))
def import_name_is_definitely_bound_after_import_from_statement(): alias_node = nodes.import_alias("x.y", None) node = nodes.import_from(["."], [alias_node]) bindings = _updated_bindings(node) assert_equal(True, bindings.is_definitely_bound(alias_node))
def test_import_from_assigns_value_to_asname_if_asname_is_set(self): _assert_transform( nodes.import_from(["os", "path"], [nodes.import_alias("join", "j")]), cc.assign(cc.ref("j"), cc.attr(cc.module_ref(["os", "path"]), "join")), )
def test_import_from_uses_two_dots_to_indicate_import_from_parent_package(self): _assert_transform( nodes.import_from([".."], [nodes.import_alias("x", None)]), cc.assign(cc.ref("x"), cc.attr(cc.module_ref([".."]), "x")), )