Differentiate in finder between get_value_filters and get_global_filters

This commit is contained in:
Dave Halter
2019-08-15 09:29:08 +02:00
parent 9986d8c9aa
commit 21a18c698e
3 changed files with 26 additions and 25 deletions

View File

@@ -66,7 +66,10 @@ class HelperValueMixin(object):
from jedi.inference import finder
f = finder.NameFinder(self.infer_state, self, name_value, name_or_str,
position, analysis_errors=analysis_errors)
filters = f.get_filters(search_global)
if search_global:
filters = f.get_global_filters()
else:
filters = f.get_value_filters()
if is_goto:
return f.filter_name(filters)
return f.find(filters, attribute_lookup=not search_global)

View File

@@ -92,33 +92,31 @@ class NameFinder(object):
else:
return None
def get_filters(self, search_global=False):
def get_global_filters(self):
origin_scope = self._get_origin_scope()
if search_global:
position = self._position
position = self._position
# For functions and classes the defaults don't belong to the
# function and get inferred in the value before the function. So
# make sure to exclude the function/class name.
if origin_scope is not None:
ancestor = search_ancestor(origin_scope, 'funcdef', 'classdef', 'lambdef')
lambdef = None
if ancestor == 'lambdef':
# For lambdas it's even more complicated since parts will
# be inferred later.
lambdef = ancestor
ancestor = search_ancestor(origin_scope, 'funcdef', 'classdef')
if ancestor is not None:
colon = ancestor.children[-2]
if position is not None and position < colon.start_pos:
if lambdef is None or position < lambdef.children[-2].start_pos:
position = ancestor.start_pos
# For functions and classes the defaults don't belong to the
# function and get inferred in the value before the function. So
# make sure to exclude the function/class name.
if origin_scope is not None:
ancestor = search_ancestor(origin_scope, 'funcdef', 'classdef', 'lambdef')
lambdef = None
if ancestor == 'lambdef':
# For lambdas it's even more complicated since parts will
# be inferred later.
lambdef = ancestor
ancestor = search_ancestor(origin_scope, 'funcdef', 'classdef')
if ancestor is not None:
colon = ancestor.children[-2]
if position is not None and position < colon.start_pos:
if lambdef is None or position < lambdef.children[-2].start_pos:
position = ancestor.start_pos
return get_global_filters(self._infer_state, self._value, position, origin_scope)
else:
return self._get_value_filters(origin_scope)
return get_global_filters(self._infer_state, self._value, position, origin_scope)
def _get_value_filters(self, origin_scope):
def get_value_filters(self):
origin_scope = self._get_origin_scope()
for f in self._value.get_filters(False, self._position, origin_scope=origin_scope):
yield f
# This covers the case where a stub files are incomplete.

View File

@@ -572,7 +572,7 @@ def tree_name_to_values(infer_state, value, tree_name):
if node.type == 'global_stmt':
value = infer_state.create_value(value, tree_name)
finder = NameFinder(infer_state, value, value, tree_name.value)
filters = finder.get_filters(search_global=True)
filters = finder.get_global_filters()
# For global_stmt lookups, we only need the first possible scope,
# which means the function itself.
filters = [next(filters)]