= 1 + 2 + \
x 3
Black
1. What is Black?
- Purpose: Black formats Python code to follow a consistent style. It automates formatting, saving time and avoiding manual reformatting errors.
- Philosophy: Code formatting should be deterministic, and Black prioritizes code readability and simplicity.
2. Key Features
- Deterministic Formatting: Given the same code, Black will always produce the same output.
- Minimal Configuration: Black enforces a single style with very few customization options.
- Speed and Safety: It’s fast and guarantees that formatting will not change your code’s functionality.
- Support for Python Versions: Black supports Python 3.6+ and Python 2.7 (with reduced functionality for legacy versions).
3. Installation
Install Black via pip
:
pip install black
Alternatively, use pipx
for an isolated environment:
pipx install black
4. Usage
Run Black on a file or directory:
black filename.py
black path/to/code/
Common Options
Check for Formatting: Check if code is formatted without changing it:
black --check .
Show Changes: See what changes Black would make:
black --diff .
Exclude Files: Skip specific files or directories:
black --exclude "migrations|env"
Line Length: Adjust line length (default is 88):
black --line-length 79 .
Safe Mode: Skip files with syntax errors:
black --safe .
5. Configuration
Using pyproject.toml
Black can be configured via a pyproject.toml
file:
[tool.black]
line-length = 88
target-version = ['py38']
include = '\.pyi?$'
exclude = '''
/(
\.git
| \.mypy_cache
| \.pytest_cache
| \.tox
| _build
| buck-out
| build
| dist
)/
'''
Place the pyproject.toml
file in the root directory of your project.
6. Features
a. Line Wrapping
- Lines exceeding the specified length (
--line-length
) are automatically wrapped. - Black prioritizes readability when wrapping.
b. String Normalization
By default, Black normalizes all strings to double quotes (
"
):print("This is Black-formatted.")
Disable this behavior with the --skip-string-normalization
flag:
black --skip-string-normalization .
c. Implicit vs. Explicit Line Joins
- Black uses parentheses for line continuations instead of backslashes.
Example:
# Before
= 1 + 2 + \
x 3
# After
= (
x 1 + 2 +
3
)
7. Integration
a. IDE Integration
- VS Code:
- Install the Python extension.
- Set Black as the formatter:
- Go to
Settings
> Search for “Python Formatting Provider” > SelectBlack
.
- Go to
- Enable “Format on Save”:
- Go to
Settings
> Search for “Format on Save” > Enable it.
- Go to
- PyCharm:
- Install Black globally or in your virtual environment.
- Set up a File Watcher:
- Go to File > Settings > Tools > File Watchers.
- Add a new watcher for
Black
.
b. Pre-commit Hooks
Use pre-commit
to enforce Black before committing code:
Install
pre-commit
:pip install pre-commit
Add
.pre-commit-config.yaml
:repos: - repo: https://github.com/psf/black rev: 23.1.0 # Replace with the latest version hooks: - id: black
Install the hook:
pre-commit install
Now, Black will automatically run on changed files before every commit.
8. Continuous Integration
Example: GitHub Actions
Add Black to your CI pipeline:
name: Python Linting
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.9
- name: Install dependencies
run: pip install black
- name: Run Black
run: black --check .
9. Combining Black with isort
isort formats imports. Use it alongside Black:
Install isort:
pip install isort
Run isort and Black together:
isort . && black .
Add both to your
pre-commit
hooks:repos: - repo: https://github.com/pycqa/isort rev: 5.10.1 hooks: - id: isort - repo: https://github.com/psf/black rev: 23.1.0 hooks: - id: black
10. Advantages of Black
- Consistency: Eliminates debates over style.
- Automation: Saves time with automatic formatting.
- Collaboration: Teams can focus on functionality instead of style.
- Tool Integration: Works seamlessly with IDEs, pre-commit, and CI tools.
11. Limitations
- No Custom Styles: Black enforces a strict style with minimal customization.
- Changes Code Structure: Some developers may find the formatting disruptive (e.g., line wrapping decisions).
- Formatting-First: Black does not lint for correctness; use tools like flake8 for that.
12. Common Issues
a. Black Not Formatting Code
Ensure Black is installed and the Python environment