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

Black fails for some long lines. #1710

Closed
exhuma opened this issue Sep 16, 2020 · 3 comments
Closed

Black fails for some long lines. #1710

exhuma opened this issue Sep 16, 2020 · 3 comments
Labels
C: unstable formatting Formatting changed on the second pass R: duplicate This issue or pull request already exists T: bug Something isn't working

Comments

@exhuma
Copy link

exhuma commented Sep 16, 2020

Describe the bug

For some very long lines, black bails out with the error:

error: cannot format foo.py: INTERNAL ERROR: Black produced different code on the second pass of the formatter.  Please report a bug on https://github.com/psf/black/issues.  This diff might be helpful: /tmp/blk_e7p5_wy5.log

To Reproduce Steps to reproduce the behavior:

  1. Take this file '...'
class MyLongClassName:
    LONG_NAME_1 = "foo"
    LONG_NAME_2 = "bar"

    def __init__(self, value):
        if value not in (MyLongClassName.LONG_NAME_2, MyLongClassName.LONG_NAME_1) and not isinstance(
            value, int
        ):
            pass
  1. Run Black on it with these arguments

black -l 80 foo.py

  1. See error
    error: cannot format foo.py: INTERNAL ERROR: Black produced different code on the second pass of the formatter.  Please report a bug on https://github.com/psf/black/issues.  This diff might be helpful: /tmp/blk_e7p5_wy5.log

Expected behavior

No error should appear

Environment (please complete the following information):

  • Version: 20.8b1
  • OS and Python version: e.g. Linux/Python 3.6.9

Does this bug also happen on master? yes

Additional context

The following file works:

class Foo:
    LONG_NAME_1 = "foo"
    LONG_NAME_2 = "bar"

    def __init__(self, value):
        if value not in (Foo.LONG_NAME_2, Foo.LONG_NAME_1) and not isinstance(
            value, int
        ):
            pass

Log File /tmp/blk_e7p5_wy5.log

Mode(target_versions=set(), line_length=80, string_normalization=True, experimental_string_processing=False, is_pyi=False)
--- source
+++ first pass
@@ -1,9 +1,10 @@
 class MyLongClassName:
     LONG_NAME_1 = "foo"
     LONG_NAME_2 = "bar"

     def __init__(self, value):
-        if value not in (MyLongClassName.LONG_NAME_2, MyLongClassName.LONG_NAME_1) and not isinstance(
-            value, int
-        ):
+        if value not in (
+            MyLongClassName.LONG_NAME_2,
+            MyLongClassName.LONG_NAME_1,
+        ) and not isinstance(value, int):
             pass
--- first pass
+++ second pass
@@ -1,10 +1,14 @@
 class MyLongClassName:
     LONG_NAME_1 = "foo"
     LONG_NAME_2 = "bar"

     def __init__(self, value):
-        if value not in (
-            MyLongClassName.LONG_NAME_2,
-            MyLongClassName.LONG_NAME_1,
-        ) and not isinstance(value, int):
+        if (
+            value
+            not in (
+                MyLongClassName.LONG_NAME_2,
+                MyLongClassName.LONG_NAME_1,
+            )
+            and not isinstance(value, int)
+        ):
             pass
@exhuma exhuma added the T: bug Something isn't working label Sep 16, 2020
@exhuma
Copy link
Author

exhuma commented Sep 16, 2020

In case someone runs into this issue as well, I fenced it with # fmt: off and # fmt: on as a workaround for now.

@lodagro
Copy link

lodagro commented Sep 23, 2020

Similar situation, input is only one (very long) line of code.

 x = "%s   %s        %s                                        %s %s %s      %s        %s %s" % (a_very_long,b_very_long,c_very_long,d_very_long,e_very_long,f_very_long,g_very_long,h_very_long,i_very_long) 
  1 Mode(target_versions=set(), line_length=88, string_normalization=1, experimental_string_processing=False, is_pyi=False)                                                                                                                                                                                                  
    2 --- source                                                                                                                                                                                                                                                                                                               
    3 +++ first pass                                                                                                                                                                                                                                                                                                           
    4 @@ -1 +1,14 @@                                                                                                                                                                                                                                                                                                           
    5 -x = "%s   %s        %s                                        %s %s %s      %s        %s %s" % (a_very_long,b_very_long,c_very_long,d_very_long,e_very_long,f_very_long,g_very_long,h_very_long,i_very_long)                                                                                                            
    6 +x = (                                                                                                                                                                                                                                                                                                                   
    7 +    "%s   %s        %s                                        %s %s %s      %s        %s %s"                                                                                                                                                                                                                            
    8 +    % (                                                                                                                                                                                                                                                                                                                 
    9 +        a_very_long,                                                                                                                                                                                                                                                                                                    
   10 +        b_very_long,                                                                                                                                                                                                                                                                                                    
   11 +        c_very_long,                                                                                                                                                                                                                                                                                                    
   12 +        d_very_long,                                                                                                                                                                                                                                                                                                    
   13 +        e_very_long,                                                                                                                                                                                                                                                                                                    
   14 +        f_very_long,                                                                                                                                                                                                                                                                                                    
   15 +        g_very_long,                                                                                                                                                                                                                                                                                                    
   16 +        h_very_long,                                                                                                                                                                                                                                                                                                    
   17 +        i_very_long,                                                                                                                                                                                                                                                                                                    
   18 +    )                                                                                                                                                                                                                                                                                                                   
   19 +)                                                                                                                                                                                                                                                                                                                       
   20 --- first pass                                                                                                                                                                                                                                                                                                           
   21 +++ second pass                                                                                                                                                                                                                                                                                                          
   22 @@ -1,14 +1,11 @@                                                                                                                                                                                                                                                                                                        
   23 -x = (                                                                                                                                                                                                                                                                                                                   
   24 -    "%s   %s        %s                                        %s %s %s      %s        %s %s"                                                                                                                                                                                                                            
   25 -    % (                                                                                                                                                                                                                                                                                                                 
   26 -        a_very_long,                                                                                                                                                                                                                                                                                                    
   27 -        b_very_long,                                                                                                                                                                                                                                                                                                    
   28 -        c_very_long,                                                                                                                                                                                                                                                                                                    
   29 -        d_very_long,                                                                                                                                                                                                                                                                                                    
   30 -        e_very_long,                                                                                                                                                                                                                                                                                                    
   31 -        f_very_long,                                                                                                                                                                                                                                                                                                    
   32 -        g_very_long,                                                                                                                                                                                                                                                                                                    
   33 -        h_very_long,                                                                                                                                                                                                                                                                                                    
   34 -        i_very_long,                                                                                                                                                                                                                                                                                                    
   35 -    )                                                                                                                                                                                                                                                                                                                   
   36 +x = "%s   %s        %s                                        %s %s %s      %s        %s %s" % (                                                                                                                                                                                                                        
   37 +    a_very_long,                                                                                                                                                                                                                                                                                                        
   38 +    b_very_long,                                                                                                                                                                                                                                                                                                        
   39 +    c_very_long,                                                                                                                                                                                                                                                                                                        
   40 +    d_very_long,                                                                                                                                                                                                                                                                                                        
   41 +    e_very_long,                                                                                                                                                                                                                                                                                                        
   42 +    f_very_long,                                                                                                                                                                                                                                                                                                        
   43 +    g_very_long,                                                                                                                                                                                                                                                                                                        
   44 +    h_very_long,                                                                                                                                                                                                                                                                                                        
   45 +    i_very_long,                                                                                                                                                                                                                                                                                                        
   46  )                                        
INTERNAL ERROR: Black produced different code on the second pass of the formatter.
  • Version: 20.8b1
  • OS and Python version: Linux/Python 3.8

Also occurs on master

@ichard26 ichard26 added the C: unstable formatting Formatting changed on the second pass label Oct 26, 2020
@ichard26 ichard26 added the R: duplicate This issue or pull request already exists label Apr 25, 2021
@ichard26
Copy link
Collaborator

Hello!

All reproduction cases in this issue format without error on master. The fixing commit was 8672af3 from PR GH-2126. I'll be marking this issue as a duplicate of GH-1629 since that's what GH-2126 aimed to fix and it's highly likely this issue falls under GH-1629.

Since we use the issue tracker as a reflection of what's on master, I'll be closing this issue. If you have any issues, especially with the new (but stable) output, please open a new issue.

Thank you for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: unstable formatting Formatting changed on the second pass R: duplicate This issue or pull request already exists T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants