|
| 1 | +# Black Code Formatter Setup |
| 2 | + |
| 3 | +This project uses [Black](https://github.com/psf/black) for code formatting to ensure consistent code style across the codebase. |
| 4 | + |
| 5 | +## Configuration |
| 6 | + |
| 7 | +Black is configured in the `pyproject.toml` file with the following settings: |
| 8 | + |
| 9 | +```toml |
| 10 | +[tool.black] |
| 11 | +line-length = 120 |
| 12 | +target-version = ["py311"] |
| 13 | +include = '\.pyi?$' |
| 14 | +exclude = ''' |
| 15 | +/( |
| 16 | + \.git |
| 17 | + | \.hg |
| 18 | + | \.mypy_cache |
| 19 | + | \.tox |
| 20 | + | \.venv |
| 21 | + | _build |
| 22 | + | buck-out |
| 23 | + | build |
| 24 | + | dist |
| 25 | + | generated |
| 26 | + | __pycache__ |
| 27 | +)/ |
| 28 | +''' |
| 29 | +``` |
| 30 | + |
| 31 | +## Installation |
| 32 | + |
| 33 | +Install Black and other development tools: |
| 34 | + |
| 35 | +```bash |
| 36 | +pip install -r requirements.dev.txt |
| 37 | +``` |
| 38 | + |
| 39 | +Or install Black directly: |
| 40 | + |
| 41 | +```bash |
| 42 | +pip install black==23.3.0 |
| 43 | +``` |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +### Manual Usage |
| 48 | + |
| 49 | +Run Black on your codebase: |
| 50 | + |
| 51 | +```bash |
| 52 | +# Format all Python files in the project |
| 53 | +python -m black . |
| 54 | + |
| 55 | +# Format a specific directory |
| 56 | +python -m black cosmotech/coal/ |
| 57 | + |
| 58 | +# Check if files would be reformatted without actually changing them |
| 59 | +python -m black --check . |
| 60 | + |
| 61 | +# Show diff of changes without writing files |
| 62 | +python -m black --diff . |
| 63 | +``` |
| 64 | + |
| 65 | +### Pre-commit Hooks |
| 66 | + |
| 67 | +This project uses pre-commit hooks to automatically run Black before each commit. To set up pre-commit: |
| 68 | + |
| 69 | +1. Install pre-commit: |
| 70 | + |
| 71 | +```bash |
| 72 | +pip install pre-commit |
| 73 | +``` |
| 74 | + |
| 75 | +2. Install the git hooks: |
| 76 | + |
| 77 | +```bash |
| 78 | +pre-commit install |
| 79 | +``` |
| 80 | + |
| 81 | +Now Black will run automatically on the files you've changed when you commit. |
| 82 | + |
| 83 | +### VSCode Integration |
| 84 | + |
| 85 | +If you use VSCode, the included settings in `.vscode/settings.json` will automatically format your code with Black when you save a file. Make sure you have the Python extension installed. |
| 86 | + |
| 87 | +## CI Integration |
| 88 | + |
| 89 | +To enforce Black formatting in your CI pipeline, add a step to run Black in check mode: |
| 90 | + |
| 91 | +```yaml |
| 92 | +- name: Check code formatting with Black |
| 93 | + run: | |
| 94 | + python -m pip install black==23.3.0 |
| 95 | + python -m black --check . |
| 96 | +``` |
| 97 | +
|
| 98 | +## Migrating Existing Code |
| 99 | +
|
| 100 | +When first applying Black to an existing codebase, you might want to: |
| 101 | +
|
| 102 | +1. Run Black with `--diff` first to see the changes |
| 103 | +2. Consider running it on one module at a time |
| 104 | +3. Make the formatting change in a separate commit from functional changes |
| 105 | + |
| 106 | +## Benefits of Using Black |
| 107 | + |
| 108 | +- Eliminates debates about formatting |
| 109 | +- Consistent code style across the entire codebase |
| 110 | +- Minimal configuration needed |
| 111 | +- Widely adopted in the Python community |
0 commit comments