From fc1c16b966d0248be240f3cf206e99e4d11cacb7 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Thu, 20 Jul 2017 09:44:10 +0200 Subject: [PATCH] await checks. --- parso/python/normalizer.py | 4 ++++ test/normalizer_issue_files/allowed_syntax.py | 2 ++ test/test_python_errors.py | 24 ++++++++++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/parso/python/normalizer.py b/parso/python/normalizer.py index 3a9ceac..b7dbe35 100644 --- a/parso/python/normalizer.py +++ b/parso/python/normalizer.py @@ -128,6 +128,10 @@ class ErrorFinder(Normalizer): elif leaf.value == 'return': if self._context.node.type != 'funcdef': self._add_syntax_error("'return' outside function", leaf) + elif leaf.value == 'await': + if self._context.node.type != 'funcdef' \ + or self._context.node.parent.type != 'async_funcdef': + self._add_syntax_error("'await' outside async function", leaf) return '' def _add_indentation_error(self, message, spacing): diff --git a/test/normalizer_issue_files/allowed_syntax.py b/test/normalizer_issue_files/allowed_syntax.py index ea43767..fcc19b1 100644 --- a/test/normalizer_issue_files/allowed_syntax.py +++ b/test/normalizer_issue_files/allowed_syntax.py @@ -3,6 +3,8 @@ Some syntax errors are a bit complicated and need exact checking. Here we gather some of the potentially dangerous ones. """ +from __future__ import division + for x in [1]: try: continue # Only the other continue and pass is an error. diff --git a/test/test_python_errors.py b/test/test_python_errors.py index 60fd344..b594448 100644 --- a/test/test_python_errors.py +++ b/test/test_python_errors.py @@ -1,9 +1,10 @@ """ Testing if parso finds syntax errors and indentation errors. """ +import sys +from textwrap import dedent import pytest -from textwrap import dedent import parso from parso.python.normalizer import ErrorFinderConfig @@ -90,6 +91,27 @@ def test_python_exception_matches(code): assert wanted == error.message +@pytest.mark.parametrize( + ('code', 'version'), [ + # SyntaxError + ('async def bla():\n def x(): await bla()', '3.5'), + + ] +) +def test_python_exception_matches_version(code, version): + if '.'.join(str(v) for v in sys.version_info[:2]) != version: + pytest.skip() + + error, = _get_error_list(code) + try: + compile(code, '', 'exec') + except (SyntaxError, IndentationError) as e: + wanted = e.__class__.__name__ + ': ' + e.msg + else: + assert False, "The piece of code should raise an exception." + assert wanted == error.message + + def test_statically_nested_blocks(): def indent(code): lines = code.splitlines(True)