Black: Black produced different code on the second pass: parentheses and assert

Created on 12 Jun 2019  ·  4Comments  ·  Source: psf/black

Operating system: macOS 10.14.5
Python version: 3.7.2
Black version: https://github.com/python/black/commit/1bbb01b854d168d76ebe4bf78961c2152ae075d9 (19.3b0)
Does also happen on master: yep

--- source
+++ first pass
 def test_get_schema_fails_on_type():
     with pytest.raises(ValueError) as excinfo:
         Schema.read(1)
-    assert str(excinfo.value) == (
-        """"1" is an unidentified schema source.\nA dict, a full s3 path or URL is expected"""
-    )
+    assert str(
+        excinfo.value
+    ) == """"1" is an unidentified schema source.\nA dict, a full s3 path or URL is expected"""


--- first pass
+++ second pass
@@ -46,13 +46,14 @@


 def test_get_schema_fails_on_type():
     with pytest.raises(ValueError) as excinfo:
         Schema.read(1)
-    assert str(
-        excinfo.value
-    ) == """"1" is an unidentified schema source.\nA dict, a full s3 path or URL is expected"""
+    assert (
+        str(excinfo.value)
+        == """"1" is an unidentified schema source.\nA dict, a full s3 path or URL is expected"""
+    )

bug unstable formatting

Most helpful comment

FWIW #818 fixes this

All 4 comments

FWIW #818 fixes this

Hi, I'm trying to migrate our codebase to black. I think this is the biggest remaining blocker we've run into. Are there any near term plans to fix this?

This only impacts about 0.3% of our files, so I'm considering trying to mitigate this issue so this doesn't block our migration. Do you have any ideas how to do this? My best idea is to automatically throw in a #fmt: off / #fmt: on comment when we find this so our tooling doesn't complain.

If I were you, I'd just slightly refactor those lines so they don't hit the bug; in the example above, for example, I'd put the expected string in a separate variable as expected = """....""".

Closing as this is no longer reproducible on master. git bisect tells us that 14b28c89c22659e1f935bc0ac22ee03d90bcc290 (PR #1079) fixed this bug:

(black) ichard26@acer-ubuntu:~/programming/oss/black$ cat temp.py
def test_get_schema_fails_on_type():
    with pytest.raises(ValueError) as excinfo:
        Schema.read(1)
    assert str(excinfo.value) == (
        """"1" is an unidentified schema source.\nA dict, a full s3 path or URL is expected"""
    )
(black) ichard26@acer-ubuntu:~/programming/oss/black$ git checkout 14b28c89c22659e1f935bc0ac22ee03d90bcc290
HEAD is now at 14b28c8 Back out #850 (#1079)
(black) ichard26@acer-ubuntu:~/programming/oss/black$ python black.py temp.py --diff
Traceback (most recent call last):
  File "black.py", line 54, in <module>
    from _version import version as __version__
ModuleNotFoundError: No module named '_version'
(black) ichard26@acer-ubuntu:~/programming/oss/black$ vim black.py  # fix that import since this is such an old version
(black) ichard26@acer-ubuntu:~/programming/oss/black$ python black.py temp.py --diff
All done! ✨ 🍰 ✨
1 file left unchanged.
(black) ichard26@acer-ubuntu:~/programming/oss/black$ git reset --hard
HEAD is now at 14b28c8 Back out #850 (#1079)
(black) ichard26@acer-ubuntu:~/programming/oss/black$ git checkout HEAD~1
Previous HEAD position was 14b28c8 Back out #850 (#1079)
HEAD is now at a73d258 fix CI (#1078)
(black) ichard26@acer-ubuntu:~/programming/oss/black$ vim black.py  # fix the broken import again
(black) ichard26@acer-ubuntu:~/programming/oss/black$ python black.py temp.py --diff
error: cannot format temp.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_ldsldtyc.log
Oh no! 💥 💔 💥
1 file failed to reformat.
(black) ichard26@acer-ubuntu:~/programming/oss/black$ cat /tmp/blk_ldsldtyc.log 
--- source
+++ first pass
@@ -1,7 +1,7 @@
 def test_get_schema_fails_on_type():
     with pytest.raises(ValueError) as excinfo:
         Schema.read(1)
-    assert str(excinfo.value) == (
-        """"1" is an unidentified schema source.\nA dict, a full s3 path or URL is expected"""
-    )
+    assert str(
+        excinfo.value
+    ) == """"1" is an unidentified schema source.\nA dict, a full s3 path or URL is expected"""

--- first pass
+++ second pass
@@ -1,7 +1,8 @@
 def test_get_schema_fails_on_type():
     with pytest.raises(ValueError) as excinfo:
         Schema.read(1)
-    assert str(
-        excinfo.value
-    ) == """"1" is an unidentified schema source.\nA dict, a full s3 path or URL is expected"""
+    assert (
+        str(excinfo.value)
+        == """"1" is an unidentified schema source.\nA dict, a full s3 path or URL is expected"""
+    )

Environment:

  • Black version: 14b28c89c22659e1f935bc0ac22ee03d90bcc290 and its parent
  • Python version: CPython 3.8.5
  • OS version: Ubuntu 20.04.1 LTS
Was this page helpful?
0 / 5 - 0 ratings