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

Internal error while formatting this code #1818

Closed
knatten opened this issue Nov 13, 2020 · 3 comments
Closed

Internal error while formatting this code #1818

knatten opened this issue Nov 13, 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

@knatten
Copy link

knatten commented Nov 13, 2020

Describe the bug
Black fails with an INTERNAL ERROR, see below:

To Reproduce

  1. Take this file repro.py:
import numpy

def outer():
    coeff = numpy.zeros((3,3,3,3))

    def foo(a, b):
        return 1

    def bar(col_pos, row_pos, curr_iter):
        return sum(foo(
            coeff[col_pos, row_pos, 1, curr_iter],
            coeff[col_pos, row_pos, 2, curr_iter]) for idx in [1, 2]) / float(1)

    return bar(1,1,1)

print(outer())
  1. Run black --check repro.py

  2. See error:

error: cannot format repro.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_vtl3owu6.log
Oh no! 💥 💔 💥
1 file would fail to reformat.

Expected behavior
I expect the file to be formatted properly

Environment (please complete the following information):

  • Version: black, version 20.8b1
  • OS and Python version: Ubuntu 20.04 / Python 3.8.5

Does this bug also happen on master?

This also happens at https://black.now.sh/?version=master

Additional context

Output from /tmp/blk_vtl3owu6.log:

cat /tmp/blk_vtl3owu6.log
Mode(target_versions=set(), line_length=88, string_normalization=True, experimental_string_processing=False, is_pyi=False)
--- source
+++ first pass
@@ -1,16 +1,22 @@
 import numpy
 
+
 def outer():
-    coeff = numpy.zeros((3,3,3,3))
+    coeff = numpy.zeros((3, 3, 3, 3))
 
     def foo(a, b):
         return 1
 
     def bar(col_pos, row_pos, curr_iter):
-        return sum(foo(
-            coeff[col_pos, row_pos, 1, curr_iter],
-            coeff[col_pos, row_pos, 2, curr_iter]) for idx in [1, 2]) / float(1)
+        return sum(
+            foo(
+                coeff[col_pos, row_pos, 1, curr_iter],
+                coeff[col_pos, row_pos, 2, curr_iter],
+            )
+            for idx in [1, 2]
+        ) / float(1)
 
-    return bar(1,1,1)
+    return bar(1, 1, 1)
+
 
 print(outer())
--- first pass
+++ second pass
@@ -6,17 +6,20 @@
 
     def foo(a, b):
         return 1
 
     def bar(col_pos, row_pos, curr_iter):
-        return sum(
-            foo(
-                coeff[col_pos, row_pos, 1, curr_iter],
-                coeff[col_pos, row_pos, 2, curr_iter],
+        return (
+            sum(
+                foo(
+                    coeff[col_pos, row_pos, 1, curr_iter],
+                    coeff[col_pos, row_pos, 2, curr_iter],
+                )
+                for idx in [1, 2]
             )
-            for idx in [1, 2]
-        ) / float(1)
+            / float(1)
+        )
 
     return bar(1, 1, 1)
 
 
 print(outer())
@knatten knatten added the T: bug Something isn't working label Nov 13, 2020
@laktak
Copy link

laktak commented Nov 14, 2020

Here is another testcase for IMHO the same bug:

import os

def has_256cols():
    return os.environ.get("COLORTERM", "") in ["truecolor", "24bit", "8bit"] or "256" in os.environ.get("TERM", "")

print(has_256cols())
Mode(target_versions=set(), line_length=88, string_normalization=True, experimental_string_processing=False, is_pyi=False)
--- source
+++ first pass
@@ -1,6 +1,12 @@
 import os
 
+
 def has_256cols():
-    return os.environ.get("COLORTERM", "") in ["truecolor", "24bit", "8bit"] or "256" in os.environ.get("TERM", "")
+    return os.environ.get("COLORTERM", "") in [
+        "truecolor",
+        "24bit",
+        "8bit",
+    ] or "256" in os.environ.get("TERM", "")
+
 
 print(has_256cols())
--- first pass
+++ second pass
@@ -1,12 +1,16 @@
 import os
 
 
 def has_256cols():
-    return os.environ.get("COLORTERM", "") in [
-        "truecolor",
-        "24bit",
-        "8bit",
-    ] or "256" in os.environ.get("TERM", "")
+    return (
+        os.environ.get("COLORTERM", "")
+        in [
+            "truecolor",
+            "24bit",
+            "8bit",
+        ]
+        or "256" in os.environ.get("TERM", "")
+    )
 
 
 print(has_256cols())

@ichard26 ichard26 added the C: unstable formatting Formatting changed on the second pass label Nov 14, 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. Oh and the fix should be available in a published release soon, see GH-2125 for more info.

Thank you for reporting!

@knatten
Copy link
Author

knatten commented Apr 26, 2021

Thanks for fixing, and for the detailed reply!

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