diff --git a/AUTHORS.txt b/AUTHORS.txt index 451918f..7dc7874 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -55,5 +55,6 @@ Tim Gates (@timgates42) Batuhan Taskaya (@isidentical) Jocelyn Boullier (@Kazy) +Rupayon Haldar (@rupayon123) Note: (@user) means a github user name. diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a4811fb..a82e996 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,6 +6,9 @@ Changelog Unreleased ++++++++++ +- Avoid a normalizer crash for a standalone line continuation followed by a + comment, and keep indentation checks on the active continuation stack. + 0.8.7 (2026-05-02) ++++++++++++++++++ diff --git a/parso/python/pep8.py b/parso/python/pep8.py index 47db445..dd7d017 100644 --- a/parso/python/pep8.py +++ b/parso/python/pep8.py @@ -430,7 +430,7 @@ def _visit_part(self, part, spacing, leaf): indentation = spacing.value if node.type == IndentationTypes.BACKSLASH \ and self._previous_part.type == 'newline': - self._indentation_tos = self._indentation_tos.parent + self._indentation_tos = node = node.parent if not self._check_tabs_spaces(spacing): should_be_indentation = node.indentation @@ -444,7 +444,8 @@ def _visit_part(self, part, spacing, leaf): should_be_indentation = n.indentation self._last_indentation_tos = n - if n == node: + # Prefix continuations may not be in the previous stack. + 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..eb3990f 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,11 @@ def test_shebang(): assert not issues('#!\n') assert not issues('#!/foo\n') assert not issues('#! python\n') + + +@pytest.mark.parametrize("ending, expected_codes", [("", [292]), ("\n", [])]) +def test_comment_after_standalone_backslash(each_version, ending, expected_codes): + grammar = parso.load_grammar(version=each_version) + module = grammar.parse("\\\n#" + ending) + found = grammar._get_normalizer_issues(module) + assert [issue.code for issue in found] == expected_codes