fix: prevent ValueError crash on blank lines with trailing whitespace (#355)#360
Merged
weibullguy merged 2 commits intoJul 14, 2026
Conversation
…uations A blank line that contains trailing whitespace (e.g. " \n") produces an NL token whose `.line` is not exactly "\n" or "\r\n". The guard in `is_newline_continuation` only excluded the exact-blank forms, so such a line was misclassified as a continuation of the preceding line. This caused `_do_update_token_indices` to rewrite the token's start row back to the previous token's end row, yielding a start position that precedes the previous token's end. `tokenize.untokenize` then raised `ValueError: start (r,c) precedes previous end (r,c)` and docformatter crashed (regression from the stdlib tokenize migration in PyCQA#325, v1.7.8). Classify any whitespace-only line as blank by testing `token.line.strip()` instead of comparing the raw line against {"\n", "\r\n"}. Genuine continuations (whose stripped line is non-empty) are unaffected. Fixes PyCQA#355.
cnsfeir-builder
pushed a commit
to cristobal-sfeir-inc/cumplo-common
that referenced
this pull request
Jul 19, 2026
docformatter 1.7.8 crashes on all Python 3.13.x via a tokenize.untokenize regression (PyCQA/docformatter#360 — fix merged 2026-07-14, not yet released). Broadening the python constraint to ^3.12 and pinning CI to 3.12 is the approved fallback. Restore python = "^3.13" and python-version: "3.13" once 1.7.9 ships. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
4 tasks
cnsfeir-builder
pushed a commit
to cristobal-sfeir-inc/cumplo-spotter
that referenced
this pull request
Jul 19, 2026
docformatter 1.7.8 crashes on all Python 3.13.x (tokenize.untokenize regression; upstream fix not yet released as of 2026-07-19). Restore "3.13" once docformatter 1.7.9 ships (PyCQA/docformatter#360). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
cnsfeir-builder
pushed a commit
to cristobal-sfeir-inc/cumplo-common
that referenced
this pull request
Jul 19, 2026
docformatter --recursive . scanned .venv/** when Poetry created an in-project venv (virtualenvs-in-project: true). Third-party packages in .venv triggered a tokenize.untokenize crash (PyCQA/docformatter#360). Adding exclude = [".venv"] to [tool.docformatter] is the precise fix; reverts the unnecessary Python 3.12 fallback that was applied before the root cause was identified. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
cnsfeir-builder
pushed a commit
to cristobal-sfeir-inc/cumplo-spotter
that referenced
this pull request
Jul 19, 2026
docformatter 1.7.8 crashes on all Python 3.13.x (tokenize.untokenize regression; upstream fix not yet released as of 2026-07-19). Restore "3.13" once docformatter 1.7.9 ships (PyCQA/docformatter#360). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Summary
Fixes #355. Running
docformatteron a file that contains a blank line with trailing whitespace crashes in v1.7.8 (a regression introduced by the stdlibtokenizemigration in #325):Root cause
A blank line that carries trailing whitespace (e.g.
" \n") tokenizes to anNLtoken whose.lineattribute is the whole physical line," \n"— not the bare"\n"/"\r\n".is_newline_continuationguarded against blank lines withtoken.line not in {"\n", "\r\n"}, which only recognizes the exact-blank forms. A whitespace-padded blank line slipped past that guard (andtoken.line.strip()is"", which is trivially "in" any string), so the blank line was misclassified as a continuation of the preceding line.That misclassification made
_do_update_token_indicesrewrite the token's start row back to the previous token's end row, producing a start position that precedes the previous token's end.tokenize.untokenizethen rejects the stream withValueError: start ... precedes previous end ....Fix
Classify any whitespace-only line as blank by testing the stripped line for content, rather than comparing the raw line against a fixed set of blank strings:
Genuine continuation lines (whose stripped content is non-empty) are unaffected — this only stops treating blank lines, with or without trailing whitespace, as continuations.
Tests / Verification
issue_355totests/formatter/test_do_format_code.pyand its data indo_format_code.toml. The source encodes the four trailing spaces on the blank line with explicit\nescapes so they cannot be silently stripped by editors or formatters.ValueErrorfrom the issue, and passes with the fix applied.540 passed, 3 skipped(the end-to-end subprocess tests requireVIRTUAL_ENVto be set in the environment).black,isort, andruffall pass on the changed source.Disclosure: prepared with AI assistance; reviewed and verified locally.