black README

The project's README from github.com/psf/black. As one markdown file: README.md.

Black Logo

The Uncompromising Code Formatter

Actions Status Documentation Status Coverage Status License: MIT PyPI Supported Python Versions Downloads conda-forge Code style: black

“Any color you like.”

Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters.

Blackened code looks the same regardless of the project you're reading. Formatting becomes transparent after a while and you can focus on the content instead.

Black makes code review faster by producing the smallest diffs possible.

Watch the PyCon 2019 talk to learn more.


Read the documentation on ReadTheDocs!


Installation and usage

Installation

Black can be installed by running pip install black. It requires Python 3.10+ to run. If you want to format Jupyter Notebooks, install with pip install "black[jupyter]".

If you want to run Black without installing Python, download one of the PyInstaller-built standalone executables from the latest GitHub release.

Usage

To get started right away with sensible defaults:

black {source_file_or_directory}

You can run Black as a package if running it as a script doesn't work:

python -m black {source_file_or_directory}

Further information can be found in our docs:

Black is already successfully used by many projects, small and big. Black has a comprehensive test suite, with efficient parallel tests, and our own auto formatting and parallel Continuous Integration runner. Now that we have become stable, you should not expect large formatting changes in the future. Stylistic changes will mostly be responses to bug reports and support for new Python syntax. For more information please refer to The Black Code Style.

Also, as a safety measure which slows down processing, Black will check that the reformatted code still produces a valid AST that is effectively equivalent to the original (see the Pragmatism section for details). If you're feeling confident, use --fast.

The Black code style

Black is a PEP 8 compliant opinionated formatter. Black reformats entire files in place. Style configuration options are deliberately limited and rarely added. It doesn't take previous formatting into account (see Pragmatism for exceptions).

Our documentation covers the current Black code style, but planned changes to it are also documented. They're both worth taking a look at:

Changes to the Black code style are bound by the Stability Policy:

Please refer to this document before submitting an issue. What seems like a bug might be intended behaviour.

Pragmatism

Early versions of Black used to be absolutist in some respects. They took after its initial author. This was fine at the time as it made the implementation simpler and there were not many users anyway. Not many edge cases were reported. As a mature tool, Black does make some exceptions to rules it otherwise holds.

Please refer to this document before submitting an issue just like with the document above. What seems like a bug might be intended behaviour.

Configuration

Black is able to read project-specific default values for its command line options from a pyproject.toml file. This is especially useful for specifying custom --include and --exclude/--force-exclude/--extend-exclude patterns for your project.

You can find more details in our documentation:

And if you're looking for more general configuration documentation:

Pro-tip: If you're asking yourself "Do I need to configure anything?" the answer is "No". Black is all about sensible defaults. Applying those defaults will have your code in compliance with many other Black formatted projects.

Used by

The following notable open-source projects trust Black with enforcing a consistent code style: pytest, tox, Pyramid, Django, Django Channels, Hypothesis, attrs, SQLAlchemy, Poetry, PyPA applications (Warehouse, Bandersnatch, Pipenv, virtualenv), pandas, Pillow, Twisted, LocalStack, every Datadog Agent Integration, Home Assistant, Zulip, Kedro, OpenOA, FLORIS, ORBIT, WOMBAT, and many more.

The following organizations use Black: Dropbox, KeepTruckin, Lyft, Mozilla, Quora, Duolingo, QuantumBlack, Tesla, Archer Aviation.

Are we missing anyone? Let us know.

Testimonials

Mike Bayer, creator of SQLAlchemy:

I can't think of any single tool in my entire programming career that has given me a bigger productivity increase by its introduction. I can now do refactorings in about 1% of the keystrokes that it would have taken me previously when we had no way for code to format itself.

Dusty Phillips, writer:

Black is opinionated so you don't have to be.

Hynek Schlawack, creator of attrs, core developer of Twisted and CPython:

An auto-formatter that doesn't suck is all I want for Xmas!

Carl Meyer, Django core developer:

At least the name is good.

Kenneth Reitz, creator of requests and pipenv:

This vastly improves the formatting of our code. Thanks a ton!

Show your style

Use the badge in your project's README.md:

[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

Using the badge in README.rst:

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
    :target: https://github.com/psf/black

Looks like this: Code style: black

License

MIT

Contributing

Welcome! Happy to see you willing to make the project better. You can get started by reading this:

You can also take a look at the rest of the contributing docs or talk with the developers:

Change log

The log has become rather long. It moved to its own file.

See CHANGES.

Authors

The author list is quite long nowadays, so it lives in its own file.

See AUTHORS.md

Code of Conduct

Everyone participating in the Black project, and in particular in the issue tracker, pull requests, and social media activity, is expected to treat other people with respect and more generally to follow the guidelines articulated in the Python Community Code of Conduct.

At the same time, humor is encouraged. In fact, basic familiarity with Monty Python's Flying Circus is expected. We are not savages.

And if you really need to slap somebody, do it with a fish while dancing.

black (psf/black)

Searching Database…

41.9K ·GitHub
这是什么?Black 是一个毫不妥协的 Python 代码格式化工具:它会把你的代码统一重排成一种固定的风格,让你不用再手工调整格式,也不用为风格问题争论。
和其他格式化工具相比,它哪里"不妥协"?它是一个符合 PEP 8 规范的格式化工具,但刻意只提供很少的风格选项。它会直接重写整个文件,完全忽略原来的格式,所以经过 Black 处理的代码在所有项目里看起来都一模一样。
我怎么安装和运行它?先运行 pip install black,然后把它指向你的文件或项目即可。默认设置开箱即用,不需要任何配置。
我需要装 Python 吗,还是可以直接下载一个可执行文件?Black 通常需要 Python 3.10 或更高版本,但你也可以完全不装 Python,直接从 GitHub 最新发布页面下载用 PyInstaller 打包的独立可执行文件。
格式化会不会把我的代码弄坏?默认情况下 Black 只接受安全的改动:它会检查格式化后的代码生成的 AST 是否与原来基本等价且有效。如果你很有信心,可以用 --fast 参数跳过这项检查。
我用什么操作系统有影响吗?基本没影响。Black 是一个可以用 pip 安装的 Python 包,只要 Python 能跑的地方它就能跑:Windows、macOS 和 Linux 都可以。
在我的项目里使用之前,我需要先配置风格吗?不用。Black 的理念就是合理的默认值,风格选项也刻意很少。像 --include 或 --exclude 这类命令行选项,可以在 pyproject.toml 文件里为单个项目设置默认值。
它也能格式化 Jupyter Notebook 吗?可以。用 pip install 'black[jupyter]' 安装后,Black 也能处理 Notebook。
有哪些人在生产环境中使用 Black?知名的开源项目包括 pytest、Django、pandas、Pillow、Poetry、Home Assistant 和 Zulip,还有 Dropbox、Mozilla、Lyft 和 Tesla 等公司。
Black 的代码风格以后会不会变来变去?不会有大的改动。Black 很稳定,未来的风格调整主要是响应 bug 报告和新增的 Python 语法,并且会遵循公开发布的稳定性政策。
What are similar projects?ruff, yapf, rubocop, PHP-CS-Fixer, SwiftLint, rector, git-cliff, tools
Creator
psf
Size
8.5 MB
Stars
41.9K
Age
8y
Commits · 90 Days