| 1 |
# The basics |
| 2 |
|
| 3 |
An overview on contributing to the _Black_ project. |
| 4 |
|
| 5 |
## Technicalities |
| 6 |
|
| 7 |
Development on the latest version of Python is preferred. You can use any operating |
| 8 |
system. |
| 9 |
|
| 10 |
First clone the _Black_ repository: |
| 11 |
|
| 12 |
```console |
| 13 |
$ git clone https://github.com/psf/black.git |
| 14 |
$ cd black |
| 15 |
``` |
| 16 |
|
| 17 |
Then install development dependencies inside a virtual environment of your choice, for |
| 18 |
example: |
| 19 |
|
| 20 |
```console |
| 21 |
$ python3 -m venv .venv |
| 22 |
$ source .venv/bin/activate # activation for linux and mac |
| 23 |
$ .venv\Scripts\activate # activation for windows |
| 24 |
|
| 25 |
(.venv)$ pip install --group dev |
| 26 |
(.venv)$ pip install -e ".[d]" |
| 27 |
(.venv)$ pre-commit install |
| 28 |
``` |
| 29 |
|
| 30 |
Before submitting pull requests, run lints and tests with the following commands from |
| 31 |
the root of the black repo: |
| 32 |
|
| 33 |
```console |
| 34 |
(.venv)$ pre-commit run -a # Linting |
| 35 |
(.venv)$ tox -e py # Unit tests |
| 36 |
(.venv)$ tox -e fuzz # Optional Fuzz testing |
| 37 |
(.venv)$ tox -e run_self # Format Black itself |
| 38 |
``` |
| 39 |
|
| 40 |
### Development |
| 41 |
|
| 42 |
Further examples of invoking the tests |
| 43 |
|
| 44 |
```console |
| 45 |
(.venv)$ tox --parallel=auto # Run all the above in parallel |
| 46 |
(.venv)$ tox -e py314 # Run tests on a specific python version |
| 47 |
(.venv)$ pytest -k <test name> # Run an individual test |
| 48 |
(.venv)$ tox -e py -- --no-cov # Pass arguments to pytest |
| 49 |
``` |
| 50 |
|
| 51 |
### Testing |
| 52 |
|
| 53 |
All aspects of the _Black_ style should be tested. Normally, tests should be created as |
| 54 |
files in the `tests/data/cases` directory. These files consist of up to three parts: |
| 55 |
|
| 56 |
- A line that starts with `# flags: ` followed by a set of command-line options. For |
| 57 |
example, if the line is `# flags: --preview --skip-magic-trailing-comma`, the test |
| 58 |
case will be run with preview mode on and the magic trailing comma off. The options |
| 59 |
accepted are mostly a subset of those of _Black_ itself, except for the |
| 60 |
`--minimum-version=` flag, which should be used when testing a grammar feature that |
| 61 |
works only in newer versions of Python. This flag ensures that we don't try to |
| 62 |
validate the AST on older versions and tests that we autodetect the Python version |
| 63 |
correctly when the feature is used. For the exact flags accepted, see the function |
| 64 |
`get_flags_parser` in `tests/util.py`. If this line is omitted, the default options |
| 65 |
are used. |
| 66 |
- A block of Python code used as input for the formatter. |
| 67 |
- The line `# output`, followed by the output of _Black_ when run on the previous block. |
| 68 |
If this is omitted, the test asserts that _Black_ will leave the input code unchanged. |
| 69 |
|
| 70 |
_Black_ has two pytest command-line options affecting test files in `tests/data/` that |
| 71 |
are split into an input part, and an output part, separated by a line with `# output`. |
| 72 |
These can be passed to `pytest` through `tox`, or directly into pytest if not using |
| 73 |
`tox`. |
| 74 |
|
| 75 |
#### `--print-full-tree` |
| 76 |
|
| 77 |
Upon a failing test, print the full concrete syntax tree (CST) as it is after processing |
| 78 |
the input ("actual"), and the tree that's yielded after parsing the output ("expected"). |
| 79 |
Note that a test can fail with different output with the same CST. This used to be the |
| 80 |
default, but now defaults to `False`. |
| 81 |
|
| 82 |
```console |
| 83 |
(.venv)$ tox -e py -- --print-full-tree |
| 84 |
``` |
| 85 |
|
| 86 |
#### `--print-tree-diff` |
| 87 |
|
| 88 |
Upon a failing test, print the diff of the trees as described above. This is the |
| 89 |
default. To turn it off pass `--print-tree-diff=False`. |
| 90 |
|
| 91 |
```console |
| 92 |
(.venv)$ tox -e py -- --print-tree-diff=False |
| 93 |
``` |
| 94 |
|
| 95 |
### News / Changelog Requirement |
| 96 |
|
| 97 |
`Black` has CI that will check for an entry corresponding to your PR in `CHANGES.md`. If |
| 98 |
you feel this PR does not require a changelog entry please state that in a comment and a |
| 99 |
maintainer can add a `ci: skip news` label to make the CI pass. Otherwise, please ensure |
| 100 |
you have a line in the following format added below the appropriate header: |
| 101 |
|
| 102 |
```md |
| 103 |
- `Black` is now more awesome (#X) |
| 104 |
``` |
| 105 |
|
| 106 |
<!--- |
| 107 |
The Next PR Number link uses HTML because of a bug in MyST-Parser that double-escapes the ampersand, causing the query parameters to not be processed. |
| 108 |
MyST-Parser issue: https://github.com/executablebooks/MyST-Parser/issues/760 |
| 109 |
MyST-Parser stalled fix PR: https://github.com/executablebooks/MyST-Parser/pull/929 |
| 110 |
--> |
| 111 |
|
| 112 |
Note that X should be your PR number, not issue number! To workout X, please use |
| 113 |
<a href="https://ichard26.github.io/next-pr-number/?owner=psf&name=black">Next PR |
| 114 |
Number</a>. This is not perfect but saves a lot of release overhead as now the releaser |
| 115 |
does not need to go back and workout what to add to the `CHANGES.md` for each release. |
| 116 |
|
| 117 |
### Style Changes |
| 118 |
|
| 119 |
Please familiarize yourself with our [stability policy](labels/stability-policy). |
| 120 |
Therefore, most style changes must be added to the `--preview` style. Exceptions are |
| 121 |
fixing crashes or changes that would not affect an already-formatted file. |
| 122 |
|
| 123 |
If a change would affect the advertised code style, please modify the documentation (The |
| 124 |
_Black_ code style) to reflect that change. Patches that fix unintended bugs in |
| 125 |
formatting don't need to be mentioned separately. |
| 126 |
|
| 127 |
If the change is implemented with the `--preview` flag, please include the change in the |
| 128 |
Future Style document instead and write the changelog entry under the dedicated "Preview |
| 129 |
style" heading. |
| 130 |
|
| 131 |
### Docs Testing |
| 132 |
|
| 133 |
If you make changes to docs, you can test they still build locally too. |
| 134 |
|
| 135 |
```console |
| 136 |
(.venv)$ pip install --group docs |
| 137 |
(.venv)$ pip install -e ".[d]" |
| 138 |
(.venv)$ sphinx-build -a -b html -W docs/ docs/_build/ |
| 139 |
``` |
| 140 |
|
| 141 |
## Hygiene |
| 142 |
|
| 143 |
If you're fixing a bug, add a test. Run it first to confirm it fails, then fix the bug, |
| 144 |
and run the test again to confirm it's really fixed. |
| 145 |
|
| 146 |
If adding a new feature, add a test. In fact, always add a test. If adding a large |
| 147 |
feature, please first open an issue to discuss it beforehand. |
| 148 |
|
| 149 |
## Finally |
| 150 |
|
| 151 |
Thanks again for your interest in improving the project! You're taking action when most |
| 152 |
people decide to sit and watch. |