def _find_imports(self, node):
"""Recurses through AST collecting the targets of all import
statements."""
if isinstance(node, ast.Import):
return {self._extract_root_module(alias.name) for alias in node.names}
elif isinstance(node, ast.ImportFrom):
# We ignore all imports with levels other than 0. That's because if
# if level > 0, we know that it's a relative import, and we only
# care about root modules.
if node.level == 0:
return {self._extract_root_module(node.module)}
else:
return set()
elif hasattr(node, 'body') and hasattr(node.body, '__iter__'):
# Not all bodies are lists (for ex. exec)
imps = set()
for child_node in node.body:
imps.update(self._find_imports(child_node))
return imps
else:
return set()
评论列表
文章目录