Trying to move towards unifying goto and py__getattribute__

This commit is contained in:
Dave Halter
2019-08-24 00:18:48 +02:00
parent fcec30dff6
commit b13a9f7d5b
4 changed files with 18 additions and 23 deletions

View File

@@ -76,27 +76,24 @@ class HelperValueMixin(object):
"""
if name_context is None:
name_context = self
from jedi.inference import finder
f = finder.NameFinder(self.inference_state, self, name_context, name_or_str,
position, analysis_errors=analysis_errors)
filters = self._get_value_filters(name_or_str)
values = f.find(filters, attribute_lookup=True)
names, f = self._goto(name_or_str, name_context, analysis_errors)
values = f.find(names, attribute_lookup=True)
if not values:
n = name_or_str.value if isinstance(name_or_str, Name) else name_or_str
values = self.py__getattribute__alternatives(n)
return values
def goto(self, name_or_str, name_context=None, analysis_errors=True):
"""
:param position: Position of the last statement -> tuple of line, column
"""
def goto(self, *args, **kwargs):
return self._goto(*args, **kwargs)[0]
def _goto(self, name_or_str, name_context=None, analysis_errors=True):
if name_context is None:
name_context = self
from jedi.inference import finder
f = finder.NameFinder(self.inference_state, self, name_context, name_or_str,
analysis_errors=analysis_errors)
filters = self._get_value_filters(name_or_str)
return f.filter_name(filters)
return f.filter_name(filters), f
def py__await__(self):
await_value_set = self.py__getattribute__(u"__await__")

View File

@@ -20,12 +20,15 @@ class AbstractContext(object):
raise NotImplementedError
def goto(self, name_or_str, position):
return self._goto(name_or_str, position)[0]
def _goto(self, name_or_str, position):
from jedi.inference import finder
f = finder.NameFinder(self.inference_state, self, self, name_or_str, position)
filters = _get_global_filters_for_name(
self, name_or_str if isinstance(name_or_str, Name) else None, position,
)
return f.filter_name(filters)
return f.filter_name(filters), f
def py__getattribute__(self, name_or_str, name_value=None, position=None,
analysis_errors=True):
@@ -34,13 +37,8 @@ class AbstractContext(object):
"""
if name_value is None:
name_value = self
from jedi.inference import finder
f = finder.NameFinder(self.inference_state, self, name_value, name_or_str,
position, analysis_errors=analysis_errors)
filters = _get_global_filters_for_name(
self, name_or_str if isinstance(name_or_str, Name) else None, position,
)
return f.find(filters, attribute_lookup=False)
names, f = self._goto(name_or_str, position)
return f.find(names, attribute_lookup=False)
def get_root_context(self):
parent_context = self.parent_context

View File

@@ -46,12 +46,11 @@ class NameFinder(object):
self._found_predefined_types = None
self._analysis_errors = analysis_errors
def find(self, filters, attribute_lookup):
def find(self, names, attribute_lookup):
"""
:params bool attribute_lookup: Tell to logic if we're accessing the
attribute or the contents of e.g. a function.
"""
names = self.filter_name(filters)
if self._found_predefined_types is not None and names:
check = flow_analysis.reachability_check(
context=self._context,
@@ -63,7 +62,10 @@ class NameFinder(object):
return self._found_predefined_types
types = self._names_to_types(names)
self.check_issues(names, types, attribute_lookup)
return types
def check_issues(self, names, types, attribute_lookup):
if not names and self._analysis_errors and not types \
and not (isinstance(self._name, tree.Name) and
isinstance(self._name.parent.parent, tree.Param)):
@@ -76,8 +78,6 @@ class NameFinder(object):
% self._string_name)
analysis.add(self._name_context, 'name-error', self._name, message)
return types
def filter_name(self, filters):
"""
Searches names that are defined in a scope (the different

View File

@@ -575,7 +575,7 @@ def tree_name_to_values(inference_state, context, tree_name):
# For global_stmt lookups, we only need the first possible scope,
# which means the function itself.
filters = [next(c.get_filters())]
return finder.find(filters, attribute_lookup=False)
return finder.find(finder.filter_name(filters), attribute_lookup=False)
elif node.type not in ('import_from', 'import_name'):
c = context.create_context(tree_name)
return infer_atom(c, tree_name)