Add and fix some async/await issues.

This commit is contained in:
Dave Halter
2017-07-20 17:27:51 +02:00
parent 9296fe3def
commit fa30872d26
3 changed files with 31 additions and 3 deletions

View File

@@ -3,6 +3,7 @@ from contextlib import contextmanager
from parso.normalizer import Normalizer, NormalizerConfig, Issue
_BLOCK_STMTS = ('if_stmt', 'while_stmt', 'for_stmt', 'try_stmt', 'with_stmt')
_ASYNC_FUNC_TYPES = 'async_funcdef', 'async_stmt'
# This is the maximal block size given by python.
_MAX_BLOCK_SIZE = 20
@@ -22,6 +23,7 @@ class Context(object):
def __init__(self, node, parent_context=None):
self.node = node
self.blocks = []
self.parent_context = parent_context
@contextmanager
def add_block(self, node):
@@ -130,8 +132,13 @@ class ErrorFinder(Normalizer):
self._add_syntax_error("'%s' outside function" % leaf.value, leaf)
elif leaf.value == 'await':
if self._context.node.type != 'funcdef' \
or self._context.node.parent.type != 'async_funcdef':
or self._context.node.parent.type not in _ASYNC_FUNC_TYPES:
self._add_syntax_error("'await' outside async function", leaf)
elif leaf.value == 'from' and leaf.parent.type == 'yield_arg' \
and self._context.node.type == 'funcdef' \
and self._context.node.parent.type in _ASYNC_FUNC_TYPES:
yield_ = leaf.parent.parent
self._add_syntax_error("'yield from' inside async function", yield_)
return ''
def _add_indentation_error(self, message, spacing):