1
0
forked from VimPlug/jedi

Split Import, now there is ImportFrom and ImportName as it exists in the python grammar.

This commit is contained in:
Dave Halter
2014-11-19 00:40:16 +01:00
parent 535a69e499
commit 3c6d5dafb1
5 changed files with 46 additions and 27 deletions

View File

@@ -1061,29 +1061,6 @@ class ForFlow(Flow):
class Import(Simple):
def get_defined_names(self):
if self.children[0] == 'import':
n = self.children[1]
if is_node(n, 'dotted_name'):
return [n.children[0]]
else:
return [n]
else: # from
# <Operator: '.'>, <Name: decoder@110,6>, <Keyword: 'import'>, <Name: JSONDecoder@110,21>
return [self.children[-1]]
# TODO remove
if self.defunct:
return []
if self.star:
return [self]
if self.alias:
return [self.alias]
if len(self.namespace_names) > 1:
return [self.namespace_names[0]]
else:
return self.namespace_names
def get_all_import_names(self):
n = []
if self.from_names:
@@ -1135,6 +1112,41 @@ class Import(Simple):
return not self.alias and not self.from_names \
and len(self.namespace_names) > 1
def is_star_import(self):
return self.children[-1] == '*'
class ImportFrom(Import):
def get_defined_names(self):
return [alias or name for name, alias in self._as_name_tuples()]
def _as_name_tuples(self):
last = self.children[-1]
if last == ')':
last = self.children[-2]
elif last == '*':
return # No names defined directly.
if is_node(last, 'import_as_names'):
as_names = last.children[::2]
else:
as_names = [last]
for as_name in as_names:
if isinstance(as_name, Name):
yield as_name, None
else:
yield as_name.children[::2] # yields x, y -> ``x as y``
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]
class KeywordStatement(Simple):
"""