diff --git a/parso/python/normalizer.py b/parso/python/normalizer.py index 43c736a..af0f006 100644 --- a/parso/python/normalizer.py +++ b/parso/python/normalizer.py @@ -73,7 +73,11 @@ class ErrorFinder(Normalizer): # Therefore in case of an error we also have to check for this. print(repr(leaf.prefix), leaf.get_next_leaf()) spacing = list(leaf.get_next_leaf()._split_prefix())[-1] - self._add_indentation_error("Indentation Error", spacing) + if leaf.original_type == 'indent': + message = 'unexpected indent' + else: + message = 'unindent does not match any outer indentation level' + self._add_indentation_error("IndentationError: " + message, spacing) else: self._add_syntax_error("Syntax Error", leaf) diff --git a/test/test_python_errors.py b/test/test_python_errors.py index 1ce2b3f..533b670 100644 --- a/test/test_python_errors.py +++ b/test/test_python_errors.py @@ -3,6 +3,7 @@ Testing if parso finds syntax errors and indentation errors. """ import pytest +import ast import parso from parso.python.normalizer import ErrorFinderConfig @@ -46,7 +47,26 @@ def test_syntax_errors(code, positions): ('code', 'positions'), [ (' 1', [(1, 0)]), ('def x():\n 1\n 2', [(3, 0)]), + ('def x():\n 1\n 2', [(3, 0)]), ] ) def test_indentation_errors(code, positions): assert_comparison(code, 903, positions) + + +@pytest.mark.parametrize( + 'code', [ + ' foo', + 'def x():\n 1\n 2', + 'def x():\n 1\n 2', + ] +) +def test_python_exception_matches(code): + error, = _get_error_list(code) + try: + ast.parse(code) + 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