black icon indicating copy to clipboard operation
black copied to clipboard

Black 23.9.1 reformat already clean code expression and creates a line too long

Open LLyaudet opened this issue 2 years ago • 4 comments

Describe the bug

Black 23.9.1 reformat already clean but complex code expression and creates a line too long. The original expression does not have lines longer than 79 characters. But black reformat it and creates a line of 94 characters.

To Reproduce

Create a file named bug.py with the following code :

if True:
    if True:
        if True:
            abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqr = (
                abcdefghijklmnopqrstuvwxyzabcd
                | (
                    # Blablablablablablablablablablablablablablablablablablabl
                    # blablablablablablablablablablablablablabla - (
                    abcdefghijk - (
                        abcdefghijklmnopqrstuvwxyzabcd
                        | abcdefghijklmnopqrstuvwxyza
                    )
                )
            )


And run black with these arguments:

$ black --check --diff bug.py

The resulting display is:

--- bug.py	2023-10-06 10:06:30.801786+00:00
+++ bug.py	2023-10-06 10:12:04.476521+00:00
@@ -1,15 +1,9 @@
 if True:
     if True:
         if True:
-            abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqr = (
-                abcdefghijklmnopqrstuvwxyzabcd
-                | (
-                    # Blablablablablablablablablablablablablablablablablablabl
-                    # blablablablablablablablablablablablablabla - (
-                    abcdefghijk - (
-                        abcdefghijklmnopqrstuvwxyzabcd
-                        | abcdefghijklmnopqrstuvwxyza
-                    )
-                )
+            abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqr = abcdefghijklmnopqrstuvwxyzabcd | (
+                # Blablablablablablablablablablablablablablablablablablabl
+                # blablablablablablablablablablablablablabla - (
+                abcdefghijk
+                - (abcdefghijklmnopqrstuvwxyzabcd | abcdefghijklmnopqrstuvwxyza)
             )
-
would reformat bug.py

Oh no! 💥 💔 💥
1 file would be reformatted.

Expected behavior

Black should keep it as is or at least propose something that does not go beyond the default line length.

Environment

  • Black's version: black, 23.9.1 (compiled: yes)
  • OS and Python version: Ubuntu 23.04 Python (CPython) 3.11.4

Additional context

LLyaudet avatar Oct 06 '23 10:10 LLyaudet

Hi @LLyaudet, I want to work on this issue. Could you assign it to me? Thanks

tienthai0205 avatar Oct 14 '23 11:10 tienthai0205

Hi @tienthai0205 :) Thanks for wanting to solve it. Unfortunately, I am not among the maintainers of this repo and I do not have the rights to assign issues. Maybe @JelleZijlstra can help :)

LLyaudet avatar Oct 14 '23 17:10 LLyaudet

Feel free to send a PR with a proposed solution.

JelleZijlstra avatar Oct 15 '23 21:10 JelleZijlstra