Black: Feature request: let individual files encode whether they should be blackened

Created on 21 Jun 2019  路  5Comments  路  Source: psf/black

Prettier calls this concept "require pragma".

Sometimes you're in a big project whose git history is too valuable to entirely blow away by reformatting. But you want your new files to be blackened, and if major changes are made to old files, blacken them at that point (and not before). A simple way to do this would be to look for a comment like # @black before any definitions, and add a CLI flag which allows non-marked files to be skipped.

enhancement

Most helpful comment

@zsol that # fmt: off is not documented anywhere afaik, would be appreciated if it was

Edit: also it still does some formatting, even if told not to. It adds new empty line at the end of the file.

All 5 comments

I think this is a reasonable approach, but I'd like to point out that the opposite is already possible: you can mark all your existing files with # fmt: off at the beginning and Black won't touch them. Any new files you add without this will be formatted though.

Good point - so conversely there could be a "strict mode" command line option which only formats # fmt: on blocks.

@zsol that # fmt: off is not documented anywhere afaik, would be appreciated if it was

Edit: also it still does some formatting, even if told not to. It adds new empty line at the end of the file.

It's documented here. The empty line at the end of the file looks like a bug at first glance. Feel free to open a separate issue about it

A workaround for now is to format everything and skipping the commit in git history. More info in the docs

Was this page helpful?
0 / 5 - 0 ratings