fix: do not emit stray newline tokens before a trailing comment - #362
Open
Sanjays2402 wants to merge 1 commit into
Open
fix: do not emit stray newline tokens before a trailing comment#362Sanjays2402 wants to merge 1 commit into
Sanjays2402 wants to merge 1 commit into
Conversation
When a docstring is followed by an inline comment, _do_add_formatted_docstring
skipped the NEWLINE token but still called _do_add_blank_lines, which appended
newline tokens positioned after the docstring and before the comment. The
comment token that follows then starts at a position earlier than the injected
token's end, so tokenize.untokenize() raised
ValueError: start (2,26) precedes previous end (3,0)
on a file as simple as a one-line module docstring with a trailing noqa
comment. The blank-line insertion is now skipped along with the NEWLINE token
when a comment follows.
Guarding only that call exposed a second crash in the same path: the
subsequent self.new_tokens[-2] lookup in _do_rewrite_docstring_blocks assumed
at least two tokens had been appended, which is no longer true for a module
docstring on line one. That index is now length-checked.
Adds test_do_format_code_inline_comment_after_docstring, which fails on master
with the reported ValueError and passes with the fix.
Closes PyCQA#347
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #347
When a docstring is followed by an inline comment,
_do_add_formatted_docstringcorrectly skipped the NEWLINE token but still called_do_add_blank_lines, appending newline tokens positioned between the docstring and the comment. The comment token then starts before the injected token's end, sotokenize.untokenize()raisesValueError: start (2,26) precedes previous end (3,0)on a file as simple as"""This is a comment.""" # noqa: D415.Guarding that call exposed a second crash in the same path: the following
self.new_tokens[-2]lookup assumed at least two tokens had been appended, which is no longer true for a module docstring on line one. That index is now length-checked.New
test_do_format_code_inline_comment_after_docstringfails on master with the reported ValueError and passes with the fix; the rest of the suite is unchanged (500 -> 501 passed).