1
0
forked from VimPlug/jedi

Move the isinstance checks out of finder

This commit is contained in:
Dave Halter
2019-08-24 03:09:40 +02:00
parent eba088b049
commit c4b0b45a1d
2 changed files with 27 additions and 25 deletions

View File

@@ -7,6 +7,7 @@ from parso.python.tree import Name
from jedi.inference.filters import ParserTreeFilter, MergedFilter, \ from jedi.inference.filters import ParserTreeFilter, MergedFilter, \
GlobalNameFilter GlobalNameFilter
from jedi.inference.base_value import NO_VALUES from jedi.inference.base_value import NO_VALUES
from jedi.parser_utils import get_parent_scope
from jedi import debug from jedi import debug
from jedi import parser_utils from jedi import parser_utils
@@ -80,7 +81,29 @@ class AbstractContext(object):
from jedi.inference import analysis from jedi.inference import analysis
message = ("NameError: name '%s' is not defined." % string_name) message = ("NameError: name '%s' is not defined." % string_name)
analysis.add(name_context, 'name-error', name_or_str, message) analysis.add(name_context, 'name-error', name_or_str, message)
if values:
return 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): def get_root_context(self):
parent_context = self.parent_context parent_context = self.parent_context

View File

@@ -23,7 +23,7 @@ from jedi.inference.arguments import TreeArguments
from jedi.inference import helpers from jedi.inference import helpers
from jedi.inference.value import iterable from jedi.inference.value import iterable
from jedi.inference.base_value import ValueSet, NO_VALUES 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): class NameFinder(object):
@@ -47,9 +47,7 @@ class NameFinder(object):
""" """
values = ValueSet.from_sets(name.infer() for name in names) values = ValueSet.from_sets(name.infer() for name in names)
debug.dbg('finder._names_to_types: %s -> %s', names, values) debug.dbg('finder._names_to_types: %s -> %s', names, values)
if values:
return values return values
return self._add_other_knowledge()
def filter_name(self, filters): def filter_name(self, filters):
""" """
@@ -64,27 +62,8 @@ class NameFinder(object):
return list(names) 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): def check_flow_information(value, flow, search_name, pos):
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):
""" Try to find out the type of a variable just with the information that """ 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.:: is given by the flows: e.g. It is also responsible for assert checks.::