Skip to content

Commit b1ef196

Browse files
authored
More instructions in contributing guide regarding formatting (#4536)
* Enhanced contributing guide * Some more * Address comments
1 parent f630e67 commit b1ef196

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

CONTRIBUTING.md

+39-10
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,50 @@ If you would like to contribute a new dataset, please see [here](#New-dataset).
7575

7676
### Code formatting and typing
7777

78-
Contributions should be compatible with Python 3.X versions and be compliant with PEP8. To check the codebase, please
79-
either run
78+
#### Formatting
79+
80+
The torchvision code is formatted by [black](https://black.readthedocs.io/en/stable/),
81+
and checked against pep8 compliance with [flake8](https://flake8.pycqa.org/en/latest/).
82+
Instead of relying directly on `black` however, we rely on
83+
[ufmt](https://github.com/omnilib/ufmt), for compatibility reasons with Facebook
84+
internal infrastructure.
85+
86+
To format your code, install `ufmt` with `pip install ufmt` and use e.g.:
87+
8088
```bash
81-
pre-commit run --all-files
89+
ufmt format torchvision
8290
```
83-
or run
91+
92+
For the vast majority of cases, this is all you should need to run. For the
93+
formatting to be a bit faster, you can also choose to only apply `ufmt` to the
94+
files that were edited in your PR with e.g.:
95+
8496
```bash
85-
pre-commit install
86-
```
87-
once to perform these checks automatically before every `git commit`. If `pre-commit` is not available you can install
88-
it with
89-
```
90-
pip install pre-commit
97+
ufmt format `git diff main --name-only`
9198
```
9299

100+
Similarly, you can check for `flake8` errors with `flake8 torchvision`, although
101+
they should be fairly rare considering that most of the errors are automatically
102+
taken care of by `ufmt` already.
103+
104+
##### Pre-commit hooks
105+
106+
For convenience and **purely optionally**, you can rely on [pre-commit
107+
hooks](https://pre-commit.com/) which will run both `ufmt` and `flake8` prior to
108+
every commit.
109+
110+
First install the `pre-commit` package with `pip install pre-commit`, and then
111+
run `pre-commit install` at the root of the repo for the hooks to be set up -
112+
that's it.
113+
114+
Feel free to read the [pre-commit docs](https://pre-commit.com/#usage) to learn
115+
more and improve your workflow. You'll see for example that `pre-commit run
116+
--all-files` will run both `ufmt` and `flake8` without the need for you to
117+
commit anything, and that the `--no-verify` flag can be added to `git commit` to
118+
temporarily deactivate the hooks.
119+
120+
#### Type annotations
121+
93122
The codebase has type annotations, please make sure to add type hints if required. We use `mypy` tool for type checking:
94123
```bash
95124
mypy --config-file mypy.ini

0 commit comments

Comments
 (0)