Evaluator -> InferState

This commit is contained in:
Dave Halter
2019-08-15 00:33:49 +02:00
parent 8157d119a7
commit a5dff65142
62 changed files with 768 additions and 768 deletions

View File

@@ -28,7 +28,7 @@ from jedi.api import helpers
from jedi.api.completion import Completion
from jedi.api.environment import InterpreterEnvironment
from jedi.api.project import get_default_project, Project
from jedi.inference import Evaluator
from jedi.inference import InferState
from jedi.inference import imports
from jedi.inference import usages
from jedi.inference.arguments import try_iter_content
@@ -111,11 +111,11 @@ class Script(object):
# TODO deprecate and remove sys_path from the Script API.
if sys_path is not None:
project._sys_path = sys_path
self._evaluator = Evaluator(
self._infer_state = InferState(
project, environment=environment, script_path=self.path
)
debug.speed('init')
self._module_node, source = self._evaluator.parse_and_get_code(
self._module_node, source = self._infer_state.parse_and_get_code(
code=source,
path=self.path,
encoding=encoding,
@@ -156,7 +156,7 @@ class Script(object):
is_package = False
if self.path is not None:
import_names, is_p = transform_path_to_dotted(
self._evaluator.get_sys_path(add_parent_paths=False),
self._infer_state.get_sys_path(add_parent_paths=False),
self.path
)
if import_names is not None:
@@ -170,7 +170,7 @@ class Script(object):
if self.path is not None and self.path.endswith('.pyi'):
# We are in a stub file. Try to load the stub properly.
stub_module = load_proper_stub_module(
self._evaluator,
self._infer_state,
file_io,
names,
self._module_node
@@ -182,21 +182,21 @@ class Script(object):
names = ('__main__',)
module = ModuleContext(
self._evaluator, self._module_node, file_io,
self._infer_state, self._module_node, file_io,
string_names=names,
code_lines=self._code_lines,
is_package=is_package,
)
if names[0] not in ('builtins', '__builtin__', 'typing'):
# These modules are essential for Jedi, so don't overwrite them.
self._evaluator.module_cache.add(names, ContextSet([module]))
self._infer_state.module_cache.add(names, ContextSet([module]))
return module
def __repr__(self):
return '<%s: %s %r>' % (
self.__class__.__name__,
repr(self._orig_path),
self._evaluator.environment,
self._infer_state.environment,
)
def completions(self):
@@ -209,7 +209,7 @@ class Script(object):
"""
with debug.increase_indent_cm('completions'):
completion = Completion(
self._evaluator, self._get_module(), self._code_lines,
self._infer_state, self._get_module(), self._code_lines,
self._pos, self.call_signatures
)
return completion.completions()
@@ -239,16 +239,16 @@ class Script(object):
if leaf is None:
return []
context = self._evaluator.create_context(self._get_module(), leaf)
context = self._infer_state.create_context(self._get_module(), leaf)
contexts = helpers.infer_goto_definition(self._evaluator, context, leaf)
contexts = helpers.infer_goto_definition(self._infer_state, context, leaf)
contexts = convert_contexts(
contexts,
only_stubs=only_stubs,
prefer_stubs=prefer_stubs,
)
defs = [classes.Definition(self._evaluator, c.name) for c in contexts]
defs = [classes.Definition(self._infer_state, c.name) for c in contexts]
# The additional set here allows the definitions to become unique in an
# API sense. In the internals we want to separate more things than in
# the API.
@@ -299,8 +299,8 @@ class Script(object):
# Without a name we really just want to jump to the result e.g.
# executed by `foo()`, if we the cursor is after `)`.
return self.goto_definitions(only_stubs=only_stubs, prefer_stubs=prefer_stubs)
context = self._evaluator.create_context(self._get_module(), tree_name)
names = list(self._evaluator.goto(context, tree_name))
context = self._infer_state.create_context(self._get_module(), tree_name)
names = list(self._infer_state.goto(context, tree_name))
if follow_imports:
names = filter_follow_imports(names, lambda name: name.is_import())
@@ -310,7 +310,7 @@ class Script(object):
prefer_stubs=prefer_stubs,
)
defs = [classes.Definition(self._evaluator, d) for d in set(names)]
defs = [classes.Definition(self._infer_state, d) for d in set(names)]
return helpers.sorted_definitions(defs)
def usages(self, additional_module_paths=(), **kwargs):
@@ -342,7 +342,7 @@ class Script(object):
names = usages.usages(self._get_module(), tree_name)
definitions = [classes.Definition(self._evaluator, n) for n in names]
definitions = [classes.Definition(self._infer_state, n) for n in names]
if not include_builtins:
definitions = [d for d in definitions if not d.in_builtin_module()]
return helpers.sorted_definitions(definitions)
@@ -368,12 +368,12 @@ class Script(object):
if call_details is None:
return []
context = self._evaluator.create_context(
context = self._infer_state.create_context(
self._get_module(),
call_details.bracket_leaf
)
definitions = helpers.cache_call_signatures(
self._evaluator,
self._infer_state,
context,
call_details.bracket_leaf,
self._code_lines,
@@ -383,19 +383,19 @@ class Script(object):
# TODO here we use stubs instead of the actual contexts. We should use
# the signatures from stubs, but the actual contexts, probably?!
return [classes.CallSignature(self._evaluator, signature, call_details)
return [classes.CallSignature(self._infer_state, signature, call_details)
for signature in definitions.get_signatures()]
def _analysis(self):
self._evaluator.is_analysis = True
self._evaluator.analysis_modules = [self._module_node]
self._infer_state.is_analysis = True
self._infer_state.analysis_modules = [self._module_node]
module = self._get_module()
try:
for node in get_executable_nodes(self._module_node):
context = module.create_context(node)
if node.type in ('funcdef', 'classdef'):
# Resolve the decorators.
tree_name_to_contexts(self._evaluator, context, node.children[1])
tree_name_to_contexts(self._infer_state, context, node.children[1])
elif isinstance(node, tree.Import):
import_names = set(node.get_defined_names())
if node.is_nested():
@@ -409,16 +409,16 @@ class Script(object):
unpack_tuple_to_dict(context, types, testlist)
else:
if node.type == 'name':
defs = self._evaluator.goto_definitions(context, node)
defs = self._infer_state.goto_definitions(context, node)
else:
defs = infer_call_of_leaf(context, node)
try_iter_content(defs)
self._evaluator.reset_recursion_limitations()
self._infer_state.reset_recursion_limitations()
ana = [a for a in self._evaluator.analysis if self.path == a.path]
ana = [a for a in self._infer_state.analysis if self.path == a.path]
return sorted(set(ana), key=lambda x: x.line)
finally:
self._evaluator.is_analysis = False
self._infer_state.is_analysis = False
class Interpreter(Script):
@@ -467,11 +467,11 @@ class Interpreter(Script):
super(Interpreter, self).__init__(source, environment=environment,
_project=Project(os.getcwd()), **kwds)
self.namespaces = namespaces
self._evaluator.allow_descriptor_getattr = self._allow_descriptor_getattr_default
self._infer_state.allow_descriptor_getattr = self._allow_descriptor_getattr_default
def _get_module(self):
return interpreter.MixedModuleContext(
self._evaluator,
self._infer_state,
self._module_node,
self.namespaces,
file_io=KnownContentFileIO(self.path, self._code),
@@ -514,7 +514,7 @@ def names(source=None, path=None, encoding='utf-8', all_scopes=False,
module_context = script._get_module()
defs = [
classes.Definition(
script._evaluator,
script._infer_state,
create_name(name)
) for name in get_module_names(script._module_node, all_scopes)
]

View File

@@ -25,7 +25,7 @@ def _sort_names_by_start_pos(names):
return sorted(names, key=lambda s: s.start_pos or (0, 0))
def defined_names(evaluator, context):
def defined_names(infer_state, context):
"""
List sub-definitions (e.g., methods in class).
@@ -34,11 +34,11 @@ def defined_names(evaluator, context):
"""
filter = next(context.get_filters(search_global=True))
names = [name for name in filter.values()]
return [Definition(evaluator, n) for n in _sort_names_by_start_pos(names)]
return [Definition(infer_state, n) for n in _sort_names_by_start_pos(names)]
def _contexts_to_definitions(contexts):
return [Definition(c.evaluator, c.name) for c in contexts]
return [Definition(c.infer_state, c.name) for c in contexts]
class BaseDefinition(object):
@@ -62,8 +62,8 @@ class BaseDefinition(object):
'argparse._ActionsContainer': 'argparse.ArgumentParser',
}.items())
def __init__(self, evaluator, name):
self._evaluator = evaluator
def __init__(self, infer_state, name):
self._infer_state = infer_state
self._name = name
"""
An instance of :class:`parso.python.tree.Name` subclass.
@@ -306,7 +306,7 @@ class BaseDefinition(object):
only_stubs=only_stubs,
prefer_stubs=prefer_stubs,
)
return [self if n == self._name else Definition(self._evaluator, n)
return [self if n == self._name else Definition(self._infer_state, n)
for n in names]
def infer(self, **kwargs): # Python 2...
@@ -329,7 +329,7 @@ class BaseDefinition(object):
prefer_stubs=prefer_stubs,
)
resulting_names = [c.name for c in contexts]
return [self if n == self._name else Definition(self._evaluator, n)
return [self if n == self._name else Definition(self._infer_state, n)
for n in resulting_names]
@property
@@ -346,7 +346,7 @@ class BaseDefinition(object):
for context in self._name.infer():
for signature in context.get_signatures():
return [
Definition(self._evaluator, n)
Definition(self._infer_state, n)
for n in signature.get_param_names(resolve_stars=True)
]
@@ -366,7 +366,7 @@ class BaseDefinition(object):
if isinstance(context, FunctionExecutionContext):
context = context.function_context
return Definition(self._evaluator, context.name)
return Definition(self._infer_state, context.name)
def __repr__(self):
return "<%s %sname=%r, description=%r>" % (
@@ -396,7 +396,7 @@ class BaseDefinition(object):
return ''.join(lines[start_index:index + after + 1])
def get_signatures(self):
return [Signature(self._evaluator, s) for s in self._name.infer().get_signatures()]
return [Signature(self._infer_state, s) for s in self._name.infer().get_signatures()]
def execute(self):
return _contexts_to_definitions(self._name.infer().execute_with_values())
@@ -407,8 +407,8 @@ class Completion(BaseDefinition):
`Completion` objects are returned from :meth:`api.Script.completions`. They
provide additional information about a completion.
"""
def __init__(self, evaluator, name, stack, like_name_length):
super(Completion, self).__init__(evaluator, name)
def __init__(self, infer_state, name, stack, like_name_length):
super(Completion, self).__init__(infer_state, name)
self._like_name_length = like_name_length
self._stack = stack
@@ -512,8 +512,8 @@ class Definition(BaseDefinition):
*Definition* objects are returned from :meth:`api.Script.goto_assignments`
or :meth:`api.Script.goto_definitions`.
"""
def __init__(self, evaluator, definition):
super(Definition, self).__init__(evaluator, definition)
def __init__(self, infer_state, definition):
super(Definition, self).__init__(infer_state, definition)
@property
def description(self):
@@ -588,7 +588,7 @@ class Definition(BaseDefinition):
"""
defs = self._name.infer()
return sorted(
unite(defined_names(self._evaluator, d) for d in defs),
unite(defined_names(self._infer_state, d) for d in defs),
key=lambda s: s._name.start_pos or (0, 0)
)
@@ -606,13 +606,13 @@ class Definition(BaseDefinition):
return self._name.start_pos == other._name.start_pos \
and self.module_path == other.module_path \
and self.name == other.name \
and self._evaluator == other._evaluator
and self._infer_state == other._infer_state
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return hash((self._name.start_pos, self.module_path, self.name, self._evaluator))
return hash((self._name.start_pos, self.module_path, self.name, self._infer_state))
class Signature(Definition):
@@ -621,8 +621,8 @@ class Signature(Definition):
It knows what functions you are currently in. e.g. `isinstance(` would
return the `isinstance` function. without `(` it would return nothing.
"""
def __init__(self, evaluator, signature):
super(Signature, self).__init__(evaluator, signature.name)
def __init__(self, infer_state, signature):
super(Signature, self).__init__(infer_state, signature.name)
self._signature = signature
@property
@@ -630,7 +630,7 @@ class Signature(Definition):
"""
:return list of ParamDefinition:
"""
return [ParamDefinition(self._evaluator, n)
return [ParamDefinition(self._infer_state, n)
for n in self._signature.get_param_names(resolve_stars=True)]
def to_string(self):
@@ -644,8 +644,8 @@ class CallSignature(Signature):
return the `isinstance` function with its params. Without `(` it would
return nothing.
"""
def __init__(self, evaluator, signature, call_details):
super(CallSignature, self).__init__(evaluator, signature)
def __init__(self, infer_state, signature, call_details):
super(CallSignature, self).__init__(infer_state, signature)
self._call_details = call_details
self._signature = signature

View File

@@ -28,7 +28,7 @@ def get_call_signature_param_names(call_signatures):
yield p._name
def filter_names(evaluator, completion_names, stack, like_name):
def filter_names(infer_state, completion_names, stack, like_name):
comp_dct = {}
if settings.case_insensitive_completion:
like_name = like_name.lower()
@@ -39,7 +39,7 @@ def filter_names(evaluator, completion_names, stack, like_name):
if string.startswith(like_name):
new = classes.Completion(
evaluator,
infer_state,
name,
stack,
len(like_name)
@@ -85,8 +85,8 @@ def get_flow_scope_node(module_node, position):
class Completion:
def __init__(self, evaluator, module, code_lines, position, call_signatures_callback):
self._evaluator = evaluator
def __init__(self, infer_state, module, code_lines, position, call_signatures_callback):
self._infer_state = infer_state
self._module_context = module
self._module_node = module.tree_node
self._code_lines = code_lines
@@ -104,7 +104,7 @@ class Completion:
string, start_leaf = _extract_string_while_in_string(leaf, self._position)
if string is not None:
completions = list(file_name_completions(
self._evaluator, self._module_context, start_leaf, string,
self._infer_state, self._module_context, start_leaf, string,
self._like_name, self._call_signatures_callback,
self._code_lines, self._original_position
))
@@ -113,7 +113,7 @@ class Completion:
completion_names = self._get_context_completions(leaf)
completions = filter_names(self._evaluator, completion_names,
completions = filter_names(self._infer_state, completion_names,
self.stack, self._like_name)
return sorted(completions, key=lambda x: (x.name.startswith('__'),
@@ -135,7 +135,7 @@ class Completion:
- In params (also lambda): no completion before =
"""
grammar = self._evaluator.grammar
grammar = self._infer_state.grammar
self.stack = stack = None
try:
@@ -234,14 +234,14 @@ class Completion:
def _get_keyword_completion_names(self, allowed_transitions):
for k in allowed_transitions:
if isinstance(k, str) and k.isalpha():
yield keywords.KeywordName(self._evaluator, k)
yield keywords.KeywordName(self._infer_state, k)
def _global_completions(self):
context = get_user_scope(self._module_context, self._position)
debug.dbg('global completion scope: %s', context)
flow_scope_node = get_flow_scope_node(self._module_node, self._position)
filters = get_global_filters(
self._evaluator,
self._infer_state,
context,
self._position,
origin_scope=flow_scope_node
@@ -253,7 +253,7 @@ class Completion:
def _trailer_completions(self, previous_leaf):
user_context = get_user_scope(self._module_context, self._position)
inferred_context = self._evaluator.create_context(
inferred_context = self._infer_state.create_context(
self._module_context, previous_leaf
)
contexts = infer_call_of_leaf(inferred_context, previous_leaf)
@@ -276,8 +276,8 @@ class Completion:
def _get_importer_names(self, names, level=0, only_modules=True):
names = [n.value for n in names]
i = imports.Importer(self._evaluator, names, self._module_context, level)
return i.completion_names(self._evaluator, only_modules=only_modules)
i = imports.Importer(self._infer_state, names, self._module_context, level)
return i.completion_names(self._infer_state, only_modules=only_modules)
def _get_class_context_completions(self, is_function=True):
"""

View File

@@ -11,7 +11,7 @@ from collections import namedtuple
from jedi._compatibility import highest_pickle_protocol, which
from jedi.cache import memoize_method, time_cache
from jedi.inference.compiled.subprocess import CompiledSubprocess, \
EvaluatorSameProcess, EvaluatorSubprocess
InferStateSameProcess, InferStateSubprocess
import parso
@@ -109,8 +109,8 @@ class Environment(_BaseEnvironment):
version = '.'.join(str(i) for i in self.version_info)
return '<%s: %s in %s>' % (self.__class__.__name__, version, self.path)
def get_evaluator_subprocess(self, evaluator):
return EvaluatorSubprocess(evaluator, self._get_subprocess())
def get_infer_state_subprocess(self, infer_state):
return InferStateSubprocess(infer_state, self._get_subprocess())
@memoize_method
def get_sys_path(self):
@@ -140,8 +140,8 @@ class SameEnvironment(_SameEnvironmentMixin, Environment):
class InterpreterEnvironment(_SameEnvironmentMixin, _BaseEnvironment):
def get_evaluator_subprocess(self, evaluator):
return EvaluatorSameProcess(evaluator)
def get_infer_state_subprocess(self, infer_state):
return InferStateSameProcess(infer_state)
def get_sys_path(self):
return sys.path

View File

@@ -7,7 +7,7 @@ from jedi.inference.helpers import get_str_or_none
from jedi.parser_utils import get_string_quote
def file_name_completions(evaluator, module_context, start_leaf, string,
def file_name_completions(infer_state, module_context, start_leaf, string,
like_name, call_signatures_callback, code_lines, position):
# First we want to find out what can actually be changed as a name.
like_name_length = len(os.path.basename(string) + like_name)
@@ -30,7 +30,7 @@ def file_name_completions(evaluator, module_context, start_leaf, string,
is_in_os_path_join = False
else:
string = to_be_added + string
base_path = os.path.join(evaluator.project._path, string)
base_path = os.path.join(infer_state.project._path, string)
try:
listed = os.listdir(base_path)
except FileNotFoundError:
@@ -53,8 +53,8 @@ def file_name_completions(evaluator, module_context, start_leaf, string,
name += os.path.sep
yield classes.Completion(
evaluator,
FileName(evaluator, name[len(must_start_with) - like_name_length:]),
infer_state,
FileName(infer_state, name[len(must_start_with) - like_name_length:]),
stack=None,
like_name_length=like_name_length
)

View File

@@ -136,11 +136,11 @@ def get_stack_at_position(grammar, code_lines, leaf, pos):
)
def infer_goto_definition(evaluator, context, leaf):
def infer_goto_definition(infer_state, context, leaf):
if leaf.type == 'name':
# In case of a name we can just use goto_definition which does all the
# magic itself.
return evaluator.goto_definitions(context, leaf)
return infer_state.goto_definitions(context, leaf)
parent = leaf.parent
definitions = NO_CONTEXTS
@@ -154,7 +154,7 @@ def infer_goto_definition(evaluator, context, leaf):
# e.g. `"foo"` or `1.0`
return infer_atom(context, leaf)
elif leaf.type in ('fstring_string', 'fstring_start', 'fstring_end'):
return get_string_context_set(evaluator)
return get_string_context_set(infer_state)
return definitions
@@ -376,7 +376,7 @@ def get_call_signature_details(module, position):
@call_signature_time_cache("call_signatures_validity")
def cache_call_signatures(evaluator, context, bracket_leaf, code_lines, user_pos):
def cache_call_signatures(infer_state, context, bracket_leaf, code_lines, user_pos):
"""This function calculates the cache key."""
line_index = user_pos[0] - 1
@@ -391,7 +391,7 @@ def cache_call_signatures(evaluator, context, bracket_leaf, code_lines, user_pos
else:
yield (module_path, before_bracket, bracket_leaf.start_pos)
yield infer_goto_definition(
evaluator,
infer_state,
context,
bracket_leaf.get_previous_leaf(),
)

View File

@@ -9,9 +9,9 @@ from jedi.inference.compiled.access import create_access_path
from jedi.inference.base_context import ContextWrapper
def _create(evaluator, obj):
def _create(infer_state, obj):
return compiled.create_from_access_path(
evaluator, create_access_path(evaluator, obj)
infer_state, create_access_path(infer_state, obj)
)
@@ -23,9 +23,9 @@ class NamespaceObject(object):
class MixedModuleContext(ContextWrapper):
type = 'mixed_module'
def __init__(self, evaluator, tree_module, namespaces, file_io, code_lines):
def __init__(self, infer_state, tree_module, namespaces, file_io, code_lines):
module_context = ModuleContext(
evaluator, tree_module,
infer_state, tree_module,
file_io=file_io,
string_names=('__main__',),
code_lines=code_lines
@@ -38,7 +38,7 @@ class MixedModuleContext(ContextWrapper):
yield filter
for namespace_obj in self._namespace_objects:
compiled_object = _create(self.evaluator, namespace_obj)
compiled_object = _create(self.infer_state, namespace_obj)
mixed_object = mixed.MixedObject(
compiled_object=compiled_object,
tree_context=self._wrapped_context

View File

@@ -15,24 +15,24 @@ except ImportError:
pydoc_topics = None
def get_operator(evaluator, string, pos):
return Keyword(evaluator, string, pos)
def get_operator(infer_state, string, pos):
return Keyword(infer_state, string, pos)
class KeywordName(AbstractArbitraryName):
api_type = u'keyword'
def infer(self):
return [Keyword(self.evaluator, self.string_name, (0, 0))]
return [Keyword(self.infer_state, self.string_name, (0, 0))]
class Keyword(object):
api_type = u'keyword'
def __init__(self, evaluator, name, pos):
self.name = KeywordName(evaluator, name)
def __init__(self, infer_state, name, pos):
self.name = KeywordName(infer_state, name)
self.start_pos = pos
self.parent = evaluator.builtins_module
self.parent = infer_state.builtins_module
@property
def names(self):

View File

@@ -7,7 +7,7 @@ from jedi.api.environment import SameEnvironment, \
from jedi.api.exceptions import WrongVersion
from jedi._compatibility import force_unicode
from jedi.inference.sys_path import discover_buildout_paths
from jedi.inference.cache import evaluator_as_method_param_cache
from jedi.inference.cache import infer_state_as_method_param_cache
from jedi.common.utils import traverse_parents
_CONFIG_FOLDER = '.jedi'
@@ -77,8 +77,8 @@ class Project(object):
py2_comp(path, **kwargs)
@evaluator_as_method_param_cache()
def _get_base_sys_path(self, evaluator, environment=None):
@infer_state_as_method_param_cache()
def _get_base_sys_path(self, infer_state, environment=None):
if self._sys_path is not None:
return self._sys_path
@@ -93,8 +93,8 @@ class Project(object):
pass
return sys_path
@evaluator_as_method_param_cache()
def _get_sys_path(self, evaluator, environment=None, add_parent_paths=True):
@infer_state_as_method_param_cache()
def _get_sys_path(self, infer_state, environment=None, add_parent_paths=True):
"""
Keep this method private for all users of jedi. However internally this
one is used like a public method.
@@ -102,15 +102,15 @@ class Project(object):
suffixed = []
prefixed = []
sys_path = list(self._get_base_sys_path(evaluator, environment))
sys_path = list(self._get_base_sys_path(infer_state, environment))
if self._smart_sys_path:
prefixed.append(self._path)
if evaluator.script_path is not None:
suffixed += discover_buildout_paths(evaluator, evaluator.script_path)
if infer_state.script_path is not None:
suffixed += discover_buildout_paths(infer_state, infer_state.script_path)
if add_parent_paths:
traversed = list(traverse_parents(evaluator.script_path))
traversed = list(traverse_parents(infer_state.script_path))
# AFAIK some libraries have imports like `foo.foo.bar`, which
# leads to the conclusion to by default prefer longer paths