def IMPORT_STAR(self, instr): import_ = self.ast_stack.pop() names = import_.names alias = _ast.alias(name='*', asname=None) from_ = _ast.ImportFrom(module=names[0].name, names=[alias], level=0, lineno=instr.lineno, col_offset=0) self.ast_stack.append(from_)
def IMPORT_FROM(self, instr): import_ = self.ast_stack.pop() names = [_ast.alias(instr.arg, None)] modname = import_.names[0].name from_ = _ast.ImportFrom(module=modname, names=names, level=0, lineno=instr.lineno, col_offset=0) self.ast_stack.append(from_) self.ast_stack.append(import_)
def Import(import_part='', from_part='', asname=None): """Creates either an _ast.Import node or an _ast.ImportFrom node. Args: import_part: The text that follows "import". from_part: The text that follows "from". Optional. Determines if we will return an _ast.Import or _ast.ImportFrom node. asname: Text that follows "as". Optional. Returns: An _ast.Import or _ast.ImportFrom node. """ names = [_ast.alias(name=import_part, asname=asname)] if from_part: return _ast.ImportFrom(level=0, module=from_part, names=names) else: return _ast.Import(names=names)
def import_from_stmt(module: str, names: List[_ast.alias], level: int) -> _ast.ImportFrom: return _ast.ImportFrom(module=module, names=names, level=level)
def test_int(self): ast = self.ast imp = ast.ImportFrom("", ["apples"], -1) assert imp.level == -1 imp.level = 3 assert imp.level == 3
def import_from(self, items): (import_names, _, __) = items return _ast.ImportFrom(module=_.value.replace('/', '.'), names=import_names, level=0) # TODO: determine the level properly