1
0
forked from VimPlug/jedi

Make the some names more concise in the parser tree.

This commit is contained in:
Dave Halter
2017-04-10 09:44:08 +02:00
parent 769cc80d6b
commit 218e715553
5 changed files with 21 additions and 16 deletions

View File

@@ -81,6 +81,7 @@ from jedi.evaluate import pep0484
from jedi.evaluate.filters import TreeNameDefinition, ParamName from jedi.evaluate.filters import TreeNameDefinition, ParamName
from jedi.evaluate.instance import AnonymousInstance, BoundMethod from jedi.evaluate.instance import AnonymousInstance, BoundMethod
from jedi.evaluate.context import ContextualizedName, ContextualizedNode from jedi.evaluate.context import ContextualizedName, ContextualizedNode
from jedi import parser_utils
class Evaluator(object): class Evaluator(object):
@@ -165,11 +166,11 @@ class Evaluator(object):
for_stmt = tree.search_ancestor(stmt, 'for_stmt') for_stmt = tree.search_ancestor(stmt, 'for_stmt')
if for_stmt is not None and for_stmt.type == 'for_stmt' and types \ if for_stmt is not None and for_stmt.type == 'for_stmt' and types \
and for_stmt.defines_one_name(): and parser_utils.for_stmt_defines_one_name(for_stmt):
# Iterate through result and add the values, that's possible # Iterate through result and add the values, that's possible
# only in for loops without clutter, because they are # only in for loops without clutter, because they are
# predictable. Also only do it, if the variable is not a tuple. # predictable. Also only do it, if the variable is not a tuple.
node = for_stmt.get_input_node() node = for_stmt.get_testlist()
cn = ContextualizedNode(context, node) cn = ContextualizedNode(context, node)
ordered = list(iterable.py__iter__(self, cn.infer(), cn)) ordered = list(iterable.py__iter__(self, cn.infer(), cn))

View File

@@ -153,7 +153,7 @@ def _check_for_exception_catch(node_context, jedi_name, exception, payload=None)
and not (branch_type.start_pos < jedi_name.start_pos <= suite.end_pos): and not (branch_type.start_pos < jedi_name.start_pos <= suite.end_pos):
return False return False
for node in obj.except_clauses(): for node in obj.get_except_clauses():
if node is None: if node is None:
return True # An exception block that catches everything. return True # An exception block that catches everything.
else: else:

View File

@@ -62,6 +62,7 @@ from jedi.evaluate.filters import ParserTreeFilter, FunctionExecutionFilter, \
from jedi.evaluate.dynamic import search_params from jedi.evaluate.dynamic import search_params
from jedi.evaluate import context from jedi.evaluate import context
from jedi.evaluate.context import ContextualizedNode from jedi.evaluate.context import ContextualizedNode
from jedi import parser_utils
def apply_py__get__(context, base_context): def apply_py__get__(context, base_context):
@@ -353,7 +354,7 @@ class FunctionExecutionContext(context.TreeContext):
if parent.type == 'suite': if parent.type == 'suite':
parent = parent.parent parent = parent.parent
if for_stmt.type == 'for_stmt' and parent == self.tree_node \ if for_stmt.type == 'for_stmt' and parent == self.tree_node \
and for_stmt.defines_one_name(): # Simplicity for now. and parser_utils.for_stmt_defines_one_name(for_stmt): # 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:
@@ -375,7 +376,7 @@ class FunctionExecutionContext(context.TreeContext):
for result in self._eval_yield(yield_): for result in self._eval_yield(yield_):
yield result yield result
else: else:
input_node = for_stmt.get_input_node() input_node = for_stmt.get_testlist()
cn = ContextualizedNode(self, input_node) cn = ContextualizedNode(self, input_node)
ordered = iterable.py__iter__(evaluator, cn.infer(), cn) ordered = iterable.py__iter__(evaluator, cn.infer(), cn)
ordered = list(ordered) ordered = list(ordered)

View File

@@ -745,32 +745,24 @@ class ForStmt(Flow):
type = 'for_stmt' type = 'for_stmt'
__slots__ = () __slots__ = ()
def get_input_node(self): def get_testlist(self):
""" """
Returns the input node ``y`` from: ``for x in y:``. Returns the input node ``y`` from: ``for x in y:``.
""" """
return self.children[3] return self.children[3]
def defines_one_name(self):
"""
Returns True if only one name is returned: ``for x in y``.
Returns False if the for loop is more complicated: ``for x, z in y``.
:returns: bool
"""
return self.children[1].type == 'name'
class TryStmt(Flow): class TryStmt(Flow):
type = 'try_stmt' type = 'try_stmt'
__slots__ = () __slots__ = ()
def except_clauses(self): def get_except_clauses(self):
""" """
Returns the ``test`` nodes found in ``except_clause`` nodes. Returns the ``test`` nodes found in ``except_clause`` nodes.
Returns ``[None]`` for except clauses without an exception given. Returns ``[None]`` for except clauses without an exception given.
""" """
for node in self.children: for node in self.children:
# TODO this is not correct. We're not returning an except clause.
if node.type == 'except_clause': if node.type == 'except_clause':
yield node.children[1] yield node.children[1]
elif node == 'except': elif node == 'except':
@@ -790,6 +782,7 @@ class WithStmt(Flow):
return names return names
def get_context_manager_from_name(self, name): def get_context_manager_from_name(self, name):
# TODO Replace context_manager with test?
node = name.parent node = name.parent
if node.type != 'with_item': if node.type != 'with_item':
raise ValueError('The name is not actually part of a with statement.') raise ValueError('The name is not actually part of a with statement.')

View File

@@ -58,3 +58,13 @@ def get_comp_fors(comp_for):
elif not last.type == 'comp_if': elif not last.type == 'comp_if':
break break
last = last.children[-1] last = last.children[-1]
def for_stmt_defines_one_name(for_stmt):
"""
Returns True if only one name is returned: ``for x in y``.
Returns False if the for loop is more complicated: ``for x, z in y``.
:returns: bool
"""
return for_stmt.children[1].type == 'name'