Skip to content

Black crashes when moving type: ignore comments #3737

Closed
@Zeckie

Description

Describe the bug

Black crashes when run on some code that includes a multiline statement with #type: ignore comments on multiple lines.

To Reproduce

For example, take this code:

class Foo:
   def foo(self) -> None:
       if True:
           self.foooooo=# type:ignore[error-code]
               Baaaaaaar# type:ignore[error-code]
                   self.baz()
               )
           ) 

And run it with these arguments:

$ black file.py

The resulting error is:

cannot format black.py: INTERNAL ERROR: Black 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

Logfile:

--- src
+++ dst
@@ -108,8 +108,6 @@
        'Foo',  # str
    )  # /ClassDef
  type_ignores=
    TypeIgnore(
    )  # /TypeIgnore
-    TypeIgnore(
-    )  # /TypeIgnore
)  # /Module
\ No newline at end of file

Expected behavior
Run without errors, leaving #type: ignore comments with the code they are annotating.

Environment

  • Black's version: main, 23.3.0
  • OS and Python version: Windows 10, python 3.11.2

Additional context

The #type: ignore comments are for mypy.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    C: crashBlack is crashingF: commentsThe syntactic kind. Not in the language grammar, always on our minds. Best bugs.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions