1
0
forked from VimPlug/jedi

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
+12 -12
View File
@@ -10,7 +10,7 @@ import re
from parso import ParserSyntaxError, parse
from jedi._compatibility import force_unicode
from jedi.inference.cache import evaluator_method_cache
from jedi.inference.cache import infer_state_method_cache
from jedi.inference.base_context import ContextSet, NO_CONTEXTS
from jedi.inference.gradual.typing import TypeVar, LazyGenericClass, \
AbstractAnnotatedClass
@@ -32,13 +32,13 @@ def infer_annotation(context, annotation):
"""
context_set = context.infer_node(annotation)
if len(context_set) != 1:
debug.warning("Eval'ed typing index %s should lead to 1 object, "
debug.warning("Inferred typing index %s should lead to 1 object, "
" not %s" % (annotation, context_set))
return context_set
evaled_context = list(context_set)[0]
if is_string(evaled_context):
result = _get_forward_reference_node(context, evaled_context.get_safe_value())
inferred_context = list(context_set)[0]
if is_string(inferred_context):
result = _get_forward_reference_node(context, inferred_context.get_safe_value())
if result is not None:
return context.infer_node(result)
return context_set
@@ -60,7 +60,7 @@ def _infer_annotation_string(context, string, index=None):
def _get_forward_reference_node(context, string):
try:
new_node = context.evaluator.grammar.parse(
new_node = context.infer_state.grammar.parse(
force_unicode(string),
start_symbol='eval_input',
error_recovery=False
@@ -106,21 +106,21 @@ def _split_comment_param_declaration(decl_text):
return params
@evaluator_method_cache()
@infer_state_method_cache()
def infer_param(execution_context, param):
contexts = _infer_param(execution_context, param)
evaluator = execution_context.evaluator
infer_state = execution_context.infer_state
if param.star_count == 1:
tuple_ = builtin_from_name(evaluator, 'tuple')
tuple_ = builtin_from_name(infer_state, 'tuple')
return ContextSet([GenericClass(
tuple_,
generics=(contexts,),
) for c in contexts])
elif param.star_count == 2:
dct = builtin_from_name(evaluator, 'dict')
dct = builtin_from_name(infer_state, 'dict')
return ContextSet([GenericClass(
dct,
generics=(ContextSet([builtin_from_name(evaluator, 'str')]), contexts),
generics=(ContextSet([builtin_from_name(infer_state, 'str')]), contexts),
) for c in contexts])
pass
return contexts
@@ -190,7 +190,7 @@ def py__annotations__(funcdef):
return dct
@evaluator_method_cache()
@infer_state_method_cache()
def infer_return_types(function_execution_context):
"""
Infers the type of a function's return value,
+2 -2
View File
@@ -87,11 +87,11 @@ def _load_stub_module(module):
return module
from jedi.inference.gradual.typeshed import _try_to_load_stub_cached
return _try_to_load_stub_cached(
module.evaluator,
module.infer_state,
import_names=module.string_names,
python_context_set=ContextSet([module]),
parent_module_context=None,
sys_path=module.evaluator.get_sys_path(),
sys_path=module.infer_state.get_sys_path(),
)
+1 -1
View File
@@ -36,7 +36,7 @@ class StubModuleContext(ModuleContext):
def _get_stub_filters(self, search_global, **filter_kwargs):
return [StubFilter(
self.evaluator,
self.infer_state,
context=self,
search_global=search_global,
**filter_kwargs
+26 -26
View File
@@ -89,9 +89,9 @@ def _cache_stub_file_map(version_info):
def import_module_decorator(func):
@wraps(func)
def wrapper(evaluator, import_names, parent_module_context, sys_path, prefer_stubs):
def wrapper(infer_state, import_names, parent_module_context, sys_path, prefer_stubs):
try:
python_context_set = evaluator.module_cache.get(import_names)
python_context_set = infer_state.module_cache.get(import_names)
except KeyError:
if parent_module_context is not None and parent_module_context.is_stub():
parent_module_contexts = parent_module_context.non_stub_context_set
@@ -104,19 +104,19 @@ def import_module_decorator(func):
# ``os``.
python_parent = next(iter(parent_module_contexts))
if python_parent is None:
python_parent, = evaluator.import_module(('os',), prefer_stubs=False)
python_parent, = infer_state.import_module(('os',), prefer_stubs=False)
python_context_set = python_parent.py__getattribute__('path')
else:
python_context_set = ContextSet.from_sets(
func(evaluator, import_names, p, sys_path,)
func(infer_state, import_names, p, sys_path,)
for p in parent_module_contexts
)
evaluator.module_cache.add(import_names, python_context_set)
infer_state.module_cache.add(import_names, python_context_set)
if not prefer_stubs:
return python_context_set
stub = _try_to_load_stub_cached(evaluator, import_names, python_context_set,
stub = _try_to_load_stub_cached(infer_state, import_names, python_context_set,
parent_module_context, sys_path)
if stub is not None:
return ContextSet([stub])
@@ -125,21 +125,21 @@ def import_module_decorator(func):
return wrapper
def _try_to_load_stub_cached(evaluator, import_names, *args, **kwargs):
def _try_to_load_stub_cached(infer_state, import_names, *args, **kwargs):
try:
return evaluator.stub_module_cache[import_names]
return infer_state.stub_module_cache[import_names]
except KeyError:
pass
# TODO is this needed? where are the exceptions coming from that make this
# necessary? Just remove this line.
evaluator.stub_module_cache[import_names] = None
evaluator.stub_module_cache[import_names] = result = \
_try_to_load_stub(evaluator, import_names, *args, **kwargs)
infer_state.stub_module_cache[import_names] = None
infer_state.stub_module_cache[import_names] = result = \
_try_to_load_stub(infer_state, import_names, *args, **kwargs)
return result
def _try_to_load_stub(evaluator, import_names, python_context_set,
def _try_to_load_stub(infer_state, import_names, python_context_set,
parent_module_context, sys_path):
"""
Trying to load a stub for a set of import_names.
@@ -150,7 +150,7 @@ def _try_to_load_stub(evaluator, import_names, python_context_set,
if parent_module_context is None and len(import_names) > 1:
try:
parent_module_context = _try_to_load_stub_cached(
evaluator, import_names[:-1], NO_CONTEXTS,
infer_state, import_names[:-1], NO_CONTEXTS,
parent_module_context=None, sys_path=sys_path)
except KeyError:
pass
@@ -161,7 +161,7 @@ def _try_to_load_stub(evaluator, import_names, python_context_set,
for p in sys_path:
init = os.path.join(p, *import_names) + '-stubs' + os.path.sep + '__init__.pyi'
m = _try_to_load_stub_from_file(
evaluator,
infer_state,
python_context_set,
file_io=FileIO(init),
import_names=import_names,
@@ -185,7 +185,7 @@ def _try_to_load_stub(evaluator, import_names, python_context_set,
for file_path in file_paths:
m = _try_to_load_stub_from_file(
evaluator,
infer_state,
python_context_set,
# The file path should end with .pyi
file_io=FileIO(file_path),
@@ -195,7 +195,7 @@ def _try_to_load_stub(evaluator, import_names, python_context_set,
return m
# 3. Try to load typeshed
m = _load_from_typeshed(evaluator, python_context_set, parent_module_context, import_names)
m = _load_from_typeshed(infer_state, python_context_set, parent_module_context, import_names)
if m is not None:
return m
@@ -216,7 +216,7 @@ def _try_to_load_stub(evaluator, import_names, python_context_set,
for p in check_path:
m = _try_to_load_stub_from_file(
evaluator,
infer_state,
python_context_set,
file_io=FileIO(os.path.join(p, *names_for_path) + '.pyi'),
import_names=import_names,
@@ -229,11 +229,11 @@ def _try_to_load_stub(evaluator, import_names, python_context_set,
return None
def _load_from_typeshed(evaluator, python_context_set, parent_module_context, import_names):
def _load_from_typeshed(infer_state, python_context_set, parent_module_context, import_names):
import_name = import_names[-1]
map_ = None
if len(import_names) == 1:
map_ = _cache_stub_file_map(evaluator.grammar.version_info)
map_ = _cache_stub_file_map(infer_state.grammar.version_info)
import_name = _IMPORT_MAP.get(import_name, import_name)
elif isinstance(parent_module_context, StubModuleContext):
if not parent_module_context.is_package:
@@ -247,16 +247,16 @@ def _load_from_typeshed(evaluator, python_context_set, parent_module_context, im
path = map_.get(import_name)
if path is not None:
return _try_to_load_stub_from_file(
evaluator,
infer_state,
python_context_set,
file_io=FileIO(path),
import_names=import_names,
)
def _try_to_load_stub_from_file(evaluator, python_context_set, file_io, import_names):
def _try_to_load_stub_from_file(infer_state, python_context_set, file_io, import_names):
try:
stub_module_node = evaluator.parse(
stub_module_node = infer_state.parse(
file_io=file_io,
cache=True,
use_latest_grammar=True
@@ -266,24 +266,24 @@ def _try_to_load_stub_from_file(evaluator, python_context_set, file_io, import_n
return None
else:
return create_stub_module(
evaluator, python_context_set, stub_module_node, file_io,
infer_state, python_context_set, stub_module_node, file_io,
import_names
)
def create_stub_module(evaluator, python_context_set, stub_module_node, file_io, import_names):
def create_stub_module(infer_state, python_context_set, stub_module_node, file_io, import_names):
if import_names == ('typing',):
module_cls = TypingModuleWrapper
else:
module_cls = StubModuleContext
file_name = os.path.basename(file_io.path)
stub_module_context = module_cls(
python_context_set, evaluator, stub_module_node,
python_context_set, infer_state, stub_module_node,
file_io=file_io,
string_names=import_names,
# The code was loaded with latest_grammar, so use
# that.
code_lines=get_cached_code_lines(evaluator.latest_grammar, file_io.path),
code_lines=get_cached_code_lines(infer_state.latest_grammar, file_io.path),
is_package=file_name == '__init__.pyi',
)
return stub_module_context
+34 -34
View File
@@ -7,7 +7,7 @@ This file deals with all the typing.py cases.
"""
from jedi._compatibility import unicode, force_unicode
from jedi import debug
from jedi.inference.cache import evaluator_method_cache
from jedi.inference.cache import infer_state_method_cache
from jedi.inference.compiled import builtin_from_name
from jedi.inference.base_context import ContextSet, NO_CONTEXTS, Context, \
iterator_to_context_set, ContextWrapper, LazyContextWrapper
@@ -45,8 +45,8 @@ class TypingName(AbstractTreeName):
class _BaseTypingContext(Context):
def __init__(self, evaluator, parent_context, tree_name):
super(_BaseTypingContext, self).__init__(evaluator, parent_context)
def __init__(self, infer_state, parent_context, tree_name):
super(_BaseTypingContext, self).__init__(infer_state, parent_context)
self._tree_name = tree_name
@property
@@ -71,7 +71,7 @@ class _BaseTypingContext(Context):
# TODO this is obviously not correct, but at least gives us a class if
# we have none. Some of these objects don't really have a base class in
# typeshed.
return builtin_from_name(self.evaluator, u'object')
return builtin_from_name(self.infer_state, u'object')
@property
def name(self):
@@ -87,39 +87,39 @@ class TypingModuleName(NameWrapper):
def _remap(self):
name = self.string_name
evaluator = self.parent_context.evaluator
infer_state = self.parent_context.infer_state
try:
actual = _TYPE_ALIAS_TYPES[name]
except KeyError:
pass
else:
yield TypeAlias.create_cached(evaluator, self.parent_context, self.tree_name, actual)
yield TypeAlias.create_cached(infer_state, self.parent_context, self.tree_name, actual)
return
if name in _PROXY_CLASS_TYPES:
yield TypingClassContext.create_cached(evaluator, self.parent_context, self.tree_name)
yield TypingClassContext.create_cached(infer_state, self.parent_context, self.tree_name)
elif name in _PROXY_TYPES:
yield TypingContext.create_cached(evaluator, self.parent_context, self.tree_name)
yield TypingContext.create_cached(infer_state, self.parent_context, self.tree_name)
elif name == 'runtime':
# We don't want anything here, not sure what this function is
# supposed to do, since it just appears in the stubs and shouldn't
# have any effects there (because it's never executed).
return
elif name == 'TypeVar':
yield TypeVarClass.create_cached(evaluator, self.parent_context, self.tree_name)
yield TypeVarClass.create_cached(infer_state, self.parent_context, self.tree_name)
elif name == 'Any':
yield Any.create_cached(evaluator, self.parent_context, self.tree_name)
yield Any.create_cached(infer_state, self.parent_context, self.tree_name)
elif name == 'TYPE_CHECKING':
# This is needed for e.g. imports that are only available for type
# checking or are in cycles. The user can then check this variable.
yield builtin_from_name(evaluator, u'True')
yield builtin_from_name(infer_state, u'True')
elif name == 'overload':
yield OverloadFunction.create_cached(evaluator, self.parent_context, self.tree_name)
yield OverloadFunction.create_cached(infer_state, self.parent_context, self.tree_name)
elif name == 'NewType':
yield NewTypeFunction.create_cached(evaluator, self.parent_context, self.tree_name)
yield NewTypeFunction.create_cached(infer_state, self.parent_context, self.tree_name)
elif name == 'cast':
# TODO implement cast
yield CastFunction.create_cached(evaluator, self.parent_context, self.tree_name)
yield CastFunction.create_cached(infer_state, self.parent_context, self.tree_name)
elif name == 'TypedDict':
# TODO doesn't even exist in typeshed/typing.py, yet. But will be
# added soon.
@@ -139,8 +139,8 @@ class TypingModuleFilterWrapper(FilterWrapper):
class _WithIndexBase(_BaseTypingContext):
def __init__(self, evaluator, parent_context, name, index_context, context_of_index):
super(_WithIndexBase, self).__init__(evaluator, parent_context, name)
def __init__(self, infer_state, parent_context, name, index_context, context_of_index):
super(_WithIndexBase, self).__init__(infer_state, parent_context, name)
self._index_context = index_context
self._context_of_index = context_of_index
@@ -164,7 +164,7 @@ class TypingContextWithIndex(_WithIndexBase):
# Optional is basically just saying it's either None or the actual
# type.
return self.gather_annotation_classes().execute_annotation() \
| ContextSet([builtin_from_name(self.evaluator, u'None')])
| ContextSet([builtin_from_name(self.infer_state, u'None')])
elif string_name == 'Type':
# The type is actually already given in the index_context
return ContextSet([self._index_context])
@@ -174,7 +174,7 @@ class TypingContextWithIndex(_WithIndexBase):
cls = globals()[string_name]
return ContextSet([cls(
self.evaluator,
self.infer_state,
self.parent_context,
self._tree_name,
self._index_context,
@@ -194,7 +194,7 @@ class TypingContext(_BaseTypingContext):
def py__getitem__(self, index_context_set, contextualized_node):
return ContextSet(
self.index_class.create_cached(
self.evaluator,
self.infer_state,
self.parent_context,
self._tree_name,
index_context,
@@ -206,7 +206,7 @@ class TypingContext(_BaseTypingContext):
class _TypingClassMixin(object):
def py__bases__(self):
return [LazyKnownContexts(
self.evaluator.builtins_module.py__getattribute__('object')
self.infer_state.builtins_module.py__getattribute__('object')
)]
def get_metaclasses(self):
@@ -246,7 +246,7 @@ def _iter_over_arguments(maybe_tuple_context, defining_context):
class TypeAlias(LazyContextWrapper):
def __init__(self, parent_context, origin_tree_name, actual):
self.evaluator = parent_context.evaluator
self.infer_state = parent_context.infer_state
self.parent_context = parent_context
self._origin_tree_name = origin_tree_name
self._actual = actual # e.g. builtins.list
@@ -263,13 +263,13 @@ class TypeAlias(LazyContextWrapper):
def _get_wrapped_context(self):
module_name, class_name = self._actual.split('.')
if self.evaluator.environment.version_info.major == 2 and module_name == 'builtins':
if self.infer_state.environment.version_info.major == 2 and module_name == 'builtins':
module_name = '__builtin__'
# TODO use evaluator.import_module?
# TODO use infer_state.import_module?
from jedi.inference.imports import Importer
module, = Importer(
self.evaluator, [module_name], self.evaluator.builtins_module
self.infer_state, [module_name], self.infer_state.builtins_module
).follow()
classes = module.py__getattribute__(class_name)
# There should only be one, because it's code that we control.
@@ -358,7 +358,7 @@ class TypeVarClass(_BaseTypingContext):
return NO_CONTEXTS
return ContextSet([TypeVar.create_cached(
self.evaluator,
self.infer_state,
self.parent_context,
self._tree_name,
var_name,
@@ -382,7 +382,7 @@ class TypeVarClass(_BaseTypingContext):
return None
else:
safe_value = method(default=None)
if self.evaluator.environment.version_info.major == 2:
if self.infer_state.environment.version_info.major == 2:
if isinstance(safe_value, bytes):
return force_unicode(safe_value)
if isinstance(safe_value, (str, unicode)):
@@ -391,8 +391,8 @@ class TypeVarClass(_BaseTypingContext):
class TypeVar(_BaseTypingContext):
def __init__(self, evaluator, parent_context, tree_name, var_name, unpacked_args):
super(TypeVar, self).__init__(evaluator, parent_context, tree_name)
def __init__(self, infer_state, parent_context, tree_name, var_name, unpacked_args):
super(TypeVar, self).__init__(infer_state, parent_context, tree_name)
self._var_name = var_name
self._constraints_lazy_contexts = []
@@ -469,7 +469,7 @@ class NewTypeFunction(_BaseTypingContext):
return NO_CONTEXTS
return ContextSet(
NewType(
self.evaluator,
self.infer_state,
contextualized_node.context,
contextualized_node.node,
second_arg.infer(),
@@ -477,8 +477,8 @@ class NewTypeFunction(_BaseTypingContext):
class NewType(Context):
def __init__(self, evaluator, parent_context, tree_node, type_context_set):
super(NewType, self).__init__(evaluator, parent_context)
def __init__(self, infer_state, parent_context, tree_node, type_context_set):
super(NewType, self).__init__(infer_state, parent_context)
self._type_context_set = type_context_set
self.tree_node = tree_node
@@ -643,7 +643,7 @@ class LazyGenericClass(AbstractAnnotatedClass):
self._index_context = index_context
self._context_of_index = context_of_index
@evaluator_method_cache()
@infer_state_method_cache()
def get_generics(self):
return list(_iter_over_arguments(self._index_context, self._context_of_index))
@@ -668,7 +668,7 @@ class LazyAnnotatedBaseClass(object):
if isinstance(base, AbstractAnnotatedClass):
# Here we have to recalculate the given types.
yield GenericClass.create_cached(
base.evaluator,
base.infer_state,
base._wrapped_context,
tuple(self._remap_type_vars(base)),
)
@@ -703,5 +703,5 @@ class InstanceWrapper(ContextWrapper):
except IndexError:
pass
elif cls.py__name__() == 'Iterator':
return ContextSet([builtin_from_name(self.evaluator, u'None')])
return ContextSet([builtin_from_name(self.infer_state, u'None')])
return self._wrapped_context.py__stop_iteration_returns()
+4 -4
View File
@@ -3,7 +3,7 @@ import os
from jedi.inference.gradual.typeshed import TYPESHED_PATH, create_stub_module
def load_proper_stub_module(evaluator, file_io, import_names, module_node):
def load_proper_stub_module(infer_state, file_io, import_names, module_node):
"""
This function is given a random .pyi file and should return the proper
module.
@@ -20,13 +20,13 @@ def load_proper_stub_module(evaluator, file_io, import_names, module_node):
import_names = import_names[:-1]
if import_names is not None:
actual_context_set = evaluator.import_module(import_names, prefer_stubs=False)
actual_context_set = infer_state.import_module(import_names, prefer_stubs=False)
if not actual_context_set:
return None
stub = create_stub_module(
evaluator, actual_context_set, module_node, file_io, import_names
infer_state, actual_context_set, module_node, file_io, import_names
)
evaluator.stub_module_cache[import_names] = stub
infer_state.stub_module_cache[import_names] = stub
return stub
return None