More fixes

This commit is contained in:
Dave Halter
2019-08-17 17:01:21 +02:00
parent 2629ff55f3
commit 680388a7e8
8 changed files with 47 additions and 40 deletions

View File

@@ -170,6 +170,9 @@ class Value(HelperValueMixin, BaseValue):
def is_bound_method(self): def is_bound_method(self):
return False return False
def is_builtins_module(self):
return False
def py__bool__(self): def py__bool__(self):
""" """
Since Wrapper is a super class for classes, functions and modules, Since Wrapper is a super class for classes, functions and modules,

View File

@@ -64,7 +64,7 @@ class NameFinder(object):
return NO_VALUES return NO_VALUES
return self._found_predefined_types return self._found_predefined_types
types = self._names_to_types(names, attribute_lookup) types = self._names_to_types(names)
if not names and self._analysis_errors and not types \ if not names and self._analysis_errors and not types \
and not (isinstance(self._name, tree.Name) and and not (isinstance(self._name, tree.Name) and
@@ -184,7 +184,7 @@ class NameFinder(object):
inst.get_function_slot_names(u'__getattribute__')) inst.get_function_slot_names(u'__getattribute__'))
return inst.execute_function_slots(names, name) return inst.execute_function_slots(names, name)
def _names_to_types(self, names, attribute_lookup): def _names_to_types(self, names):
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)

View File

@@ -20,6 +20,7 @@ from jedi.inference.names import NameWrapper, AbstractTreeName, \
AbstractNameDefinition, ValueName AbstractNameDefinition, ValueName
from jedi.inference.helpers import is_string from jedi.inference.helpers import is_string
from jedi.inference.value.klass import ClassMixin, ClassFilter from jedi.inference.value.klass import ClassMixin, ClassFilter
from jedi.inference.context import ClassContext
_PROXY_CLASS_TYPES = 'Tuple Generic Protocol Callable Type'.split() _PROXY_CLASS_TYPES = 'Tuple Generic Protocol Callable Type'.split()
_TYPE_ALIAS_TYPES = { _TYPE_ALIAS_TYPES = {
@@ -549,21 +550,23 @@ class TypeVarFilter(object):
return [] return []
class AbstractAnnotatedClass(ClassMixin, ValueWrapper): class AnnotatedClassContext(ClassContext):
def get_type_var_filter(self):
return TypeVarFilter(self.get_generics(), self.list_type_vars())
def get_filters(self, *args, **kwargs): def get_filters(self, *args, **kwargs):
filters = super(AbstractAnnotatedClass, self).get_filters( filters = super(AnnotatedClassContext, self).get_filters(
*args, **kwargs *args, **kwargs
) )
for f in filters: for f in filters:
yield f yield f
if search_global: # The type vars can only be looked up if it's a global search and
# The type vars can only be looked up if it's a global search and # not a direct lookup on the class.
# not a direct lookup on the class. yield self._value.get_type_var_filter()
yield self.get_type_var_filter()
class AbstractAnnotatedClass(ClassMixin, ValueWrapper):
def get_type_var_filter(self):
return TypeVarFilter(self.get_generics(), self.list_type_vars())
def is_same_class(self, other): def is_same_class(self, other):
if not isinstance(other, AbstractAnnotatedClass): if not isinstance(other, AbstractAnnotatedClass):
@@ -623,6 +626,9 @@ class AbstractAnnotatedClass(ClassMixin, ValueWrapper):
generics=tuple(new_generics) generics=tuple(new_generics)
)]) )])
def as_context(self):
return AnnotatedClassContext(self)
def __repr__(self): def __repr__(self):
return '<%s: %s%s>' % ( return '<%s: %s%s>' % (
self.__class__.__name__, self.__class__.__name__,

View File

@@ -75,7 +75,7 @@ def infer_node(context, element):
if typ in ('name', 'number', 'string', 'atom', 'strings', 'keyword', 'fstring'): if typ in ('name', 'number', 'string', 'atom', 'strings', 'keyword', 'fstring'):
return infer_atom(context, element) return infer_atom(context, element)
elif typ == 'lambdef': elif typ == 'lambdef':
return ValueSet([FunctionValue.from_value(context, element)]) return ValueSet([FunctionValue.from_context(context, element)])
elif typ == 'expr_stmt': elif typ == 'expr_stmt':
return infer_expr_stmt(context, element) return infer_expr_stmt(context, element)
elif typ in ('power', 'atom_expr'): elif typ in ('power', 'atom_expr'):
@@ -571,8 +571,7 @@ def tree_name_to_values(inference_state, context, tree_name):
node = tree_name.parent node = tree_name.parent
if node.type == 'global_stmt': if node.type == 'global_stmt':
c = context.create_context(tree_name) c = context.create_context(tree_name)
raise NotImplementedError finder = NameFinder(inference_state, c, c, tree_name.value)
finder = NameFinder(inference_state, value, value, tree_name.value)
filters = finder.get_global_filters() filters = finder.get_global_filters()
# For global_stmt lookups, we only need the first possible scope, # For global_stmt lookups, we only need the first possible scope,
# which means the function itself. # which means the function itself.
@@ -642,7 +641,7 @@ def _apply_decorators(context, node):
tree_node=node tree_node=node
) )
else: else:
decoratee_value = FunctionValue.from_value(context, node) decoratee_value = FunctionValue.from_context(context, node)
initial = values = ValueSet([decoratee_value]) initial = values = ValueSet([decoratee_value])
for dec in reversed(node.get_decorators()): for dec in reversed(node.get_decorators()):
debug.dbg('decorator: %s %s', dec, values, color="MAGENTA") debug.dbg('decorator: %s %s', dec, values, color="MAGENTA")

View File

@@ -16,6 +16,7 @@ from jedi.inference.base_value import ValueualizedNode, NO_VALUES, \
ValueSet, TreeValue, ValueWrapper ValueSet, TreeValue, ValueWrapper
from jedi.inference.lazy_value import LazyKnownValues, LazyKnownValue, \ from jedi.inference.lazy_value import LazyKnownValues, LazyKnownValue, \
LazyTreeValue LazyTreeValue
from jedi.inference.context import AbstractContext
from jedi.inference.value import iterable from jedi.inference.value import iterable
from jedi import parser_utils from jedi import parser_utils
from jedi.inference.parser_cache import get_yield_exprs from jedi.inference.parser_cache import get_yield_exprs
@@ -90,7 +91,7 @@ class FunctionMixin(object):
if arguments is None: if arguments is None:
arguments = AnonymousArguments() arguments = AnonymousArguments()
return FunctionExecutionContext(self.inference_state, self.parent_context, self, arguments) return FunctionExecutionContext(self, arguments)
def get_signatures(self): def get_signatures(self):
return [TreeSignature(f) for f in self.get_signature_functions()] return [TreeSignature(f) for f in self.get_signature_functions()]
@@ -101,25 +102,25 @@ class FunctionValue(use_metaclass(CachedMetaClass, FunctionMixin, FunctionAndCla
return True return True
@classmethod @classmethod
def from_value(cls, value, tree_node): def from_context(cls, context, tree_node):
def create(tree_node): def create(tree_node):
if value.is_class(): if context.is_class():
return MethodValue( return MethodValue(
value.inference_state, context.inference_state,
value, context,
parent_context=parent_context, parent_context=parent_context,
tree_node=tree_node tree_node=tree_node
) )
else: else:
return cls( return cls(
value.inference_state, context.inference_state,
parent_context=parent_context, parent_context=parent_context,
tree_node=tree_node tree_node=tree_node
) )
overloaded_funcs = list(_find_overload_functions(value, tree_node)) overloaded_funcs = list(_find_overload_functions(context, tree_node))
parent_context = value parent_context = context
while parent_context.is_class() or parent_context.is_instance(): while parent_context.is_class() or parent_context.is_instance():
parent_context = parent_context.parent_context parent_context = parent_context.parent_context
@@ -160,15 +161,11 @@ class MethodValue(FunctionValue):
return names + (self.py__name__(),) return names + (self.py__name__(),)
class FunctionExecutionContext(TreeValue): class FunctionExecutionContext(AbstractContext):
function_execution_filter = FunctionExecutionFilter function_execution_filter = FunctionExecutionFilter
def __init__(self, inference_state, parent_context, function_value, var_args): def __init__(self, function_value, var_args):
super(FunctionExecutionContext, self).__init__( super(FunctionExecutionContext, self).__init__(function_value)
inference_state,
parent_context,
function_value.tree_node,
)
self.function_value = function_value self.function_value = function_value
self.var_args = var_args self.var_args = var_args

View File

@@ -190,7 +190,7 @@ class AbstractInstanceValue(Value):
for name in self.get_function_slot_names(u'__init__'): for name in self.get_function_slot_names(u'__init__'):
# TODO is this correct? I think we need to check for functions. # TODO is this correct? I think we need to check for functions.
if isinstance(name, LazyInstanceClassName): if isinstance(name, LazyInstanceClassName):
function = FunctionValue.from_value( function = FunctionValue.from_context(
self.parent_context, self.parent_context,
name.tree_name.parent name.tree_name.parent
) )
@@ -207,7 +207,7 @@ class AbstractInstanceValue(Value):
else: else:
parent_context = self.create_instance_value(class_value, scope) parent_context = self.create_instance_value(class_value, scope)
if scope.type == 'funcdef': if scope.type == 'funcdef':
func = FunctionValue.from_value( func = FunctionValue.from_context(
parent_context, parent_context,
scope, scope,
) )

View File

@@ -114,10 +114,11 @@ def execute(callback):
except AttributeError: except AttributeError:
pass pass
else: else:
if value.parent_context.is_builtins_module(): p = value.parent_context
if p is not None and p.is_builtins_module():
module_name = 'builtins' module_name = 'builtins'
elif value.parent_context is not None and value.parent_context.is_module(): elif p is not None and p.is_module():
module_name = value.parent_context.py__name__() module_name = p.py__name__()
else: else:
return call() return call()

View File

@@ -224,14 +224,15 @@ class IntegrationTestCase(object):
string = match.group(0) string = match.group(0)
parser = grammar36.parse(string, start_symbol='eval_input', error_recovery=False) parser = grammar36.parse(string, start_symbol='eval_input', error_recovery=False)
parser_utils.move(parser.get_root_node(), self.line_nr) parser_utils.move(parser.get_root_node(), self.line_nr)
element = parser.get_root_node() node = parser.get_root_node()
module_context = script._get_module_context() module_context = script._get_module_context()
user_context = get_user_context(module_context, (self.line_nr, 0)) user_context = get_user_context(module_context, (self.line_nr, 0))
if user_context._value.api_type == 'function': # TODO needed?
user_context = user_context.get_function_execution() #if user_context._value.api_type == 'function':
element.parent = user_context.tree_node # user_context = user_context.get_function_execution()
node.parent = user_context.tree_node
results = convert_values( results = convert_values(
inference_state.infer_element(user_context, element), user_context.infer_node(node),
) )
if not results: if not results:
raise Exception('Could not resolve %s on line %s' raise Exception('Could not resolve %s on line %s'