惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
B
Blog
博客园_首页
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog RSS Feed
M
MIT News - Artificial intelligence
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
量子位
V
V2EX
Y
Y Combinator Blog
Hugging Face - Blog
Hugging Face - Blog
Martin Fowler
Martin Fowler
Recent Announcements
Recent Announcements
I
InfoQ
博客园 - 【当耐特】

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Black Formatter Setup for Python Project
2024-02-23 · via jdhao's digital space

This post is about how to set up black, the popular code formatter for Python projects.

Basic usage#

Format the file inplace :

If we only want to check if the file needs reformat, we can use --check:

black --check your_script.py

Show the diffs between old and new file if black were to reformat:

black --diff --color your_script.py

skip formatting for some code#

To skip formatting for a single line, you can add # fmt: skip at the end of the line. To skip formatting for a block of code, you need to add # fmt: off and # fmt:on before and after the block.

Configuration#

Black offers very little configuration. You can put the configuration inside pyproject.toml under the section [tool.black]:

[tool.black]
# The keys are the long option names what black accept on the command line.
line-length = 100
skip-string-normalization = true

The pyproject.toml file should be put at the root of the project.

ref:

Integration to Editor/IDE#

PyCharm#

For latest version of PyCharm, open settings, and go to Tools --> Black, you can configure how black should be run for a file.

Visual Studio Code#

Install the black formatter extension from Microsoft. Open the command plate (shift + command + P), and search Open User Settings (JSON), and open the user setting. Add the following config:

    "[python]": {
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.formatOnSave": true,
      }

Neovim#

For neovim you can use dedicated formatter plugins from here.

You can also use python-language-server and black as the formatter. The detailed configuration can be found here.

CI/CD integration#

GitHub action#

For GitHub action integration, please check the official doc: https://black.readthedocs.io/en/stable/integrations/github_actions.html

Azure DevOps#

For Azure DevOps, we can add a custom step to check the code formatting:

- script: |
    pip install black
    black --check --diff .
  displayName: Check code format with black

Ref:

Ignore the code reformat commit#

Create a file named .git-blame-ignore-revs in the project root and add the commit that reformat the code:

# reformat the code using black
<the-full-commit-hash>

Then when using git blame, we can provide this to ignore certain commit:

git blame your_script.py --ignore-revs-file .git-blame-ignore-revs

ref: