diff --git a/AUTHORS.txt b/AUTHORS.txt index 9737530..cf83eaf 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -55,4 +55,6 @@ Batuhan Taskaya (@isidentical) Jocelyn Boullier (@Kazy) +eunwoo song (@be-student) + Note: (@user) means a github user name. diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a4811fb..c9a043c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,6 +6,8 @@ Changelog Unreleased ++++++++++ +- Fix a PEP8 normalizer crash on comments following a line continuation. + 0.8.7 (2026-05-02) ++++++++++++++++++ diff --git a/parso/python/pep8.py b/parso/python/pep8.py index 47db445..c851ffa 100644 --- a/parso/python/pep8.py +++ b/parso/python/pep8.py @@ -444,7 +444,9 @@ def _visit_part(self, part, spacing, leaf): should_be_indentation = n.indentation self._last_indentation_tos = n - if n == node: + # A continuation in this prefix can make node newer + # than the saved stack. Always stop at its root. + 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..a71b6a9 100644 --- a/test/test_pep8.py +++ b/test/test_pep8.py @@ -1,3 +1,5 @@ +import pytest + import parso @@ -40,3 +42,12 @@ 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('prefix', ['', 'x = 1\n', 'if True:\n pass\n']) +@pytest.mark.parametrize('comment', ['#', '# comment', '# comment\n']) +def test_comment_after_line_continuation(newline, prefix, comment): + code = prefix + '\\' + newline + comment + found = issues(code) + assert (292 in [issue.code for issue in found]) == (not code.endswith('\n'))