black icon indicating copy to clipboard operation
black copied to clipboard

INTERNAL ERROR when redundant parentheses are removed and two `# type: ignore` comments end up on one line

Open chaitanyachaudhary30 opened this issue 3 weeks ago • 1 comments

Describe the bug

Black fails with INTERNAL ERROR ... produced code that is not equivalent to the source when it removes redundant parentheses around a right-hand side and both the last line inside the parentheses and the closing parenthesis have a # type: ignore comment.

Black joins the two comments onto one line: x = f(a) # type: ignore[arg-type] # type: ignore[assignment]. Python only recognizes the first # type: ignore on a line, so the source has two TypeIgnore nodes and the output has one, and the equivalence check fails. The file is left unformatted.

To Reproduce

Take this code:

x = (
    f(a)  # type: ignore[arg-type]
)  # type: ignore[assignment]

And run it with these arguments:

$ black example.py

The resulting error is:

error: cannot format example.py: INTERNAL ERROR: Black 26.10.2.dev98+gb4f1fcae4 on Python (CPython) 3.13.18 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: ...\blk_8bklel6t.log

The diff in the log:

--- src
+++ dst
@@ -33,8 +33,6 @@
         )  # /Call
     )  # /Assign
     type_ignores=
     TypeIgnore(
     )  # /TypeIgnore
-    TypeIgnore(
-    )  # /TypeIgnore
 )  # /Module

With --fast the output is:

x = f(a)  # type: ignore[arg-type]  # type: ignore[assignment]

Expected behavior

Black formats the file and both comments stay valid # type: ignore comments. Leaving the code unchanged would do that. Black already leaves it unchanged when the parenthesized value is a name or an attribute access:

x = (
    y  # type: ignore[arg-type]
)  # type: ignore[assignment]

The error needs a value that ends in a bracket. These also fail: [a], f(a).run.

Environment

  • Black's version: main (26.10.2.dev98+gb4f1fcae4), also 26.10.1
  • OS and Python version: Windows 11, Python 3.13.18

Additional context

I hit this when running Black on an installed copy of PyTorch: torch/_inductor/output_code.py has this statement, and Black cannot format the file because of it.

            current_callable = (
                torch._C._aoti.AOTIModelContainerRunnerCuda(  # type: ignore[call-arg]
                    current_callable,
                    1,
                    self.device_type,
                    "",
                    True,
                ).run  # type: ignore[attr-defined]
            )  # type: ignore[attr-defined]

This looks related to #5618 and #5625, which fixed other cases where a # type: ignore ends up after another comment on the same line. The case of a comment that is not a type ignore inside the parentheses (f(a) # comment followed by ) # type: ignore) fails on 26.10.1 and is already fixed on main. The case above, with two type ignores, still fails on main.

chaitanyachaudhary30 avatar Nov 11 '26 22:11 chaitanyachaudhary30

I’d like to take this on 🙂 I’ll investigate keeping both type-ignore comments on separate lines and add regression tests

hector2394 avatar Nov 11 '26 22:11 hector2394