1
0
forked from VimPlug/jedi

Replace Scope.subscopes with iter_funcdefs and iter_classdefs.

This commit is contained in:
Dave Halter
2017-04-30 01:35:59 +02:00
parent 3e05061f3b
commit b4039872bd
10 changed files with 32 additions and 34 deletions

View File

@@ -7,6 +7,7 @@ mixing in Python code, the autocompletion should work much better for builtins.
import os
import inspect
import types
from itertools import chain
from jedi._compatibility import is_py3, builtins, unicode, is_py34
from jedi.parser.python import parse
@@ -74,7 +75,7 @@ def _load_faked_module(module):
def _search_scope(scope, obj_name):
for s in scope.subscopes:
for s in chain(scope.iter_classdefs(), scope.iter_funcdefs()):
if s.name.value == obj_name:
return s

View File

@@ -134,7 +134,7 @@ def _evaluate_for_statement_string(module_context, string):
# don't need to conform with the current grammar.
module = parse(code.format(indent_block(string)))
try:
funcdef = module.subscopes[0]
funcdef = next(module.iter_funcdefs())
# First pick suite, then simple_stmt and then the node,
# which is also not the last item, because there's a newline.
stmt = funcdef.children[-1].children[-1].children[-2]

View File

@@ -275,7 +275,7 @@ def get_global_filters(evaluator, context, until_position, origin_scope):
... y = None
... '''))
>>> module_node = script._get_module_node()
>>> scope = module_node.subscopes[0]
>>> scope = next(module_node.iter_funcdefs())
>>> scope
<Function: func@3-5>
>>> context = script._get_module().create_context(scope)

View File

@@ -270,7 +270,7 @@ def collections_namedtuple(evaluator, obj, arguments):
)
# Parse source
generated_class = parse(source, grammar=evaluator.grammar).subscopes[0]
generated_class = next(parse(source, grammar=evaluator.grammar).iter_classdefs())
return set([er.ClassContext(evaluator, generated_class, evaluator.BUILTINS)])

View File

@@ -23,8 +23,6 @@ Any subclasses of :class:`Scope`, including :class:`Module` has an attribute
>>> module.imports
[<ImportName: import os@1,0>]
See also :attr:`Scope.subscopes` and :attr:`Scope.statements`.
"""
from itertools import chain
@@ -234,12 +232,7 @@ class Scope(PythonBaseNode, DocstringMixin):
"""
Super class for the parser tree, which represents the state of a python
text file.
A Scope manages and owns its subscopes, which are classes and functions, as
well as variables and imports. It is used to access the structure of python
files.
:param start_pos: The position (line and column) of the scope.
:type start_pos: tuple(int, int)
A Scope is either a function, class or lambda.
"""
__slots__ = ()
@@ -250,26 +243,27 @@ class Scope(PythonBaseNode, DocstringMixin):
def returns(self):
# Needed here for fast_parser, because the fast_parser splits and
# returns will be in "normal" modules.
return self._search_in_scope(ReturnStmt)
return list(self._search_in_scope(ReturnStmt))
@property
def subscopes(self):
return self._search_in_scope(Scope)
def iter_funcdefs(self):
return self._search_in_scope(Function)
def iter_classdefs(self):
return self._search_in_scope(Class)
@property
def imports(self):
return self._search_in_scope(Import)
return list(self._search_in_scope(Import))
def _search_in_scope(self, typ):
def scan(children):
elements = []
for element in children:
if isinstance(element, typ):
elements.append(element)
yield element
if element.type in ('suite', 'simple_stmt', 'decorated') \
or isinstance(element, Flow):
elements += scan(element.children)
return elements
for e in scan(element.children):
yield e
return scan(self.children)
@@ -473,7 +467,7 @@ class Function(ClassOrFunc):
@property
def yields(self):
# TODO This is incorrect, yields are also possible in a statement.
return self._search_in_scope(YieldExpr)
return list(self._search_in_scope(YieldExpr))
def is_generator(self):
return bool(self.yields)

View File

@@ -164,10 +164,10 @@ def get_doc_with_call_signature(scope_node):
"""
call_signature = None
if scope_node.type == 'classdef':
for sub in scope_node.subscopes:
if sub.name.value == '__init__':
for funcdef in scope_node.iter_funcdefs():
if funcdef.name.value == '__init__':
call_signature = \
get_call_signature(sub, call_string=scope_node.name.value)
get_call_signature(funcdef, call_string=scope_node.name.value)
elif scope_node.type in ('funcdef', 'lambdef'):
call_signature = get_call_signature(scope_node)