Skip to content

string_processing: makes it impossible to use a comment to ignore a static type violation #3802

@tylerlaprade

Description

@tylerlaprade

Describe the bug

Black's --preview mode makes it impossible to suppress a type error in a string with a long comment.

To Reproduce

For example, take this code:

    def __str__(self):
        return "foo bar baz qux" # pyright: ignore[reportGeneralTypeIssues] -- This is some special Django magic that we can't easily tell Pyright about

And run it with these arguments:

$ black --preview

The resulting error is:

Unnecessary "# pyright: ignore" rule: "reportGeneralTypeIssues" (Pylance)

    def __str__(self):
        return (  # pyright: ignore[reportGeneralTypeIssues] -- This is some special Django magic that we can't easily tell Pyright about
            "foo bar baz qux"
        )

If I move the comment to the middle line, black moves it back up to the top.

Expected behavior

Even if black breaks my comment in some cases, there should be at least one possible place for me to leave the comment that will suppress my type error without black moving it.

Environment

  • Black's version: 23.7.0
  • OS and Python version: MacOS, 3.11

Additional context

This also occurs if the comment starts on the line with the violation

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: preview styleIssues with the preview and unstable style. Add the name of the responsible feature in the title.F: stringsRelated to our handling of stringsT: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions