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

@@ -46,6 +46,7 @@ class ImportWrapper():
self._import = name.get_parent_until(pr.Import) self._import = name.get_parent_until(pr.Import)
self.import_path = self._import.path_for_name(name) self.import_path = self._import.path_for_name(name)
self.is_like_search = False # TODO REMOVE
@memoize_default() @memoize_default()
def follow(self, is_goto=False): def follow(self, is_goto=False):

View File

@@ -56,8 +56,8 @@ class Parser(object):
'classdef': pr.Class, 'classdef': pr.Class,
'funcdef': pr.Function, 'funcdef': pr.Function,
'file_input': pr.SubModule, 'file_input': pr.SubModule,
'import_name': pr.Import, 'import_name': pr.ImportName,
'import_from': pr.Import, 'import_from': pr.ImportFrom,
'break_stmt': pr.KeywordStatement, 'break_stmt': pr.KeywordStatement,
'continue_stmt': pr.KeywordStatement, 'continue_stmt': pr.KeywordStatement,
'return_stmt': pr.ReturnStmt, 'return_stmt': pr.ReturnStmt,

View File

@@ -1061,29 +1061,6 @@ class ForFlow(Flow):
class Import(Simple): 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): def get_all_import_names(self):
n = [] n = []
if self.from_names: if self.from_names:
@@ -1135,6 +1112,41 @@ class Import(Simple):
return not self.alias and not self.from_names \ return not self.alias and not self.from_names \
and len(self.namespace_names) > 1 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): class KeywordStatement(Simple):
""" """

View File

@@ -1,2 +1,4 @@
a = 1 a = 1
from import_tree.random import a as c from import_tree.random import a as c
foobarbaz = 3.0

View File

@@ -97,13 +97,16 @@ def scope_from_import_variable():
def scope_from_import_variable_with_parenthesis(): def scope_from_import_variable_with_parenthesis():
from import_tree.mod2.fake import ( from import_tree.mod2.fake import (
a, c a, foobarbaz
) )
#? #?
a a
#? #?
c foobarbaz
# shouldn't complete, should still list the name though.
#? ['foobarbaz']
foobarbaz
# ----------------- # -----------------
# std lib modules # std lib modules
@@ -303,6 +306,7 @@ from import_tree import recurse_class1
#? ['a'] #? ['a']
recurse_class1.C.a recurse_class1.C.a
# github #239 RecursionError # github #239 RecursionError
# TODO UNCOMMENT!!!!!!
##? ['a'] ##? ['a']
recurse_class1.C().a recurse_class1.C().a