Skip to content

Treat black's NothingChanged as a successful no-op - #573

Open
Eljees wants to merge 1 commit into
Instagram:mainfrom
Eljees:fix/427-black-nothing-changed
Open

Treat black's NothingChanged as a successful no-op#573
Eljees wants to merge 1 commit into
Instagram:mainfrom
Eljees:fix/427-black-nothing-changed

Conversation

@Eljees

@Eljees Eljees commented Aug 29, 2026

Copy link
Copy Markdown

Closes #427.

The problem

black.format_file_contents reports "this is already formatted" by raising
NothingChanged rather than by returning the content unchanged.
BlackFormatter.format does not catch it, so as soon as an autofix produces
content that black has nothing to say about, the exception escapes
format_module.

fixit_bytes wraps the whole pass in a broad except Exception, so the
outcome is worse than a lost fix: the run yields a Result carrying
black.report.NothingChanged as a lint error, and the fix is dropped. That
matches the traceback in the report.

UfmtFormatter is not affected, because ufmt_bytes handles this internally.
That is why the issue only appears with formatter = "black".

The change

Catch NothingChanged and return the module bytes, which is what black would
have returned had it not signalled through an exception.

Tests

A new fixit.tests.format module. Without the change the already-formatted
case reproduces the reported failure:

ERROR: test_black_already_formatted (content=b'x = 1\n')
  File "src/fixit/format.py", line 51, in format
    content = black.format_file_contents(
  File "src/black/__init__.py", line 1117, in format_file_contents
black.report.NothingChanged

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. flake8 and ufmt check
on the touched files are clean.

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
@Eljees
Eljees requested a review from amyreese as a code owner August 29, 2026 04:41
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Autofix does not work with "black" formatter if black formatter changes nothing

1 participant