From b13a9f7d5b342053fbc7d28705f264c598fa8808 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sat, 24 Aug 2019 00:18:48 +0200 Subject: [PATCH] Trying to move towards unifying goto and py__getattribute__ --- jedi/inference/base_value.py | 17 +++++++---------- jedi/inference/context.py | 14 ++++++-------- jedi/inference/finder.py | 8 ++++---- jedi/inference/syntax_tree.py | 2 +- 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/jedi/inference/base_value.py b/jedi/inference/base_value.py index 4128cffc..8384f95e 100644 --- a/jedi/inference/base_value.py +++ b/jedi/inference/base_value.py @@ -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__") diff --git a/jedi/inference/context.py b/jedi/inference/context.py index 23761ada..fd66b6bf 100644 --- a/jedi/inference/context.py +++ b/jedi/inference/context.py @@ -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 diff --git a/jedi/inference/finder.py b/jedi/inference/finder.py index e8f05e4b..d46047ea 100644 --- a/jedi/inference/finder.py +++ b/jedi/inference/finder.py @@ -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 diff --git a/jedi/inference/syntax_tree.py b/jedi/inference/syntax_tree.py index 979bf1cb..baf5e1eb 100644 --- a/jedi/inference/syntax_tree.py +++ b/jedi/inference/syntax_tree.py @@ -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)