'as' test and implementation for ImportName. Working pretty well.

This commit is contained in:
Dave Halter
2014-11-19 13:07:08 +01:00
parent e630eeb397
commit 6c5f3419ff
2 changed files with 27 additions and 12 deletions

View File

@@ -1150,22 +1150,34 @@ class ImportFrom(Import):
class ImportName(Import):
"""For ``import_name`` nodes. Covers normal imports without ``from``."""
def get_defined_names(self):
n = self.children[1]
if is_node(n, 'dotted_name'):
return [n.children[0]]
else:
return [n]
return [alias or path[0] for path, alias in self._dotted_as_names()]
def _paths(self):
n = self.children[1]
if is_node(n, 'dotted_name'):
return [n.children[::2]]
return [path for path, alias in self._dotted_as_names()]
def _dotted_as_names(self):
"""Generator of (list(path), alias) where alias may be None."""
dotted_as_names = self.children[1]
if is_node(dotted_as_names, 'dotted_as_names'):
as_names = dotted_as_names.children[::2]
else:
return [self.children[1:]]
as_names = [dotted_as_names]
for as_name in as_names:
if is_node(as_name, 'dotted_as_name'):
alias = as_name.children[2]
as_name = as_name.children[0]
else:
alias = None
if isinstance(as_name, Name):
yield [as_name], alias
else:
# dotted_names
yield as_name.children[::2], alias
def aliases(self):
raise NotImplementedError
return []
return dict((alias, path[-1]) for path, alias in self._dotted_as_names()
if alias is not None)
class KeywordStatement(Simple):

View File

@@ -113,9 +113,12 @@ def as_imports():
from import_tree.mod1 import a as xyz
#? int()
xyz
import import_tree.mod1 as foo
import not_existant, import_tree.mod1 as foo
#? int()
foo.a
import import_tree.mod1 as bar
#? int()
bar.a
# -----------------
# std lib modules