Formatting a dictionary with a long string and type ignore fails on --unstable
Describe the bug
Black crashes with INTERNAL ERROR when trying to format a combination of a dictionary, long string, and a type ignore comment.
To Reproduce
Take this code:
my_dict = {
"key": (
"A very very very very very very very very very very very very very very very very long string literal"
) # type: ignore
}
And run it with these arguments:
$ black example.py --unstable
The resulting error is:
error: cannot format example.py: INTERNAL ERROR: Black 0.1.dev2313+gc835e9db6 on Python (CPython) 3.14.3 produced code that is not equivalent to the source. Please report a bug on https://github.com/psf/black/issues. This diff might be helpful: /tmp/blk_vleux34f.log
Oh no! š„ š š„
1 file would fail to reformat.
Expected behavior
Code should get formatted to something like:
my_dict = {
"key": (
"A very very very very very very very very very very very very very very very"
" very long string literal"
) # type: ignore
}
Environment
- Reproduced both with Black
26.5.1and the latest code on themainbranch
Additional context
Here's the content of the /tmp/blk_vleux34f.log log file:
--- src
+++ dst
@@ -28,8 +28,6 @@
'A very very very very very very very very very very very very very very very very long string literal', # str
) # /Constant
) # /Dict
) # /Assign
type_ignores=
- TypeIgnore(
- ) # /TypeIgnore
) # /Module
\ No newline at end of file
The crash happens because there is a bug that makes the # type: ignore comment just disappear.
I prepared a fix for this in https://github.com/psf/black/pull/5329.
Hi, Iād like to reproduce this unstable-formatting case with a long string and ype: ignore, then add a regression fixture and make the smallest formatter change that fixes it. Is anyone already working on this issue?