black icon indicating copy to clipboard operation
black copied to clipboard

Formatting a dictionary with a long string and type ignore fails on --unstable

Open AleksMat opened this issue 3 months ago • 2 comments

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.1 and the latest code on the main branch

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.

AleksMat avatar Aug 20 '26 11:08 AleksMat

I prepared a fix for this in https://github.com/psf/black/pull/5329.

AleksMat avatar Aug 20 '26 12:08 AleksMat

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?

Gambit-Checkmate avatar Aug 22 '26 13:08 Gambit-Checkmate