Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-line strings with type ignore pragma get merged into a single line #4510

Open
AleksMat opened this issue Nov 11, 2024 · 0 comments
Open
Labels
F: strings Related to our handling of strings T: bug Something isn't working

Comments

@AleksMat
Copy link
Contributor

Describe the bug

There is a clash between the process of reflowing multi-line strings and the rule that prevents formatting lines with # type: ignore pragma at the end. Here is what happens:

  1. A multi-line string gets first merged into a single-line string which can be arbitrarily long.
  2. Because of the pragma comment at the end of the line, the line is considered unsafe to split. Hence the final result is a single huge line.

To Reproduce

Here is a simple example:

(
    "A very very very very very very very very very very very very long string "
    "annotated with a type ignore pragma gets merged into a single very long line "
    "which is against the line length rule."
)  # type: ignore

Running black --unstable example.py produces:

("A very very very very very very very very very very very very long string annotated with a type ignore pragma gets merged into a single very long line which is against the line length rule.")  # type: ignore

Expected behavior

Probably the best option would be that the multi-line string wouldn't be reflown at all in cases there is a type ignore comment at the end. But the formatting definitely shouldn't produce code that breaks the line length rule.

Environment

  • Black's version: 24.10.0
  • OS and Python version: Linux, Python 3.11.9

Additional context

In practice this issue can appear if users do some f-string operations inside a multi-line string and want to suppress type checking. E.g. the following line also gets incorrectly reflown into a single huge line:

message = (
    "This is an error message that also does an f-string operation for which "
    f"type checks might have to be ignored: {value1 + value2} ")  # type: ignore
@AleksMat AleksMat added the T: bug Something isn't working label Nov 11, 2024
@JelleZijlstra JelleZijlstra added the F: strings Related to our handling of strings label Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: strings Related to our handling of strings T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants