mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 14:34:31 +08:00
Move get_node() to tree_node and replace all the custom classdefs/funcdefs.
This commit is contained in:
@@ -367,7 +367,8 @@ class BaseDefinition(object):
|
|||||||
if isinstance(context, er.FunctionExecutionContext):
|
if isinstance(context, er.FunctionExecutionContext):
|
||||||
# TODO the function context should be a part of the function
|
# TODO the function context should be a part of the function
|
||||||
# execution context.
|
# execution context.
|
||||||
context = er.FunctionContext(self._evaluator, context.parent_context, context.funcdef)
|
context = er.FunctionContext(
|
||||||
|
self._evaluator, context.parent_context, context.tree_node)
|
||||||
return Definition(self._evaluator, context.name)
|
return Definition(self._evaluator, context.name)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
@@ -690,7 +691,7 @@ class _Help(object):
|
|||||||
if followed:
|
if followed:
|
||||||
# TODO: Use all of the followed objects as input to Documentation.
|
# TODO: Use all of the followed objects as input to Documentation.
|
||||||
context = next(iter(followed))
|
context = next(iter(followed))
|
||||||
return context.get_node()
|
return context.tree_node
|
||||||
if self._name.tree_name is None:
|
if self._name.tree_name is None:
|
||||||
return None
|
return None
|
||||||
return self._name.tree_name.get_definition()
|
return self._name.tree_name.get_definition()
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ def get_user_scope(module_context, position):
|
|||||||
"""
|
"""
|
||||||
Returns the scope in which the user resides. This includes flows.
|
Returns the scope in which the user resides. This includes flows.
|
||||||
"""
|
"""
|
||||||
user_stmt = module_context.module_node.get_statement_for_position(position)
|
user_stmt = module_context.tree_node.get_statement_for_position(position)
|
||||||
if user_stmt is None:
|
if user_stmt is None:
|
||||||
def scan(scope):
|
def scan(scope):
|
||||||
for s in scope.children:
|
for s in scope.children:
|
||||||
@@ -62,7 +62,7 @@ def get_user_scope(module_context, position):
|
|||||||
return scan(s)
|
return scan(s)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
scanned_node = scan(module_context.module_node)
|
scanned_node = scan(module_context.tree_node)
|
||||||
if scanned_node:
|
if scanned_node:
|
||||||
return module_context.create_context(scanned_node, node_is_context=True)
|
return module_context.create_context(scanned_node, node_is_context=True)
|
||||||
return module_context
|
return module_context
|
||||||
@@ -74,7 +74,7 @@ class Completion:
|
|||||||
def __init__(self, evaluator, module, code_lines, position, call_signatures_method):
|
def __init__(self, evaluator, module, code_lines, position, call_signatures_method):
|
||||||
self._evaluator = evaluator
|
self._evaluator = evaluator
|
||||||
self._module_context = module
|
self._module_context = module
|
||||||
self._module_node = module.module_node
|
self._module_node = module.tree_node
|
||||||
self._code_lines = code_lines
|
self._code_lines = code_lines
|
||||||
|
|
||||||
# The first step of completions is to get the name
|
# The first step of completions is to get the name
|
||||||
@@ -197,7 +197,7 @@ class Completion:
|
|||||||
debug.dbg('trailer completion contexts: %s', contexts)
|
debug.dbg('trailer completion contexts: %s', contexts)
|
||||||
for context in contexts:
|
for context in contexts:
|
||||||
for filter in context.get_filters(
|
for filter in context.get_filters(
|
||||||
search_global=False, origin_scope=user_context.get_node()):
|
search_global=False, origin_scope=user_context.tree_node):
|
||||||
completion_names += filter.values()
|
completion_names += filter.values()
|
||||||
return completion_names
|
return completion_names
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ def usages(evaluator, definition_names, mods):
|
|||||||
definition_names = set(resolve_names(definition_names))
|
definition_names = set(resolve_names(definition_names))
|
||||||
for m in imports.get_modules_containing_name(evaluator, mods, search_name):
|
for m in imports.get_modules_containing_name(evaluator, mods, search_name):
|
||||||
if isinstance(m, ModuleContext):
|
if isinstance(m, ModuleContext):
|
||||||
for name_node in m.module_node.used_names.get(search_name, []):
|
for name_node in m.tree_node.used_names.get(search_name, []):
|
||||||
context = evaluator.create_context(m, name_node)
|
context = evaluator.create_context(m, name_node)
|
||||||
result = evaluator.goto(context, name_node)
|
result = evaluator.goto(context, name_node)
|
||||||
if [c for c in compare_array(result) if c in compare_definitions]:
|
if [c for c in compare_array(result) if c in compare_definitions]:
|
||||||
|
|||||||
@@ -570,7 +570,7 @@ class Evaluator(object):
|
|||||||
return iterable.CompForContext.from_comp_for(parent_context, scope_node)
|
return iterable.CompForContext.from_comp_for(parent_context, scope_node)
|
||||||
raise Exception("There's a scope that was not managed.")
|
raise Exception("There's a scope that was not managed.")
|
||||||
|
|
||||||
base_node = base_context.get_node()
|
base_node = base_context.tree_node
|
||||||
|
|
||||||
if node_is_context and node.is_scope():
|
if node_is_context and node.is_scope():
|
||||||
scope_node = node
|
scope_node = node
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ def _check_for_setattr(instance):
|
|||||||
if not isinstance(module, ModuleContext):
|
if not isinstance(module, ModuleContext):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
node = module.module_node
|
node = module.tree_node
|
||||||
try:
|
try:
|
||||||
stmts = node.used_names['setattr']
|
stmts = node.used_names['setattr']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ class CompiledObject(Context):
|
|||||||
super(CompiledObject, self).__init__(evaluator, parent_context)
|
super(CompiledObject, self).__init__(evaluator, parent_context)
|
||||||
self.obj = obj
|
self.obj = obj
|
||||||
# This attribute will not be set for most classes, except for fakes.
|
# This attribute will not be set for most classes, except for fakes.
|
||||||
self.classdef = faked_class
|
self.tree_node = faked_class
|
||||||
|
|
||||||
def get_root_node(self):
|
def get_root_node(self):
|
||||||
# To make things a bit easier with filters we add this method here.
|
# To make things a bit easier with filters we add this method here.
|
||||||
|
|||||||
@@ -190,9 +190,9 @@ def _get_faked(module, obj, name=None):
|
|||||||
|
|
||||||
|
|
||||||
def get_faked(evaluator, module, obj, name=None, parent_context=None):
|
def get_faked(evaluator, module, obj, name=None, parent_context=None):
|
||||||
if parent_context and parent_context.classdef is not None:
|
if parent_context and parent_context.tree_node is not None:
|
||||||
# Try to search in already clearly defined stuff.
|
# Try to search in already clearly defined stuff.
|
||||||
found = _search_scope(parent_context.classdef, name)
|
found = _search_scope(parent_context.tree_node, name)
|
||||||
if found is not None:
|
if found is not None:
|
||||||
return found
|
return found
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ class MixedObject(object):
|
|||||||
self.obj = compiled_object.obj
|
self.obj = compiled_object.obj
|
||||||
self._tree_name = tree_name
|
self._tree_name = tree_name
|
||||||
name_module = tree_name.get_root_node()
|
name_module = tree_name.get_root_node()
|
||||||
if parent_context.get_node().get_root_node() != name_module:
|
if parent_context.tree_node.get_root_node() != name_module:
|
||||||
from jedi.evaluate.representation import ModuleContext
|
from jedi.evaluate.representation import ModuleContext
|
||||||
module_context = ModuleContext(evaluator, name_module)
|
module_context = ModuleContext(evaluator, name_module)
|
||||||
name = compiled_object.get_root_context().py__name__()
|
name = compiled_object.get_root_context().py__name__()
|
||||||
|
|||||||
@@ -8,14 +8,12 @@ class Context(object):
|
|||||||
To be defined by subclasses.
|
To be defined by subclasses.
|
||||||
"""
|
"""
|
||||||
predefined_names = {}
|
predefined_names = {}
|
||||||
|
tree_node = None
|
||||||
|
|
||||||
def __init__(self, evaluator, parent_context=None):
|
def __init__(self, evaluator, parent_context=None):
|
||||||
self.evaluator = evaluator
|
self.evaluator = evaluator
|
||||||
self.parent_context = parent_context
|
self.parent_context = parent_context
|
||||||
|
|
||||||
def get_node(self):
|
|
||||||
return None
|
|
||||||
|
|
||||||
def get_parent_flow_context(self):
|
def get_parent_flow_context(self):
|
||||||
return self.parent_context
|
return self.parent_context
|
||||||
|
|
||||||
@@ -75,7 +73,7 @@ class TreeContext(Context):
|
|||||||
self.predefined_names = {}
|
self.predefined_names = {}
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<%s: %s>' % (self.__class__.__name__, self.get_node())
|
return '<%s: %s>' % (self.__class__.__name__, self.tree_node)
|
||||||
|
|
||||||
|
|
||||||
class FlowContext(TreeContext):
|
class FlowContext(TreeContext):
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ def _search_function_executions(evaluator, module_context, funcdef):
|
|||||||
|
|
||||||
def _get_possible_nodes(module_context, func_string_name):
|
def _get_possible_nodes(module_context, func_string_name):
|
||||||
try:
|
try:
|
||||||
names = module_context.module_node.used_names[func_string_name]
|
names = module_context.tree_node.used_names[func_string_name]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -169,7 +169,7 @@ def _check_name_for_execution(evaluator, context, compare_node, name, trailer):
|
|||||||
yield execution
|
yield execution
|
||||||
|
|
||||||
for value in evaluator.goto_definitions(context, name):
|
for value in evaluator.goto_definitions(context, name):
|
||||||
value_node = value.get_node()
|
value_node = value.tree_node
|
||||||
if compare_node == value_node:
|
if compare_node == value_node:
|
||||||
for func_execution in create_func_excs():
|
for func_execution in create_func_excs():
|
||||||
yield func_execution
|
yield func_execution
|
||||||
@@ -182,7 +182,7 @@ def _check_name_for_execution(evaluator, context, compare_node, name, trailer):
|
|||||||
if len(params) != 1:
|
if len(params) != 1:
|
||||||
continue
|
continue
|
||||||
values = params[0].infer()
|
values = params[0].infer()
|
||||||
nodes = [v.get_node() for v in values]
|
nodes = [v.tree_node for v in values]
|
||||||
if nodes == [compare_node]:
|
if nodes == [compare_node]:
|
||||||
# Found a decorator.
|
# Found a decorator.
|
||||||
module_context = context.get_root_context()
|
module_context = context.get_root_context()
|
||||||
|
|||||||
@@ -165,7 +165,8 @@ class AbstractUsedNamesFilter(AbstractFilter):
|
|||||||
|
|
||||||
|
|
||||||
class ParserTreeFilter(AbstractUsedNamesFilter):
|
class ParserTreeFilter(AbstractUsedNamesFilter):
|
||||||
def __init__(self, evaluator, context, parser_scope, until_position=None, origin_scope=None):
|
def __init__(self, evaluator, context, parser_scope, until_position=None,
|
||||||
|
origin_scope=None, ):
|
||||||
super(ParserTreeFilter, self).__init__(context, parser_scope)
|
super(ParserTreeFilter, self).__init__(context, parser_scope)
|
||||||
self._origin_scope = origin_scope
|
self._origin_scope = origin_scope
|
||||||
self._until_position = until_position
|
self._until_position = until_position
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ class NameFinder(object):
|
|||||||
names = self.filter_name(filters)
|
names = self.filter_name(filters)
|
||||||
if self._found_predefined_types is not None and names:
|
if self._found_predefined_types is not None and names:
|
||||||
check = flow_analysis.reachability_check(
|
check = flow_analysis.reachability_check(
|
||||||
self._context, self._context.get_node(), self._name)
|
self._context, self._context.tree_node, self._name)
|
||||||
if check is flow_analysis.UNREACHABLE:
|
if check is flow_analysis.UNREACHABLE:
|
||||||
return set()
|
return set()
|
||||||
return self._found_predefined_types
|
return self._found_predefined_types
|
||||||
@@ -148,7 +148,7 @@ class NameFinder(object):
|
|||||||
if not types and isinstance(self._name, tree.Name) and \
|
if not types and isinstance(self._name, tree.Name) and \
|
||||||
not isinstance(self._name_context, AbstractInstanceContext):
|
not isinstance(self._name_context, AbstractInstanceContext):
|
||||||
flow_scope = self._name
|
flow_scope = self._name
|
||||||
base_node = self._name_context.get_node()
|
base_node = self._name_context.tree_node
|
||||||
if base_node.type == 'comp_for':
|
if base_node.type == 'comp_for':
|
||||||
return types
|
return types
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ class AbstractInstanceContext(Context):
|
|||||||
if include_self_names:
|
if include_self_names:
|
||||||
for cls in self.class_context.py__mro__():
|
for cls in self.class_context.py__mro__():
|
||||||
if isinstance(cls, compiled.CompiledObject):
|
if isinstance(cls, compiled.CompiledObject):
|
||||||
if cls.classdef is not None:
|
if cls.tree_node is not None:
|
||||||
# In this case we're talking about a fake object, it
|
# In this case we're talking about a fake object, it
|
||||||
# doesn't make sense for normal compiled objects to
|
# doesn't make sense for normal compiled objects to
|
||||||
# search for self variables.
|
# search for self variables.
|
||||||
@@ -152,7 +152,7 @@ class AbstractInstanceContext(Context):
|
|||||||
if node.parent.type in ('funcdef', 'classdef'):
|
if node.parent.type in ('funcdef', 'classdef'):
|
||||||
node = node.parent
|
node = node.parent
|
||||||
scope = node.get_parent_scope()
|
scope = node.get_parent_scope()
|
||||||
if scope == class_context.classdef:
|
if scope == class_context.tree_node:
|
||||||
return class_context
|
return class_context
|
||||||
else:
|
else:
|
||||||
parent_context = self.create_instance_context(class_context, scope)
|
parent_context = self.create_instance_context(class_context, scope)
|
||||||
@@ -225,7 +225,7 @@ class CompiledInstanceName(compiled.CompiledName):
|
|||||||
|
|
||||||
yield BoundMethod(
|
yield BoundMethod(
|
||||||
result_context.evaluator, self._instance, self.parent_context,
|
result_context.evaluator, self._instance, self.parent_context,
|
||||||
parent_context, result_context.funcdef
|
parent_context, result_context.tree_node
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
if result_context.api_type == 'function':
|
if result_context.api_type == 'function':
|
||||||
@@ -268,7 +268,7 @@ class BoundMethod(er.FunctionContext):
|
|||||||
class CompiledBoundMethod(compiled.CompiledObject):
|
class CompiledBoundMethod(compiled.CompiledObject):
|
||||||
def __init__(self, func):
|
def __init__(self, func):
|
||||||
super(CompiledBoundMethod, self).__init__(
|
super(CompiledBoundMethod, self).__init__(
|
||||||
func.evaluator, func.obj, func.parent_context, func.classdef)
|
func.evaluator, func.obj, func.parent_context, func.tree_node)
|
||||||
|
|
||||||
def get_param_names(self):
|
def get_param_names(self):
|
||||||
return list(super(CompiledBoundMethod, self).get_param_names())[1:]
|
return list(super(CompiledBoundMethod, self).get_param_names())[1:]
|
||||||
@@ -308,7 +308,7 @@ class LazyInstanceClassName(LazyInstanceName):
|
|||||||
|
|
||||||
yield BoundMethod(
|
yield BoundMethod(
|
||||||
result_context.evaluator, self._instance, self.class_context,
|
result_context.evaluator, self._instance, self.class_context,
|
||||||
parent_context, result_context.funcdef
|
parent_context, result_context.tree_node
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
for c in er.apply_py__get__(result_context, self._instance):
|
for c in er.apply_py__get__(result_context, self._instance):
|
||||||
@@ -322,7 +322,7 @@ class InstanceClassFilter(filters.ParserTreeFilter):
|
|||||||
super(InstanceClassFilter, self).__init__(
|
super(InstanceClassFilter, self).__init__(
|
||||||
evaluator=evaluator,
|
evaluator=evaluator,
|
||||||
context=context,
|
context=context,
|
||||||
parser_scope=class_context.classdef,
|
parser_scope=class_context.tree_node,
|
||||||
origin_scope=origin_scope
|
origin_scope=origin_scope
|
||||||
)
|
)
|
||||||
self._class_context = class_context
|
self._class_context = class_context
|
||||||
@@ -431,7 +431,7 @@ class InstanceVarArgs(object):
|
|||||||
class InstanceFunctionExecution(er.FunctionExecutionContext):
|
class InstanceFunctionExecution(er.FunctionExecutionContext):
|
||||||
def __init__(self, instance, parent_context, function_context, var_args):
|
def __init__(self, instance, parent_context, function_context, var_args):
|
||||||
self.instance = instance
|
self.instance = instance
|
||||||
var_args = InstanceVarArgs(instance, function_context.funcdef, var_args)
|
var_args = InstanceVarArgs(instance, function_context.tree_node, var_args)
|
||||||
|
|
||||||
super(InstanceFunctionExecution, self).__init__(
|
super(InstanceFunctionExecution, self).__init__(
|
||||||
instance.evaluator, parent_context, function_context, var_args)
|
instance.evaluator, parent_context, function_context, var_args)
|
||||||
|
|||||||
@@ -184,13 +184,13 @@ class CompForContext(context.TreeContext):
|
|||||||
|
|
||||||
def __init__(self, evaluator, parent_context, comp_for):
|
def __init__(self, evaluator, parent_context, comp_for):
|
||||||
super(CompForContext, self).__init__(evaluator, parent_context)
|
super(CompForContext, self).__init__(evaluator, parent_context)
|
||||||
self.node = comp_for
|
self.tree_node = comp_for
|
||||||
|
|
||||||
def get_node(self):
|
def get_node(self):
|
||||||
return self.node
|
return self.tree_node
|
||||||
|
|
||||||
def get_filters(self, search_global, until_position=None, origin_scope=None):
|
def get_filters(self, search_global, until_position=None, origin_scope=None):
|
||||||
yield ParserTreeFilter(self.evaluator, self, self.node)
|
yield ParserTreeFilter(self.evaluator, self, self.tree_node)
|
||||||
|
|
||||||
|
|
||||||
class Comprehension(AbstractSequence):
|
class Comprehension(AbstractSequence):
|
||||||
@@ -758,12 +758,12 @@ def _check_array_additions(context, sequence):
|
|||||||
added_types = set()
|
added_types = set()
|
||||||
for add_name in search_names:
|
for add_name in search_names:
|
||||||
try:
|
try:
|
||||||
possible_names = module_context.module_node.used_names[add_name]
|
possible_names = module_context.tree_node.used_names[add_name]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
for name in possible_names:
|
for name in possible_names:
|
||||||
context_node = context.get_node()
|
context_node = context.tree_node
|
||||||
if not (context_node.start_pos < name.start_pos < context_node.end_pos):
|
if not (context_node.start_pos < name.start_pos < context_node.end_pos):
|
||||||
continue
|
continue
|
||||||
trailer = name.parent
|
trailer = name.parent
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ def _fix_forward_reference(context, node):
|
|||||||
else:
|
else:
|
||||||
module = node.get_parent_until()
|
module = node.get_parent_until()
|
||||||
new_node.move(module.end_pos[0])
|
new_node.move(module.end_pos[0])
|
||||||
new_node.parent = context.get_node()
|
new_node.parent = context.tree_node
|
||||||
return new_node
|
return new_node
|
||||||
else:
|
else:
|
||||||
return node
|
return node
|
||||||
@@ -154,7 +154,7 @@ def py__getitem__(context, typ, node):
|
|||||||
assert len(factories) == 1
|
assert len(factories) == 1
|
||||||
factory = list(factories)[0]
|
factory = list(factories)[0]
|
||||||
assert factory
|
assert factory
|
||||||
function_body_nodes = factory.funcdef.children[4].children
|
function_body_nodes = factory.tree_node.children[4].children
|
||||||
valid_classnames = set(child.name.value
|
valid_classnames = set(child.name.value
|
||||||
for child in function_body_nodes
|
for child in function_body_nodes
|
||||||
if isinstance(child, tree.Class))
|
if isinstance(child, tree.Class))
|
||||||
|
|||||||
@@ -75,12 +75,12 @@ class ExecutionRecursionDetector(object):
|
|||||||
self.recursion_level -= 1
|
self.recursion_level -= 1
|
||||||
|
|
||||||
def push_execution(self, execution):
|
def push_execution(self, execution):
|
||||||
in_par_execution_funcs = execution.funcdef in self.parent_execution_funcs
|
in_par_execution_funcs = execution.tree_node in self.parent_execution_funcs
|
||||||
in_execution_funcs = execution.funcdef in self.execution_funcs
|
in_execution_funcs = execution.tree_node in self.execution_funcs
|
||||||
self.recursion_level += 1
|
self.recursion_level += 1
|
||||||
self.execution_count += 1
|
self.execution_count += 1
|
||||||
self.execution_funcs.add(execution.funcdef)
|
self.execution_funcs.add(execution.tree_node)
|
||||||
self.parent_execution_funcs.append(execution.funcdef)
|
self.parent_execution_funcs.append(execution.tree_node)
|
||||||
|
|
||||||
if self.execution_count > settings.max_executions:
|
if self.execution_count > settings.max_executions:
|
||||||
return True
|
return True
|
||||||
|
|||||||
@@ -105,10 +105,7 @@ class ClassContext(use_metaclass(CachedMetaClass, context.TreeContext)):
|
|||||||
|
|
||||||
def __init__(self, evaluator, classdef, parent_context):
|
def __init__(self, evaluator, classdef, parent_context):
|
||||||
super(ClassContext, self).__init__(evaluator, parent_context=parent_context)
|
super(ClassContext, self).__init__(evaluator, parent_context=parent_context)
|
||||||
self.classdef = classdef
|
self.tree_node = classdef
|
||||||
|
|
||||||
def get_node(self):
|
|
||||||
return self.classdef
|
|
||||||
|
|
||||||
@memoize_default(default=())
|
@memoize_default(default=())
|
||||||
def py__mro__(self):
|
def py__mro__(self):
|
||||||
@@ -148,7 +145,7 @@ class ClassContext(use_metaclass(CachedMetaClass, context.TreeContext)):
|
|||||||
|
|
||||||
@memoize_default(default=())
|
@memoize_default(default=())
|
||||||
def py__bases__(self):
|
def py__bases__(self):
|
||||||
arglist = self.classdef.get_super_arglist()
|
arglist = self.tree_node.get_super_arglist()
|
||||||
if arglist:
|
if arglist:
|
||||||
args = param.TreeArguments(self.evaluator, self, arglist)
|
args = param.TreeArguments(self.evaluator, self, arglist)
|
||||||
return [value for key, value in args.unpack() if key is None]
|
return [value for key, value in args.unpack() if key is None]
|
||||||
@@ -169,14 +166,17 @@ class ClassContext(use_metaclass(CachedMetaClass, context.TreeContext)):
|
|||||||
|
|
||||||
def get_filters(self, search_global, until_position=None, origin_scope=None, is_instance=False):
|
def get_filters(self, search_global, until_position=None, origin_scope=None, is_instance=False):
|
||||||
if search_global:
|
if search_global:
|
||||||
yield ParserTreeFilter(self.evaluator, self, self.classdef, until_position, origin_scope=origin_scope)
|
yield ParserTreeFilter(self.evaluator, self, self.tree_node, until_position, origin_scope=origin_scope)
|
||||||
else:
|
else:
|
||||||
for scope in self.py__mro__():
|
for scope in self.py__mro__():
|
||||||
|
print(scope)
|
||||||
if isinstance(scope, compiled.CompiledObject):
|
if isinstance(scope, compiled.CompiledObject):
|
||||||
for filter in scope.get_filters(is_instance=is_instance):
|
for filter in scope.get_filters(is_instance=is_instance):
|
||||||
yield filter
|
yield filter
|
||||||
else:
|
else:
|
||||||
yield ClassFilter(self.evaluator, self, scope.classdef, origin_scope=origin_scope)
|
yield ClassFilter(
|
||||||
|
self.evaluator, self, scope.tree_node,
|
||||||
|
origin_scope=origin_scope)
|
||||||
|
|
||||||
def is_class(self):
|
def is_class(self):
|
||||||
return True
|
return True
|
||||||
@@ -209,7 +209,7 @@ class ClassContext(use_metaclass(CachedMetaClass, context.TreeContext)):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
return ContextName(self, self.classdef.name)
|
return ContextName(self, self.tree_node.name)
|
||||||
|
|
||||||
|
|
||||||
class FunctionContext(use_metaclass(CachedMetaClass, context.TreeContext)):
|
class FunctionContext(use_metaclass(CachedMetaClass, context.TreeContext)):
|
||||||
@@ -221,14 +221,11 @@ class FunctionContext(use_metaclass(CachedMetaClass, context.TreeContext)):
|
|||||||
def __init__(self, evaluator, parent_context, funcdef):
|
def __init__(self, evaluator, parent_context, funcdef):
|
||||||
""" This should not be called directly """
|
""" This should not be called directly """
|
||||||
super(FunctionContext, self).__init__(evaluator, parent_context)
|
super(FunctionContext, self).__init__(evaluator, parent_context)
|
||||||
self.base = self.base_func = self.funcdef = funcdef
|
self.tree_node = funcdef
|
||||||
|
|
||||||
def get_node(self):
|
|
||||||
return self.funcdef
|
|
||||||
|
|
||||||
def get_filters(self, search_global, until_position=None, origin_scope=None):
|
def get_filters(self, search_global, until_position=None, origin_scope=None):
|
||||||
if search_global:
|
if search_global:
|
||||||
yield ParserTreeFilter(self.evaluator, self, self.base, until_position, origin_scope=origin_scope)
|
yield ParserTreeFilter(self.evaluator, self, self.tree_node, until_position, origin_scope=origin_scope)
|
||||||
else:
|
else:
|
||||||
scope = self.py__class__()
|
scope = self.py__class__()
|
||||||
for filter in scope.get_filters(search_global=False, origin_scope=origin_scope):
|
for filter in scope.get_filters(search_global=False, origin_scope=origin_scope):
|
||||||
@@ -238,7 +235,7 @@ class FunctionContext(use_metaclass(CachedMetaClass, context.TreeContext)):
|
|||||||
"""
|
"""
|
||||||
Created to be used by inheritance.
|
Created to be used by inheritance.
|
||||||
"""
|
"""
|
||||||
if self.base.is_generator():
|
if self.tree_node.is_generator():
|
||||||
return set([iterable.Generator(self.evaluator, function_execution)])
|
return set([iterable.Generator(self.evaluator, function_execution)])
|
||||||
else:
|
else:
|
||||||
return function_execution.get_return_values()
|
return function_execution.get_return_values()
|
||||||
@@ -257,7 +254,7 @@ class FunctionContext(use_metaclass(CachedMetaClass, context.TreeContext)):
|
|||||||
def py__class__(self):
|
def py__class__(self):
|
||||||
# This differentiation is only necessary for Python2. Python3 does not
|
# This differentiation is only necessary for Python2. Python3 does not
|
||||||
# use a different method class.
|
# use a different method class.
|
||||||
if isinstance(self.base.get_parent_scope(), tree.Class):
|
if isinstance(self.tree_node.get_parent_scope(), tree.Class):
|
||||||
name = 'METHOD_CLASS'
|
name = 'METHOD_CLASS'
|
||||||
else:
|
else:
|
||||||
name = 'FUNCTION_CLASS'
|
name = 'FUNCTION_CLASS'
|
||||||
@@ -265,11 +262,11 @@ class FunctionContext(use_metaclass(CachedMetaClass, context.TreeContext)):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
return ContextName(self, self.funcdef.name)
|
return ContextName(self, self.tree_node.name)
|
||||||
|
|
||||||
def get_param_names(self):
|
def get_param_names(self):
|
||||||
function_execution = self.get_function_execution()
|
function_execution = self.get_function_execution()
|
||||||
return [ParamName(function_execution, param.name) for param in self.funcdef.params]
|
return [ParamName(function_execution, param.name) for param in self.tree_node.params]
|
||||||
|
|
||||||
|
|
||||||
class FunctionExecutionContext(Executed):
|
class FunctionExecutionContext(Executed):
|
||||||
@@ -286,15 +283,12 @@ class FunctionExecutionContext(Executed):
|
|||||||
def __init__(self, evaluator, parent_context, function_context, var_args):
|
def __init__(self, evaluator, parent_context, function_context, var_args):
|
||||||
super(FunctionExecutionContext, self).__init__(evaluator, parent_context, var_args)
|
super(FunctionExecutionContext, self).__init__(evaluator, parent_context, var_args)
|
||||||
self.function_context = function_context
|
self.function_context = function_context
|
||||||
self.funcdef = function_context.funcdef
|
self.tree_node = function_context.tree_node
|
||||||
|
|
||||||
def get_node(self):
|
|
||||||
return self.function_context.funcdef
|
|
||||||
|
|
||||||
@memoize_default(default=set())
|
@memoize_default(default=set())
|
||||||
@recursion.execution_recursion_decorator
|
@recursion.execution_recursion_decorator
|
||||||
def get_return_values(self, check_yields=False):
|
def get_return_values(self, check_yields=False):
|
||||||
funcdef = self.funcdef
|
funcdef = self.tree_node
|
||||||
if funcdef.type == 'lambda':
|
if funcdef.type == 'lambda':
|
||||||
return self.evaluator.eval_element(self, funcdef.children[-1])
|
return self.evaluator.eval_element(self, funcdef.children[-1])
|
||||||
|
|
||||||
@@ -335,7 +329,7 @@ class FunctionExecutionContext(Executed):
|
|||||||
def get_yield_values(self):
|
def get_yield_values(self):
|
||||||
for_parents = [(y, tree.search_ancestor(y, ('for_stmt', 'funcdef',
|
for_parents = [(y, tree.search_ancestor(y, ('for_stmt', 'funcdef',
|
||||||
'while_stmt', 'if_stmt')))
|
'while_stmt', 'if_stmt')))
|
||||||
for y in self.funcdef.yields]
|
for y in self.tree_node.yields]
|
||||||
|
|
||||||
# Calculate if the yields are placed within the same for loop.
|
# Calculate if the yields are placed within the same for loop.
|
||||||
yields_order = []
|
yields_order = []
|
||||||
@@ -346,13 +340,13 @@ class FunctionExecutionContext(Executed):
|
|||||||
parent = for_stmt.parent
|
parent = for_stmt.parent
|
||||||
if parent.type == 'suite':
|
if parent.type == 'suite':
|
||||||
parent = parent.parent
|
parent = parent.parent
|
||||||
if for_stmt.type == 'for_stmt' and parent == self.funcdef \
|
if for_stmt.type == 'for_stmt' and parent == self.tree_node \
|
||||||
and for_stmt.defines_one_name(): # Simplicity for now.
|
and for_stmt.defines_one_name(): # Simplicity for now.
|
||||||
if for_stmt == last_for_stmt:
|
if for_stmt == last_for_stmt:
|
||||||
yields_order[-1][1].append(yield_)
|
yields_order[-1][1].append(yield_)
|
||||||
else:
|
else:
|
||||||
yields_order.append((for_stmt, [yield_]))
|
yields_order.append((for_stmt, [yield_]))
|
||||||
elif for_stmt == self.funcdef:
|
elif for_stmt == self.tree_node:
|
||||||
yields_order.append((None, [yield_]))
|
yields_order.append((None, [yield_]))
|
||||||
else:
|
else:
|
||||||
yield self.get_return_values(check_yields=True)
|
yield self.get_return_values(check_yields=True)
|
||||||
@@ -378,13 +372,13 @@ class FunctionExecutionContext(Executed):
|
|||||||
yield result
|
yield result
|
||||||
|
|
||||||
def get_filters(self, search_global, until_position=None, origin_scope=None):
|
def get_filters(self, search_global, until_position=None, origin_scope=None):
|
||||||
yield self.function_execution_filter(self.evaluator, self, self.funcdef,
|
yield self.function_execution_filter(self.evaluator, self, self.tree_node,
|
||||||
until_position,
|
until_position,
|
||||||
origin_scope=origin_scope)
|
origin_scope=origin_scope)
|
||||||
|
|
||||||
@memoize_default(default=NO_DEFAULT)
|
@memoize_default(default=NO_DEFAULT)
|
||||||
def get_params(self):
|
def get_params(self):
|
||||||
return param.get_params(self.evaluator, self.parent_context, self.funcdef, self.var_args)
|
return param.get_params(self.evaluator, self.parent_context, self.tree_node, self.var_args)
|
||||||
|
|
||||||
|
|
||||||
class AnonymousFunctionExecution(FunctionExecutionContext):
|
class AnonymousFunctionExecution(FunctionExecutionContext):
|
||||||
@@ -395,7 +389,7 @@ class AnonymousFunctionExecution(FunctionExecutionContext):
|
|||||||
@memoize_default(default=NO_DEFAULT)
|
@memoize_default(default=NO_DEFAULT)
|
||||||
def get_params(self):
|
def get_params(self):
|
||||||
# We need to do a dynamic search here.
|
# We need to do a dynamic search here.
|
||||||
return search_params(self.evaluator, self.parent_context, self.funcdef)
|
return search_params(self.evaluator, self.parent_context, self.tree_node)
|
||||||
|
|
||||||
|
|
||||||
class ModuleAttributeName(AbstractNameDefinition):
|
class ModuleAttributeName(AbstractNameDefinition):
|
||||||
@@ -420,20 +414,17 @@ class ModuleContext(use_metaclass(CachedMetaClass, context.TreeContext)):
|
|||||||
|
|
||||||
def __init__(self, evaluator, module_node):
|
def __init__(self, evaluator, module_node):
|
||||||
super(ModuleContext, self).__init__(evaluator, parent_context=None)
|
super(ModuleContext, self).__init__(evaluator, parent_context=None)
|
||||||
self.module_node = module_node
|
self.tree_node = module_node
|
||||||
|
|
||||||
def get_node(self):
|
|
||||||
return self.module_node
|
|
||||||
|
|
||||||
def get_filters(self, search_global, until_position=None, origin_scope=None):
|
def get_filters(self, search_global, until_position=None, origin_scope=None):
|
||||||
yield ParserTreeFilter(
|
yield ParserTreeFilter(
|
||||||
self.evaluator,
|
self.evaluator,
|
||||||
self,
|
self,
|
||||||
self.module_node,
|
self.tree_node,
|
||||||
until_position,
|
until_position,
|
||||||
origin_scope=origin_scope
|
origin_scope=origin_scope
|
||||||
)
|
)
|
||||||
yield GlobalNameFilter(self, self.module_node)
|
yield GlobalNameFilter(self, self.tree_node)
|
||||||
yield DictFilter(self._sub_modules_dict())
|
yield DictFilter(self._sub_modules_dict())
|
||||||
yield DictFilter(self._module_attributes_dict())
|
yield DictFilter(self._module_attributes_dict())
|
||||||
for star_module in self.star_imports():
|
for star_module in self.star_imports():
|
||||||
@@ -445,7 +436,7 @@ class ModuleContext(use_metaclass(CachedMetaClass, context.TreeContext)):
|
|||||||
@memoize_default([])
|
@memoize_default([])
|
||||||
def star_imports(self):
|
def star_imports(self):
|
||||||
modules = []
|
modules = []
|
||||||
for i in self.module_node.imports:
|
for i in self.tree_node.imports:
|
||||||
if i.is_star_import():
|
if i.is_star_import():
|
||||||
name = i.star_import_name()
|
name = i.star_import_name()
|
||||||
new = imports.infer_import(self, name)
|
new = imports.infer_import(self, name)
|
||||||
@@ -464,7 +455,7 @@ class ModuleContext(use_metaclass(CachedMetaClass, context.TreeContext)):
|
|||||||
@property
|
@property
|
||||||
@memoize_default()
|
@memoize_default()
|
||||||
def name(self):
|
def name(self):
|
||||||
return ContextName(self, self.module_node.name)
|
return ContextName(self, self.tree_node.name)
|
||||||
|
|
||||||
def _get_init_directory(self):
|
def _get_init_directory(self):
|
||||||
"""
|
"""
|
||||||
@@ -490,10 +481,10 @@ class ModuleContext(use_metaclass(CachedMetaClass, context.TreeContext)):
|
|||||||
"""
|
"""
|
||||||
In contrast to Python's __file__ can be None.
|
In contrast to Python's __file__ can be None.
|
||||||
"""
|
"""
|
||||||
if self.module_node.path is None:
|
if self.tree_node.path is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return os.path.abspath(self.module_node.path)
|
return os.path.abspath(self.tree_node.path)
|
||||||
|
|
||||||
def py__package__(self):
|
def py__package__(self):
|
||||||
if self._get_init_directory() is None:
|
if self._get_init_directory() is None:
|
||||||
@@ -551,7 +542,7 @@ class ModuleContext(use_metaclass(CachedMetaClass, context.TreeContext)):
|
|||||||
Lists modules in the directory of this module (if this module is a
|
Lists modules in the directory of this module (if this module is a
|
||||||
package).
|
package).
|
||||||
"""
|
"""
|
||||||
path = self.module_node.path
|
path = self.tree_node.path
|
||||||
names = {}
|
names = {}
|
||||||
if path is not None and path.endswith(os.path.sep + '__init__.py'):
|
if path is not None and path.endswith(os.path.sep + '__init__.py'):
|
||||||
mods = pkgutil.iter_modules([os.path.dirname(path)])
|
mods = pkgutil.iter_modules([os.path.dirname(path)])
|
||||||
|
|||||||
@@ -167,7 +167,7 @@ def _check_module(module_context):
|
|||||||
return sys_path
|
return sys_path
|
||||||
|
|
||||||
try:
|
try:
|
||||||
possible_names = module_context.get_node().used_names['path']
|
possible_names = module_context.tree_node.used_names['path']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# module.used_names is MergedNamesDict whose getitem never throws
|
# module.used_names is MergedNamesDict whose getitem never throws
|
||||||
# keyerror, this is superfluous.
|
# keyerror, this is superfluous.
|
||||||
|
|||||||
@@ -30,3 +30,16 @@ class Y(X):
|
|||||||
|
|
||||||
#? []
|
#? []
|
||||||
myfoobar
|
myfoobar
|
||||||
|
|
||||||
|
# -----------------
|
||||||
|
# Inheritance
|
||||||
|
# -----------------
|
||||||
|
|
||||||
|
class Super():
|
||||||
|
enabled = True
|
||||||
|
if enabled:
|
||||||
|
yo_dude = 4
|
||||||
|
|
||||||
|
class Sub(Super):
|
||||||
|
#? ['yo_dude']
|
||||||
|
yo_dud
|
||||||
|
|||||||
@@ -291,16 +291,3 @@ elif 3 == flow_import.env:
|
|||||||
|
|
||||||
#? int() str()
|
#? int() str()
|
||||||
a
|
a
|
||||||
|
|
||||||
# -----------------
|
|
||||||
# Inheritance
|
|
||||||
# -----------------
|
|
||||||
|
|
||||||
class Super():
|
|
||||||
enabled = True
|
|
||||||
if enabled:
|
|
||||||
yo_dude = 4
|
|
||||||
|
|
||||||
class Sub(Super):
|
|
||||||
#? ['yo_dude']
|
|
||||||
yo_dud
|
|
||||||
|
|||||||
@@ -195,7 +195,7 @@ class IntegrationTestCase(object):
|
|||||||
user_context = get_user_scope(module_context, (self.line_nr, 0))
|
user_context = get_user_scope(module_context, (self.line_nr, 0))
|
||||||
if user_context.api_type == 'function':
|
if user_context.api_type == 'function':
|
||||||
user_context = user_context.get_function_execution()
|
user_context = user_context.get_function_execution()
|
||||||
element.parent = user_context.get_node()
|
element.parent = user_context.tree_node
|
||||||
results = evaluator.eval_element(user_context, element)
|
results = evaluator.eval_element(user_context, element)
|
||||||
if not results:
|
if not results:
|
||||||
raise Exception('Could not resolve %s on line %s'
|
raise Exception('Could not resolve %s on line %s'
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ def test_fake_loading():
|
|||||||
|
|
||||||
|
|
||||||
def test_fake_docstr():
|
def test_fake_docstr():
|
||||||
assert compiled.create(_evaluator(), next).get_node().raw_doc == next.__doc__
|
assert compiled.create(_evaluator(), next).tree_node.raw_doc == next.__doc__
|
||||||
|
|
||||||
|
|
||||||
def test_parse_function_doc_illegal_docstr():
|
def test_parse_function_doc_illegal_docstr():
|
||||||
|
|||||||
@@ -257,7 +257,7 @@ def test_string_literals():
|
|||||||
""")
|
""")
|
||||||
|
|
||||||
script = jedi.Script(dedent(source))
|
script = jedi.Script(dedent(source))
|
||||||
script._get_module().module_node.end_pos == (6, 0)
|
script._get_module().tree_node.end_pos == (6, 0)
|
||||||
assert script.completions()
|
assert script.completions()
|
||||||
|
|
||||||
|
|
||||||
@@ -275,7 +275,7 @@ def test_decorator_string_issue():
|
|||||||
|
|
||||||
s = jedi.Script(source)
|
s = jedi.Script(source)
|
||||||
assert s.completions()
|
assert s.completions()
|
||||||
assert s._get_module().module_node.get_code() == source
|
assert s._get_module().tree_node.get_code() == source
|
||||||
|
|
||||||
|
|
||||||
def test_round_trip():
|
def test_round_trip():
|
||||||
|
|||||||
@@ -175,7 +175,7 @@ class TestRegression(TestCase):
|
|||||||
completions = Script('').completions()
|
completions = Script('').completions()
|
||||||
c = get_str_completion(completions)
|
c = get_str_completion(completions)
|
||||||
str_context, = c._name.infer()
|
str_context, = c._name.infer()
|
||||||
n = len(str_context.classdef.children[-1].children)
|
n = len(str_context.tree_node.children[-1].children)
|
||||||
if i == 0:
|
if i == 0:
|
||||||
limit = n
|
limit = n
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user