Care for import aliases better.

This commit is contained in:
Dave Halter
2014-11-19 12:45:39 +01:00
parent 1c240e75d3
commit e630eeb397
3 changed files with 29 additions and 2 deletions

View File

@@ -89,6 +89,7 @@ class ImportWrapper():
# ``os.path``, because it's a very important one in Python
# that is being achieved by messing with ``sys.modules`` in
# ``os``.
raise NotImplementedError
scopes = self._evaluator.follow_path(iter(rest), [module], module)
elif rest:
if is_goto:
@@ -97,7 +98,7 @@ class ImportWrapper():
for s in scopes))
else:
scopes = list(chain.from_iterable(
self._evaluator.follow_path(iter(rest), [s], s)
self._evaluator.find_types(s, rest[0])
for s in scopes))
debug.dbg('after import: %s', scopes)
if not scopes:
@@ -431,6 +432,8 @@ class _Importer(object):
scope, rest = self.follow_file_system()
if rest:
# follow the rest of the import (not FS -> classes, functions)
raise NotImplementedError
# old
return evaluator.follow_path(iter(rest), [scope], scope)
return [scope]

View File

@@ -1077,6 +1077,12 @@ class Import(Simple):
raise NotImplementedError
def path_for_name(self, name):
try:
# The name may be an alias. If it is, just map it back to the name.
name = self.aliases()[name]
except KeyError:
pass
for path in self._paths():
if name in path:
return path[:path.index(name) + 1]
@@ -1108,6 +1114,11 @@ class ImportFrom(Import):
def get_defined_names(self):
return [alias or name for name, alias in self._as_name_tuples()]
def aliases(self):
"""Mapping from alias to its corresponding name."""
return dict((alias, name) for name, alias in self._as_name_tuples()
if alias is not None)
def _as_name_tuples(self):
last = self.children[-1]
if last == ')':
@@ -1152,6 +1163,10 @@ class ImportName(Import):
else:
return [self.children[1:]]
def aliases(self):
raise NotImplementedError
return []
class KeywordStatement(Simple):
"""

View File

@@ -108,6 +108,15 @@ def scope_from_import_variable_with_parenthesis():
#? ['foobarbaz']
foobarbaz
def as_imports():
from import_tree.mod1 import a as xyz
#? int()
xyz
import import_tree.mod1 as foo
#? int()
foo.a
# -----------------
# std lib modules
# -----------------