1.7.8 crashes with a tokenize ValueError on a multi-line f-string inside parentheses
Maintainers usually reply within 1 day
@adityaanikam is already working on this.
Since Aug 10, 2026.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 74/100
Research direction
Start with classify.is_f_string and its consumer _get_unmatched_start_end_indices in format.py:499-535, then run the supplied multi-line f-string reproducer on Python 3.11. Trace how the pre-3.12 f-string check changes the token start row; done means the reproducer formats without ValueError and multi-file runs do not abort on this input.
Written by the indexing model from the issue text.
Description
docformatter 1.7.8 aborts with a ValueError out of tokenize.untokenize when a file contains a multi-line f-string inside a parenthesized expression. 1.7.7 formats the same file cleanly.
Reproducer
def build(x):
return (
f"""a
{x}""",
)
$ docformatter --in-place t.py
File ".../docformatter/format.py", line 876, in _do_format_code
_code = tokenize.untokenize(self.new_tokens)
File "/usr/lib/python3.11/tokenize.py", line 216, in untokenize
self.add_whitespace(start)
File "/usr/lib/python3.11/tokenize.py", line 177, in add_whitespace
raise ValueError("start ({},{}) precedes previous end ({},{})"
ValueError: start (2,8) precedes previous end (3,4)
$ echo $?
1
No config file and no flags are needed. Two things narrow it:
- Remove the
fprefix and the run exits 3 (would reformat) with no error, so the f-string prefix is the trigger. - Assign the same f-string to a name instead of putting it in a tuple and it does not crash, so it has to sit inside parentheses.
Affected versions
| docformatter | Python 3.11 | Python 3.12 | Python 3.13 |
|---|---|---|---|
| 1.7.7 | ok | ok | ok |
| 1.7.8 | ValueError | ok | ok |
| master | ValueError | ok | ok |
Root cause
Bisected to d9ace1f ("fix: resolve several empty line regressions", #330). Its parent 7798699 is clean.
That commit adds a Python < 3.12 workaround to classify.is_f_string:
elif any(
[
token.string.startswith('f"""'),
prev_token.string.startswith('f"""'),
token.string.startswith("f'''"),
prev_token.string.startswith("f'''"),
]
):
return True
The PY312 branch above it tests tokenize.FSTRING_MIDDLE, which only appears inside an f-string. The < 3.12 branch instead matches any token whose text begins with f""", including an ordinary triple-quoted f-string that is not a docstring — and, via prev_token, the token that follows one.
The single consumer is _get_unmatched_start_end_indices (format.py:499), where a true verdict sets _start_row = prev_token.end[0] (format.py:526-535). That rewrites the f-string's start row back to where the previous token ended, so untokenize sees a token starting above one it has already emitted and raises.
Impact
With several files on one command line, the run aborts at the first offending file and exits 1, leaving every file after it unprocessed. On a 1,255-file tree this stopped at file 318 with no indication that the remainder was skipped.
Environment
CPython 3.11.15, Linux x86-64, docformatter==1.7.8 from PyPI.
- Dominant language
- Python
- Stars
- 600
- Forks
- 107
- Avg merge
- 22h 15m
- Merged PRs (30d)
- 12
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from PyCQA/docformatter
-
`--docstring-length` removes blank line after docstrings it doesn't format (ping-pong with ruff format / Black)Possibly taken @RClaudiuM claimed this 1 day ago. Openfresh
Difficulty 3/5 1-2 days Newbie friendliness 68/100
PyCQA/docformatter#420 ·
Maintainers usually reply within 1 day
-
fresh
Difficulty 3/5 1-2 days Newbie friendliness 66/100
PyCQA/docformatter#414 · 1 reaction ·
Maintainers usually reply within 1 day
-
docformatter fix does not consider parameters for added newlinesMay be free again @AtharvRG claimed this 25 days ago, and no pull request is open. Openfresh
Difficulty 3/5 1-2 days Newbie friendliness 68/100
PyCQA/docformatter#408 · 1 comment ·
Maintainers usually reply within 1 day
-
BUG: Inconsistent behavior between `docformatter` and `black`May be free again @AtharvRG claimed this 25 days ago, and no pull request is open. Openfresh
Difficulty 3/5 1-2 days Newbie friendliness 58/100
PyCQA/docformatter#399 · 1 comment ·
Maintainers usually reply within 1 day
-
1.7.8 rewrites the contents of a non-docstring triple-quoted string, silently changing its valueOpenC: convention P: bug U: high
Difficulty 3/5 1-2 days Newbie friendliness 72/100
PyCQA/docformatter#366 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
All issues in PyCQA/docformatter
Similar issues
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 85/100
NatLabRockies/openstudio-mcp#286 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
intersystems/ipm#1338 ·
Maintainers usually reply within 1 day
-
separate_packages splits imports from the same explicitly configured packagePossibly taken @rahul05ranjan claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
The v0-through-v1 path derives ShutdownDuration its own way, silently overriding a user's definitionOpenv1
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day