Skip to content

fix: do not emit stray newline tokens before a trailing comment - #362

Open
Sanjays2402 wants to merge 1 commit into
PyCQA:masterfrom
Sanjays2402:fix/inline-comment-after-docstring
Open

fix: do not emit stray newline tokens before a trailing comment#362
Sanjays2402 wants to merge 1 commit into
PyCQA:masterfrom
Sanjays2402:fix/inline-comment-after-docstring

Conversation

@Sanjays2402

Copy link
Copy Markdown

Closes #347

When a docstring is followed by an inline comment, _do_add_formatted_docstring correctly 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, so tokenize.untokenize() raises ValueError: 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_docstring fails on master with the reported ValueError and passes with the fix; the rest of the suite is unchanged (500 -> 501 passed).

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ValueError in tokenize.untokenize with one-line docstring followed by inline noqa on Python 3.14

1 participant