1
0
forked from VimPlug/jedi

infer_state -> inference_state

This commit is contained in:
Dave Halter
2019-08-16 11:44:30 +02:00
parent fffb39227e
commit 03920502c4
60 changed files with 727 additions and 727 deletions
+8 -8
View File
@@ -10,7 +10,7 @@ import re
from parso import ParserSyntaxError, parse
from jedi._compatibility import force_unicode
from jedi.inference.cache import infer_state_method_cache
from jedi.inference.cache import inference_state_method_cache
from jedi.inference.base_value import ValueSet, NO_VALUES
from jedi.inference.gradual.typing import TypeVar, LazyGenericClass, \
AbstractAnnotatedClass
@@ -60,7 +60,7 @@ def _infer_annotation_string(value, string, index=None):
def _get_forward_reference_node(value, string):
try:
new_node = value.infer_state.grammar.parse(
new_node = value.inference_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
@infer_state_method_cache()
@inference_state_method_cache()
def infer_param(execution_value, param):
values = _infer_param(execution_value, param)
infer_state = execution_value.infer_state
inference_state = execution_value.inference_state
if param.star_count == 1:
tuple_ = builtin_from_name(infer_state, 'tuple')
tuple_ = builtin_from_name(inference_state, 'tuple')
return ValueSet([GenericClass(
tuple_,
generics=(values,),
) for c in values])
elif param.star_count == 2:
dct = builtin_from_name(infer_state, 'dict')
dct = builtin_from_name(inference_state, 'dict')
return ValueSet([GenericClass(
dct,
generics=(ValueSet([builtin_from_name(infer_state, 'str')]), values),
generics=(ValueSet([builtin_from_name(inference_state, 'str')]), values),
) for c in values])
pass
return values
@@ -190,7 +190,7 @@ def py__annotations__(funcdef):
return dct
@infer_state_method_cache()
@inference_state_method_cache()
def infer_return_types(function_execution_value):
"""
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.infer_state,
module.inference_state,
import_names=module.string_names,
python_value_set=ValueSet([module]),
parent_module_value=None,
sys_path=module.infer_state.get_sys_path(),
sys_path=module.inference_state.get_sys_path(),
)
+26 -26
View File
@@ -89,9 +89,9 @@ def _cache_stub_file_map(version_info):
def import_module_decorator(func):
@wraps(func)
def wrapper(infer_state, import_names, parent_module_value, sys_path, prefer_stubs):
def wrapper(inference_state, import_names, parent_module_value, sys_path, prefer_stubs):
try:
python_value_set = infer_state.module_cache.get(import_names)
python_value_set = inference_state.module_cache.get(import_names)
except KeyError:
if parent_module_value is not None and parent_module_value.is_stub():
parent_module_values = parent_module_value.non_stub_value_set
@@ -104,19 +104,19 @@ def import_module_decorator(func):
# ``os``.
python_parent = next(iter(parent_module_values))
if python_parent is None:
python_parent, = infer_state.import_module(('os',), prefer_stubs=False)
python_parent, = inference_state.import_module(('os',), prefer_stubs=False)
python_value_set = python_parent.py__getattribute__('path')
else:
python_value_set = ValueSet.from_sets(
func(infer_state, import_names, p, sys_path,)
func(inference_state, import_names, p, sys_path,)
for p in parent_module_values
)
infer_state.module_cache.add(import_names, python_value_set)
inference_state.module_cache.add(import_names, python_value_set)
if not prefer_stubs:
return python_value_set
stub = _try_to_load_stub_cached(infer_state, import_names, python_value_set,
stub = _try_to_load_stub_cached(inference_state, import_names, python_value_set,
parent_module_value, sys_path)
if stub is not None:
return ValueSet([stub])
@@ -125,21 +125,21 @@ def import_module_decorator(func):
return wrapper
def _try_to_load_stub_cached(infer_state, import_names, *args, **kwargs):
def _try_to_load_stub_cached(inference_state, import_names, *args, **kwargs):
try:
return infer_state.stub_module_cache[import_names]
return inference_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.
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)
inference_state.stub_module_cache[import_names] = None
inference_state.stub_module_cache[import_names] = result = \
_try_to_load_stub(inference_state, import_names, *args, **kwargs)
return result
def _try_to_load_stub(infer_state, import_names, python_value_set,
def _try_to_load_stub(inference_state, import_names, python_value_set,
parent_module_value, sys_path):
"""
Trying to load a stub for a set of import_names.
@@ -150,7 +150,7 @@ def _try_to_load_stub(infer_state, import_names, python_value_set,
if parent_module_value is None and len(import_names) > 1:
try:
parent_module_value = _try_to_load_stub_cached(
infer_state, import_names[:-1], NO_VALUES,
inference_state, import_names[:-1], NO_VALUES,
parent_module_value=None, sys_path=sys_path)
except KeyError:
pass
@@ -161,7 +161,7 @@ def _try_to_load_stub(infer_state, import_names, python_value_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(
infer_state,
inference_state,
python_value_set,
file_io=FileIO(init),
import_names=import_names,
@@ -185,7 +185,7 @@ def _try_to_load_stub(infer_state, import_names, python_value_set,
for file_path in file_paths:
m = _try_to_load_stub_from_file(
infer_state,
inference_state,
python_value_set,
# The file path should end with .pyi
file_io=FileIO(file_path),
@@ -195,7 +195,7 @@ def _try_to_load_stub(infer_state, import_names, python_value_set,
return m
# 3. Try to load typeshed
m = _load_from_typeshed(infer_state, python_value_set, parent_module_value, import_names)
m = _load_from_typeshed(inference_state, python_value_set, parent_module_value, import_names)
if m is not None:
return m
@@ -216,7 +216,7 @@ def _try_to_load_stub(infer_state, import_names, python_value_set,
for p in check_path:
m = _try_to_load_stub_from_file(
infer_state,
inference_state,
python_value_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(infer_state, import_names, python_value_set,
return None
def _load_from_typeshed(infer_state, python_value_set, parent_module_value, import_names):
def _load_from_typeshed(inference_state, python_value_set, parent_module_value, import_names):
import_name = import_names[-1]
map_ = None
if len(import_names) == 1:
map_ = _cache_stub_file_map(infer_state.grammar.version_info)
map_ = _cache_stub_file_map(inference_state.grammar.version_info)
import_name = _IMPORT_MAP.get(import_name, import_name)
elif isinstance(parent_module_value, StubModuleValue):
if not parent_module_value.is_package:
@@ -247,16 +247,16 @@ def _load_from_typeshed(infer_state, python_value_set, parent_module_value, impo
path = map_.get(import_name)
if path is not None:
return _try_to_load_stub_from_file(
infer_state,
inference_state,
python_value_set,
file_io=FileIO(path),
import_names=import_names,
)
def _try_to_load_stub_from_file(infer_state, python_value_set, file_io, import_names):
def _try_to_load_stub_from_file(inference_state, python_value_set, file_io, import_names):
try:
stub_module_node = infer_state.parse(
stub_module_node = inference_state.parse(
file_io=file_io,
cache=True,
use_latest_grammar=True
@@ -266,24 +266,24 @@ def _try_to_load_stub_from_file(infer_state, python_value_set, file_io, import_n
return None
else:
return create_stub_module(
infer_state, python_value_set, stub_module_node, file_io,
inference_state, python_value_set, stub_module_node, file_io,
import_names
)
def create_stub_module(infer_state, python_value_set, stub_module_node, file_io, import_names):
def create_stub_module(inference_state, python_value_set, stub_module_node, file_io, import_names):
if import_names == ('typing',):
module_cls = TypingModuleWrapper
else:
module_cls = StubModuleValue
file_name = os.path.basename(file_io.path)
stub_module_value = module_cls(
python_value_set, infer_state, stub_module_node,
python_value_set, inference_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(infer_state.latest_grammar, file_io.path),
code_lines=get_cached_code_lines(inference_state.latest_grammar, file_io.path),
is_package=file_name == '__init__.pyi',
)
return stub_module_value
+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 infer_state_method_cache
from jedi.inference.cache import inference_state_method_cache
from jedi.inference.compiled import builtin_from_name
from jedi.inference.base_value import ValueSet, NO_VALUES, Value, \
iterator_to_value_set, ValueWrapper, LazyValueWrapper
@@ -45,8 +45,8 @@ class TypingName(AbstractTreeName):
class _BaseTypingValue(Value):
def __init__(self, infer_state, parent_context, tree_name):
super(_BaseTypingValue, self).__init__(infer_state, parent_context)
def __init__(self, inference_state, parent_context, tree_name):
super(_BaseTypingValue, self).__init__(inference_state, parent_context)
self._tree_name = tree_name
@property
@@ -71,7 +71,7 @@ class _BaseTypingValue(Value):
# 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.infer_state, u'object')
return builtin_from_name(self.inference_state, u'object')
@property
def name(self):
@@ -87,39 +87,39 @@ class TypingModuleName(NameWrapper):
def _remap(self):
name = self.string_name
infer_state = self.parent_context.infer_state
inference_state = self.parent_context.inference_state
try:
actual = _TYPE_ALIAS_TYPES[name]
except KeyError:
pass
else:
yield TypeAlias.create_cached(infer_state, self.parent_context, self.tree_name, actual)
yield TypeAlias.create_cached(inference_state, self.parent_context, self.tree_name, actual)
return
if name in _PROXY_CLASS_TYPES:
yield TypingClassValue.create_cached(infer_state, self.parent_context, self.tree_name)
yield TypingClassValue.create_cached(inference_state, self.parent_context, self.tree_name)
elif name in _PROXY_TYPES:
yield TypingValue.create_cached(infer_state, self.parent_context, self.tree_name)
yield TypingValue.create_cached(inference_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(infer_state, self.parent_context, self.tree_name)
yield TypeVarClass.create_cached(inference_state, self.parent_context, self.tree_name)
elif name == 'Any':
yield Any.create_cached(infer_state, self.parent_context, self.tree_name)
yield Any.create_cached(inference_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(infer_state, u'True')
yield builtin_from_name(inference_state, u'True')
elif name == 'overload':
yield OverloadFunction.create_cached(infer_state, self.parent_context, self.tree_name)
yield OverloadFunction.create_cached(inference_state, self.parent_context, self.tree_name)
elif name == 'NewType':
yield NewTypeFunction.create_cached(infer_state, self.parent_context, self.tree_name)
yield NewTypeFunction.create_cached(inference_state, self.parent_context, self.tree_name)
elif name == 'cast':
# TODO implement cast
yield CastFunction.create_cached(infer_state, self.parent_context, self.tree_name)
yield CastFunction.create_cached(inference_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(_BaseTypingValue):
def __init__(self, infer_state, parent_context, name, index_value, value_of_index):
super(_WithIndexBase, self).__init__(infer_state, parent_context, name)
def __init__(self, inference_state, parent_context, name, index_value, value_of_index):
super(_WithIndexBase, self).__init__(inference_state, parent_context, name)
self._index_value = index_value
self._value_of_index = value_of_index
@@ -164,7 +164,7 @@ class TypingValueWithIndex(_WithIndexBase):
# Optional is basically just saying it's either None or the actual
# type.
return self.gather_annotation_classes().execute_annotation() \
| ValueSet([builtin_from_name(self.infer_state, u'None')])
| ValueSet([builtin_from_name(self.inference_state, u'None')])
elif string_name == 'Type':
# The type is actually already given in the index_value
return ValueSet([self._index_value])
@@ -174,7 +174,7 @@ class TypingValueWithIndex(_WithIndexBase):
cls = globals()[string_name]
return ValueSet([cls(
self.infer_state,
self.inference_state,
self.parent_context,
self._tree_name,
self._index_value,
@@ -194,7 +194,7 @@ class TypingValue(_BaseTypingValue):
def py__getitem__(self, index_value_set, valueualized_node):
return ValueSet(
self.index_class.create_cached(
self.infer_state,
self.inference_state,
self.parent_context,
self._tree_name,
index_value,
@@ -206,7 +206,7 @@ class TypingValue(_BaseTypingValue):
class _TypingClassMixin(object):
def py__bases__(self):
return [LazyKnownValues(
self.infer_state.builtins_module.py__getattribute__('object')
self.inference_state.builtins_module.py__getattribute__('object')
)]
def get_metaclasses(self):
@@ -246,7 +246,7 @@ def _iter_over_arguments(maybe_tuple_value, defining_value):
class TypeAlias(LazyValueWrapper):
def __init__(self, parent_context, origin_tree_name, actual):
self.infer_state = parent_context.infer_state
self.inference_state = parent_context.inference_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(LazyValueWrapper):
def _get_wrapped_value(self):
module_name, class_name = self._actual.split('.')
if self.infer_state.environment.version_info.major == 2 and module_name == 'builtins':
if self.inference_state.environment.version_info.major == 2 and module_name == 'builtins':
module_name = '__builtin__'
# TODO use infer_state.import_module?
# TODO use inference_state.import_module?
from jedi.inference.imports import Importer
module, = Importer(
self.infer_state, [module_name], self.infer_state.builtins_module
self.inference_state, [module_name], self.inference_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(_BaseTypingValue):
return NO_VALUES
return ValueSet([TypeVar.create_cached(
self.infer_state,
self.inference_state,
self.parent_context,
self._tree_name,
var_name,
@@ -382,7 +382,7 @@ class TypeVarClass(_BaseTypingValue):
return None
else:
safe_value = method(default=None)
if self.infer_state.environment.version_info.major == 2:
if self.inference_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(_BaseTypingValue):
class TypeVar(_BaseTypingValue):
def __init__(self, infer_state, parent_context, tree_name, var_name, unpacked_args):
super(TypeVar, self).__init__(infer_state, parent_context, tree_name)
def __init__(self, inference_state, parent_context, tree_name, var_name, unpacked_args):
super(TypeVar, self).__init__(inference_state, parent_context, tree_name)
self._var_name = var_name
self._constraints_lazy_values = []
@@ -469,7 +469,7 @@ class NewTypeFunction(_BaseTypingValue):
return NO_VALUES
return ValueSet(
NewType(
self.infer_state,
self.inference_state,
valueualized_node.value,
valueualized_node.node,
second_arg.infer(),
@@ -477,8 +477,8 @@ class NewTypeFunction(_BaseTypingValue):
class NewType(Value):
def __init__(self, infer_state, parent_context, tree_node, type_value_set):
super(NewType, self).__init__(infer_state, parent_context)
def __init__(self, inference_state, parent_context, tree_node, type_value_set):
super(NewType, self).__init__(inference_state, parent_context)
self._type_value_set = type_value_set
self.tree_node = tree_node
@@ -643,7 +643,7 @@ class LazyGenericClass(AbstractAnnotatedClass):
self._index_value = index_value
self._value_of_index = value_of_index
@infer_state_method_cache()
@inference_state_method_cache()
def get_generics(self):
return list(_iter_over_arguments(self._index_value, self._value_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.infer_state,
base.inference_state,
base._wrapped_value,
tuple(self._remap_type_vars(base)),
)
@@ -703,5 +703,5 @@ class InstanceWrapper(ValueWrapper):
except IndexError:
pass
elif cls.py__name__() == 'Iterator':
return ValueSet([builtin_from_name(self.infer_state, u'None')])
return ValueSet([builtin_from_name(self.inference_state, u'None')])
return self._wrapped_value.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(infer_state, file_io, import_names, module_node):
def load_proper_stub_module(inference_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(infer_state, file_io, import_names, module_node):
import_names = import_names[:-1]
if import_names is not None:
actual_value_set = infer_state.import_module(import_names, prefer_stubs=False)
actual_value_set = inference_state.import_module(import_names, prefer_stubs=False)
if not actual_value_set:
return None
stub = create_stub_module(
infer_state, actual_value_set, module_node, file_io, import_names
inference_state, actual_value_set, module_node, file_io, import_names
)
infer_state.stub_module_cache[import_names] = stub
inference_state.stub_module_cache[import_names] = stub
return stub
return None