black: "INTERNAL ERROR: Black produced different code on the second pass of the formatter"
- Version: 20.8b0
- OS and Python version: all
This is a rare problem that we’re currently investigating. The most common case of it has to do with a combination of magic trailing commas and optional parentheses. Long story short, there’s this behavior:
- Black now treats all pre-existing trailing magic commas found in bracket pairs as signal to explode that bracket pair (e.g. to format it “one element per line”);
- in other places Black inserts trailing commas itself after reformatting a long expression or statement;
- then on another run, Black finds those trailing commas and treats them as pre-existing trailing commas that signal that the given bracket pair should be exploded.
The expected behavior is that there should be no difference between the first formatting and the second formatting. In practice Black sometimes chooses for or against optional parentheses differently depending on whether the line should be exploded or not. This is what needs fixing.
Workaround
We’re working on fixing this, until then, format the file twice with --fast, it will keep its formatting moving forward.
Call To Action
If you find a case of this, please attach the generated log here so we can investigate. We’ve already added three identifying examples of this as expected failures to https://github.com/psf/black/pull/1627/commits/25206d8cc6e98143f0b10bcbe9e8b41b8b543abe.
Finally, if you’re interested in debugging this yourself, look for should_explode in if statements in the Black codebase. Those are the decisions that lead to unstable formatting.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 26
- Comments: 80 (9 by maintainers)
Commits related to this issue
- Set bound on black dep, return lint veto powers the most recent version of black made some changes, _and_ introduced some bugs: https://github.com/psf/black/issues/1629 let's prevent an update until... — committed to chartbeat-labs/textacy by bdewilde 4 years ago
- Work around psf/black#1629 — committed to twisted/twisted by twm 4 years ago
- Work around psf/black#1629 — committed to twisted/twisted by twm 4 years ago
- Use tox to run black and pin the version Pinning black for now because we have some code that the latest version doesn't want to format. See the github issue below for details. https://github.com/psf... — committed to clint-lawrence/Fixate by clint-lawrence 4 years ago
- Tweak formatting to prepare for switching to black (#3534) Switching the formatting to black in #3516 brought up a few issues. Black has an edge case that requires inserting trailing commas in some c... — committed to quantumlib/Cirq by maffoo 4 years ago
- Disable black waiting for https://github.com/psf/black/issues/1629 to be fixed and deployed — committed to BRGM/gml_application_schema_toolbox by Guts 4 years ago
- Update black rev in order to include fix for https://github.com/psf/black/issues/1629 — committed to tafaRU/oca-addons-repo-template by tafaRU 3 years ago
- Update black rev in order to include fix for https://github.com/psf/black/issues/1629 — committed to tafaRU/oca-addons-repo-template by tafaRU 3 years ago
- Update black rev in order to include fix for https://github.com/psf/black/issues/1629 — committed to tafaRU/oca-addons-repo-template by tafaRU 3 years ago
- Remove outdated workaround for a bug in black formatter The bug appears to be fixed in our required version of black per https://github.com/psf/black/issues/1629 — committed to pavoljuhas/Cirq by pavoljuhas a year ago
- Remove outdated workaround for a bug in black formatter (#6045) The bug appears to be fixed in our required version of black per https://github.com/psf/black/issues/1629 — committed to quantumlib/Cirq by pavoljuhas a year ago
Fixed by #2126.
One case that I believe refers to this bug: (non-relevant parts removed)
Using the workaround (running with --fast) worked for my case.
If anyone has time and interest in testing a potential fix for second-pass formatting instability, I’d welcome your feedback on https://github.com/psf/black/pull/1958.
As always, please read the changes and make sure you’re comfortable with them before checking them out and running them locally, as you should (ideally) for any other code. And feel free to ask in the pull request if anything is unclear. I can’t promise to give answers to everything but I’ll try.
I’ve used some of the examples from this thread during development and have had promising results so far, but in particular I’d be interested to hear about any counter-examples that continue to break.
Thanks @ichard26 - that’s almost true, but in the logs for that build, the fuzzer really did generate an input that reproduces #1629 (
INTERNAL ERROR: Black produced different code on the second pass of the formatter.).It’s slightly surprising, but is valid and relevant here, I think. #1913 seems to relate to the fuzzer exposing a separate
libcst-related error.Passing
--fastto Black isn’t fully safe as Black can produce invalid code.--fastdisables the unstable formatting check, the AST equivalence check, and the valid python code check. Black can accidentally destroy a valid Python program; here’s an example with--fast.An example equivalent to something that happened naturally in some code (
version 20.8b1):I had thought that this issue was mostly focused on new and
should_exploderelated instability bugs, not allblack produced different code on the second pass of the formattererrors, but IDK and IDC now. Also, while in the beginning #1913 exposed a hypothesmith issue (libcst wasn’t related at all), the issue was reopened for theclass A:\\\r\n# type: ignore\n pass\nissue.@jayaddison that’s already tracked via #1913.
This is true, it is certainly not a fix. But in my case, for this one file, it produced semantically identical code after “stabilizing”, and let me move on with using Black normally.
On Sat, Dec 12, 2020, 10:54 AM Richard Si notifications@github.com wrote:
Wait, the --fast option just skips the determistic checking, though. Right?
@ThatXliner, it’s a bug. One that is already fixed in the development branch by commit https://github.com/psf/black/commit/1d2d7264ec7c448744b771910cc972da03b1cb80, now it’s just waiting to be included in the next release.
Black basically missed things. I think the style may be inconsistent… I prefer no whitespace before the colon but eh, it’s black
Not a problem for our repo, since this was caught by me missing a folder in our black-ignore. But if this is helpful for debugging this problem then that’d be great. The diff is quite large at around 400 lines so I put it in a gist. Also run black with --verbose and put it below.
diff https://gist.github.com/xylix/9b282167786bc7ba9447ab11db9a6c5b
stacktrace