From c4b0b45a1d02c2e70d544e659cc07d33e3724733 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sat, 24 Aug 2019 03:09:40 +0200 Subject: [PATCH] Move the isinstance checks out of finder --- jedi/inference/context.py | 25 ++++++++++++++++++++++++- jedi/inference/finder.py | 27 +++------------------------ 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/jedi/inference/context.py b/jedi/inference/context.py index b89172f6..723daec8 100644 --- a/jedi/inference/context.py +++ b/jedi/inference/context.py @@ -7,6 +7,7 @@ from parso.python.tree import Name from jedi.inference.filters import ParserTreeFilter, MergedFilter, \ GlobalNameFilter from jedi.inference.base_value import NO_VALUES +from jedi.parser_utils import get_parent_scope from jedi import debug from jedi import parser_utils @@ -80,7 +81,29 @@ class AbstractContext(object): from jedi.inference import analysis message = ("NameError: name '%s' is not defined." % string_name) analysis.add(name_context, 'name-error', name_or_str, message) - return values + if values: + return values + return self._check_for_additional_knowledge(name_or_str, name_context, position) + + def _check_for_additional_knowledge(self, name_or_str, name_context, position): + name_context = name_context or self + # Add isinstance and other if/assert knowledge. + if isinstance(name_or_str, Name) and not name_context.is_instance(): + flow_scope = name_or_str + base_nodes = [name_context.tree_node] + + if any(b.type in ('comp_for', 'sync_comp_for') for b in base_nodes): + return NO_VALUES + from jedi.inference.finder import check_flow_information + while True: + flow_scope = get_parent_scope(flow_scope, include_flows=True) + n = check_flow_information(name_context, flow_scope, + name_or_str, position) + if n is not None: + return n + if flow_scope in base_nodes: + break + return NO_VALUES def get_root_context(self): parent_context = self.parent_context diff --git a/jedi/inference/finder.py b/jedi/inference/finder.py index 460e5dec..969ae02d 100644 --- a/jedi/inference/finder.py +++ b/jedi/inference/finder.py @@ -23,7 +23,7 @@ from jedi.inference.arguments import TreeArguments from jedi.inference import helpers from jedi.inference.value import iterable from jedi.inference.base_value import ValueSet, NO_VALUES -from jedi.parser_utils import is_scope, get_parent_scope +from jedi.parser_utils import is_scope class NameFinder(object): @@ -47,9 +47,7 @@ class NameFinder(object): """ values = ValueSet.from_sets(name.infer() for name in names) debug.dbg('finder._names_to_types: %s -> %s', names, values) - if values: - return values - return self._add_other_knowledge() + return values def filter_name(self, filters): """ @@ -64,27 +62,8 @@ class NameFinder(object): return list(names) - def _add_other_knowledge(self): - # Add isinstance and other if/assert knowledge. - if isinstance(self._name, tree.Name) and \ - not self._name_context.is_instance() and not self._context.is_compiled(): - flow_scope = self._name - base_nodes = [self._name_context.tree_node] - if any(b.type in ('comp_for', 'sync_comp_for') for b in base_nodes): - return NO_VALUES - while True: - flow_scope = get_parent_scope(flow_scope, include_flows=True) - n = _check_flow_information(self._name_context, flow_scope, - self._name, self._position) - if n is not None: - return n - if flow_scope in base_nodes: - break - return NO_VALUES - - -def _check_flow_information(value, flow, search_name, pos): +def check_flow_information(value, flow, search_name, pos): """ Try to find out the type of a variable just with the information that is given by the flows: e.g. It is also responsible for assert checks.::