diff --git a/AUTHORS.txt b/AUTHORS.txt index 451918f..7c6fdcd 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -56,4 +56,6 @@ Batuhan Taskaya (@isidentical) Jocelyn Boullier (@Kazy) +salpenr (@salapenar-max) + Note: (@user) means a github user name. diff --git a/parso/python/pep8.py b/parso/python/pep8.py index 47db445..1d9c833 100644 --- a/parso/python/pep8.py +++ b/parso/python/pep8.py @@ -444,7 +444,10 @@ def _visit_part(self, part, spacing, leaf): should_be_indentation = n.indentation self._last_indentation_tos = n - if n == node: + # A backslash in the same prefix can introduce a new + # node below the previous indentation stack. In that + # case, stop at the root instead of walking past it. + if n == node or n.parent is None: break n = n.parent diff --git a/test/test_pep8.py b/test/test_pep8.py index 06cffb4..4fb6142 100644 --- a/test/test_pep8.py +++ b/test/test_pep8.py @@ -1,4 +1,5 @@ import parso +import pytest def issues(code): @@ -40,3 +41,14 @@ def test_shebang(): assert not issues('#!\n') assert not issues('#!/foo\n') assert not issues('#! python\n') + + +@pytest.mark.parametrize('newline', ['\n', '\r\n', '\r']) +@pytest.mark.parametrize('backslashes', [1, 2]) +@pytest.mark.parametrize('comment', ['#', '# comment']) +def test_comment_after_backslash(newline, backslashes, comment): + prefix = ('\\' + newline) * backslashes + found = issues(prefix + comment) + assert [(issue.code, issue.start_pos) for issue in found] == [ + (292, (backslashes + 1, len(comment))), + ]