Skip to content

Commit a940972

Browse files
authored
Add pre-commit config yaml file to enable automatic code formatting (#5561)
* add precommit config * skip running hooks if no matched files found * fix instructions for using pre-commit
1 parent b91070b commit a940972

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

.pre-commit-config.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
repos:
2+
- repo: https://github.com/psf/black
3+
rev: 23.1.0
4+
hooks:
5+
- id: black
6+
language_version: python3
7+
types: [python]
8+
stages: [commit]
9+
args: ["--config", "pyproject.toml", "tests", "src", "benchmarks", "metrics"]
10+
- repo: https://github.com/charliermarsh/ruff-pre-commit
11+
rev: 'v0.0.247'
12+
hooks:
13+
- id: ruff
14+
stages: [commit]
15+
args: [ "--config", "pyproject.toml", "tests", "src", "benchmarks", "metrics", "--fix"]

CONTRIBUTING.md

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,54 +31,59 @@ If you want to add a dataset see specific instructions in the section [*How to a
3131

3232
2. Clone your fork to your local disk, and add the base repository as a remote:
3333

34-
```bash
35-
git clone [email protected]:<your Github handle>/datasets.git
36-
cd datasets
37-
git remote add upstream https://github.com/huggingface/datasets.git
38-
```
34+
```bash
35+
git clone [email protected]:<your Github handle>/datasets.git
36+
cd datasets
37+
git remote add upstream https://github.com/huggingface/datasets.git
38+
```
3939

4040
3. Create a new branch to hold your development changes:
4141

42-
```bash
43-
git checkout -b a-descriptive-name-for-my-changes
44-
```
42+
```bash
43+
git checkout -b a-descriptive-name-for-my-changes
44+
```
4545

46-
**do not** work on the `main` branch.
46+
**do not** work on the `main` branch.
4747

4848
4. Set up a development environment by running the following command in a virtual environment:
4949

50-
```bash
51-
pip install -e ".[dev]"
52-
```
50+
```bash
51+
pip install -e ".[dev]"
52+
```
5353

5454
(If datasets was already installed in the virtual environment, remove
5555
it with `pip uninstall datasets` before reinstalling it in editable
5656
mode with the `-e` flag.)
5757

5858
5. Develop the features on your branch.
5959

60-
6. Format your code. Run black and ruff so that your newly added files look nice with the following command:
60+
6. Format your code. Run `black` and `ruff` so that your newly added files look nice with the following command:
61+
62+
```bash
63+
make style
64+
```
65+
66+
7. _(Optional)_ You can also use [`pre-commit`](https://pre-commit.com/) to format your code automatically each time run `git commit`, instead of running `make style` manually.
67+
To do this, install `pre-commit` via `pip install pre-commit` and then run `pre-commit install` in the project's root directory to set up the hooks.
68+
Note that if any files were formatted by `pre-commit` hooks during committing, you have to run `git commit` again .
6169
62-
```bash
63-
make style
64-
```
6570
66-
7. Once you're happy with your contribution, add your changed files and make a commit to record your changes locally:
71+
8. Once you're happy with your contribution, add your changed files and make a commit to record your changes locally:
6772

68-
```bash
69-
git add -u
70-
git commit
71-
```
73+
```bash
74+
git add -u
75+
git commit
76+
```
7277

73-
It is a good idea to sync your copy of the code with the original
74-
repository regularly. This way you can quickly account for changes:
78+
It is a good idea to sync your copy of the code with the original
79+
repository regularly. This way you can quickly account for changes:
7580

76-
```bash
77-
git fetch upstream
78-
git rebase upstream/main
81+
```bash
82+
git fetch upstream
83+
git rebase upstream/main
7984
```
8085

81-
8. Once you are satisfied, push the changes to your fork repo using:
86+
9. Once you are satisfied, push the changes to your fork repo using:
8287

8388
```bash
8489
git push -u origin a-descriptive-name-for-my-changes

0 commit comments

Comments
 (0)