Black: "Black produced different code on the second pass of the formatter": comments and parentheses

Created on 16 May 2019  路  2Comments  路  Source: psf/black

Operating system: macOS Mojave 10.14.4 (18E226)
Python version: Python 3.7.3
Black version: 19.3b0
Does also happen on master: yes (957ba24bb6cdd32e4dd14ff2808dcf4d58851844)

Reproducers:

  1. Long comment

Source:

result = (
    1  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)

Black diff:

--- source
+++ first pass
@@ -1,4 +1,2 @@
-result = (
-    1  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-)
+result = 1  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

--- first pass
+++ second pass
@@ -1,2 +1,4 @@
-result = 1  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+result = (
+    1
+)  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  1. Long thing in parenthesis

Source:

result = (
    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  # aaa
)

Black diff:

--- source
+++ first pass
@@ -1,4 +1,2 @@
-result = (
-    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  # aaa
-)
+result = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  # aaa

--- first pass
+++ second pass
@@ -1,2 +1,4 @@
-result = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  # aaa
+result = (
+    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+)  # aaa
bug comments unstable formatting

Most helpful comment

@thalessa the bug is that Black is producing unstable formatting -- the formatter should be idempotent and here it is not. Black itself will alert you to this situation with the error message quoted in the title of this issue: "different code" in this context really means "the same code, differently formatted."

All 2 comments

I'm not sure what you're saying but 1 and (1) are the same code. It's not a tuple unless it's got a trailing comma like (1,).

Is this about how the comments are placed?

@thalessa the bug is that Black is producing unstable formatting -- the formatter should be idempotent and here it is not. Black itself will alert you to this situation with the error message quoted in the title of this issue: "different code" in this context really means "the same code, differently formatted."

Was this page helpful?
0 / 5 - 0 ratings