This project uses Black for code formatting to ensure consistent code style across the codebase.
Black is configured in the pyproject.toml file with the following settings:
[tool.black]
line-length = 120
target-version = ["py311"]
include = '\.pyi?$'
exclude = '''
/(
\.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
| generated
| __pycache__
)/
'''Install Black and other development tools:
pip install -r requirements.dev.txtOr install Black directly:
pip install black==23.3.0Run Black on your codebase:
# Format all Python files in the project
python -m black .
# Format a specific directory
python -m black cosmotech/orchestrator/
# Check if files would be reformatted without actually changing them
python -m black --check .
# Show diff of changes without writing files
python -m black --diff .This project uses pre-commit hooks to automatically run Black before each commit. To set up pre-commit:
- Install pre-commit:
pip install pre-commit- Install the git hooks:
pre-commit installNow Black will run automatically on the files you've changed when you commit.
If you use VSCode, you can configure it to automatically format your code with Black when you save a file. Add the following to your .vscode/settings.json:
{
"python.formatting.provider": "black",
"editor.formatOnSave": true,
"python.formatting.blackArgs": ["--line-length", "120"]
}To enforce Black formatting in your CI pipeline, add a step to run Black in check mode:
- name: Check code formatting with Black
run: |
python -m pip install black==23.3.0
python -m black --check .When first applying Black to an existing codebase, you might want to:
- Run Black with
--difffirst to see the changes - Consider running it on one module at a time
- Make the formatting change in a separate commit from functional changes
- Eliminates debates about formatting
- Consistent code style across the entire codebase
- Minimal configuration needed
- Widely adopted in the Python community