Add 'yield' inside async function' for Python 3.5.

This commit is contained in:
Dave Halter
2017-08-05 22:33:11 +02:00
parent 94570acef7
commit 63e30843dc
3 changed files with 19 additions and 8 deletions

View File

@@ -603,10 +603,14 @@ class ErrorFinder(Normalizer):
elif leaf.value in ('yield', 'return'): elif leaf.value in ('yield', 'return'):
if self._context.node.type != 'funcdef': if self._context.node.type != 'funcdef':
self._add_syntax_error("'%s' outside function" % leaf.value, leaf.parent) self._add_syntax_error("'%s' outside function" % leaf.value, leaf.parent)
elif self._context.is_async_funcdef() and leaf.value == 'return' \ elif self._context.is_async_funcdef() \
and leaf.parent.type == 'return_stmt' \
and any(self._context.node.iter_yield_exprs()): and any(self._context.node.iter_yield_exprs()):
self._add_syntax_error("'return' with value in async generator", leaf.parent) if leaf.value == 'return' and leaf.parent.type == 'return_stmt':
self._add_syntax_error("'return' with value in async generator", leaf.parent)
elif leaf.value == 'yield' \
and leaf.get_next_leaf() != 'from' \
and self._version == (3, 5):
self._add_syntax_error("'yield' inside async function", leaf.parent)
elif leaf.value == 'await': elif leaf.value == 'await':
if not self._context.is_async_funcdef(): if not self._context.is_async_funcdef():
self._add_syntax_error("'await' outside async function", leaf.parent) self._add_syntax_error("'await' outside async function", leaf.parent)

View File

@@ -4,7 +4,6 @@ Mostly allowed syntax in Python 3.5.
async def foo(): async def foo():
yield 1
await bar() await bar()
#: E901 #: E901
yield from [] yield from []
@@ -16,7 +15,6 @@ async def foo():
# With decorator it's a different statement. # With decorator it's a different statement.
@bla @bla
async def foo(): async def foo():
yield 1
await bar() await bar()
#: E901 #: E901
yield from [] yield from []

View File

@@ -119,6 +119,7 @@ FAILING_EXAMPLES = [
r'b"\x"', r'b"\x"',
r'b"\"', r'b"\"',
'*a, *b = 3, 3', '*a, *b = 3, 3',
'async def foo(): yield from []',
# Parser/tokenize.c # Parser/tokenize.c
r'"""', r'"""',
@@ -264,6 +265,17 @@ if sys.version_info >= (2, 7):
# versions. Just skip it for 2.6. # versions. Just skip it for 2.6.
FAILING_EXAMPLES.append('[a, 1] += 3') FAILING_EXAMPLES.append('[a, 1] += 3')
if sys.version_info[:2] == (3, 5):
FAILING_EXAMPLES += [
'async def foo():\n yield x',
'async def foo():\n yield x',
]
else:
FAILING_EXAMPLES += [
'async def foo():\n yield x\n return 1',
'async def foo():\n yield x\n return 1',
]
def _get_error_list(code, version=None): def _get_error_list(code, version=None):
grammar = parso.load_grammar(version=version) grammar = parso.load_grammar(version=version)
@@ -378,9 +390,6 @@ def test_default_except_error_postition():
# SyntaxError # SyntaxError
('async def bla():\n def x(): await bla()', '3.5'), ('async def bla():\n def x(): await bla()', '3.5'),
('yield from []', '3.5'), ('yield from []', '3.5'),
('async def foo(): yield from []', '3.5'),
('async def foo():\n yield x\n return 1', '3.6'),
('async def foo():\n yield x\n return 1', '3.6'),
('*a = 3', '3.5'), ('*a = 3', '3.5'),
('del *a, b', '3.5'), ('del *a, b', '3.5'),
('def x(*): pass', '3.5'), ('def x(*): pass', '3.5'),