Avoid import recursions in other ways

This commit is contained in:
Dave Halter
2019-08-24 01:20:37 +02:00
parent 250ac77f4a
commit 6d361e03ac
4 changed files with 9 additions and 19 deletions

View File

@@ -279,10 +279,10 @@ class Script(object):
def _goto_assignments(self, follow_imports, follow_builtin_imports,
only_stubs=False, prefer_stubs=False):
def filter_follow_imports(names, check):
def filter_follow_imports(names):
for name in names:
if check(name):
new_names = list(filter_follow_imports(name.goto(), check))
if name.is_import():
new_names = list(filter_follow_imports(name.goto()))
found_builtin = False
if follow_builtin_imports:
for new_name in new_names:
@@ -306,7 +306,7 @@ class Script(object):
names = list(self._inference_state.goto(context, tree_name))
if follow_imports:
names = filter_follow_imports(names, lambda name: name.is_import())
names = filter_follow_imports(names)
names = convert_names(
names,
only_stubs=only_stubs,

View File

@@ -88,18 +88,6 @@ class NameFinder(object):
for filter in filters:
names = filter.get(self._string_name)
if names:
if len(names) == 1:
n, = names
if isinstance(n, TreeNameDefinition):
# Something somewhere went terribly wrong. This
# typically happens when using goto on an import in an
# __init__ file. I think we need a better solution, but
# it's kind of hard, because for Jedi it's not clear
# that that name has not been defined, yet.
if n.tree_name == self._name:
def_ = self._name.get_definition()
if def_ is not None and def_.type == 'import_from':
continue
break
debug.dbg('finder.filter_name %s in (%s): %s@%s',

View File

@@ -95,7 +95,8 @@ def goto_import(context, tree_name):
analysis_errors=False
) for c in values
])
if names:
# Avoid recursion on the same names.
if names and not any(n.tree_name is tree_name for n in names):
return names
path = import_path + (from_import_name,)

View File

@@ -219,15 +219,16 @@ def test_goto_assignments_follow_imports(Script):
def test_goto_module(Script):
def check(line, expected):
def check(line, expected, follow_imports=False):
script = Script(path=path, line=line)
module, = script.goto_assignments()
module, = script.goto_assignments(follow_imports=follow_imports)
assert module.module_path == expected
base_path = os.path.join(os.path.dirname(__file__), 'simple_import')
path = os.path.join(base_path, '__init__.py')
check(1, os.path.join(base_path, 'module.py'))
check(1, os.path.join(base_path, 'module.py'), follow_imports=True)
check(5, os.path.join(base_path, 'module2.py'))