Treat black's NothingChanged as a successful no-op - #573
Open
Eljees wants to merge 1 commit into
Open
Conversation
black reports "this is already formatted" by raising NothingChanged rather than by returning the content unchanged. BlackFormatter did not catch it, so whenever an autofix happened to produce content that black had nothing to say about, the exception escaped format_module. fixit_bytes wraps the whole lint-and-fix pass in a broad except, so the result was worse than a lost fix: the run reported a lint error carrying black.report.NothingChanged, and the fix was discarded. The ufmt formatter is unaffected because ufmt_bytes handles this internally, which is why the issue only shows up with formatter = "black". Closes Instagram#427
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #427.
The problem
black.format_file_contentsreports "this is already formatted" by raisingNothingChangedrather than by returning the content unchanged.BlackFormatter.formatdoes not catch it, so as soon as an autofix producescontent that black has nothing to say about, the exception escapes
format_module.fixit_byteswraps the whole pass in a broadexcept Exception, so theoutcome is worse than a lost fix: the run yields a
Resultcarryingblack.report.NothingChangedas a lint error, and the fix is dropped. Thatmatches the traceback in the report.
UfmtFormatteris not affected, becauseufmt_byteshandles this internally.That is why the issue only appears with
formatter = "black".The change
Catch
NothingChangedand return the module bytes, which is what black wouldhave returned had it not signalled through an exception.
Tests
A new
fixit.tests.formatmodule. Without the change the already-formattedcase reproduces the reported failure:
The same test also covers content that black does rewrite, so the change
cannot be satisfied by skipping formatting altogether, plus the no-formatter
path for completeness.
The suite goes from 389 to 391 tests, all passing.
flake8andufmt checkon the touched files are clean.