Skip to content

Commit bc55875

Browse files
niksirbiAntonin Blot
authored andcommitted
Replace black with ruff-format (neuroinformatics-unit#116)
* replaced black with ruff-format * add commented (optional) ruff rules * Updated README.md * removed black dependency * restore inadvertendly removed brackets
1 parent 3311aec commit bc55875

File tree

6 files changed

+51
-50
lines changed

6 files changed

+51
-50
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ repos:
2121
hooks:
2222
- id: ruff
2323
args: [ --config=pyproject.toml ]
24-
- repo: https://github.com/psf/black
25-
rev: 24.3.0
26-
hooks:
27-
- id: black
28-
args: [--config=pyproject.toml]
24+
- id: ruff-format
25+
args: [ --config=pyproject.toml ]
2926
- repo: https://github.com/codespell-project/codespell
3027
# Configuration for codespell is in pyproject.toml
3128
rev: v2.2.6

README.md

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ A tool to automatically create a Python project structure ready to release via G
44
It will also set up:
55
* A blank `README.md` file
66
* A `LICENSE` file
7-
* Formatting checks using [ruff](https://github.com/charliermarsh/ruff)
8-
* Autoformatting using [Black](https://black.readthedocs.io/en/stable/)
9-
* [Pre-commit hooks](https://pre-commit.com/)
7+
* [Pre-commit hooks](https://pre-commit.com/) to automate linting checks and formatting
108
* Automatic versioning using [setuptools_scm](https://github.com/pypa/setuptools_scm)
119
* A structure for automated tests using [pytest](https://docs.pytest.org/en/7.0.x/)
1210
* Automated formatting checks, testing and release using [GitHub actions](https://github.com/features/actions)
@@ -15,24 +13,25 @@ It will also set up:
1513
**Based on [cookiecutter-napari-plugin](https://github.com/napari/cookiecutter-napari-plugin)**
1614

1715
## Table of contents
18-
- [cookiecutter-python](#cookiecutter-python)
19-
- [Table of contents](#table-of-contents)
20-
- [Set up](#set-up)
21-
- [Make it a git repo](#make-it-a-git-repo)
22-
- [Add your modules and tests](#add-your-modules-and-tests)
23-
- [Add dependencies](#add-dependencies)
24-
- [Write tests](#write-tests)
25-
- [Before committing your changes](#before-committing-your-changes)
26-
- [Run the tests](#run-the-tests)
27-
- [Install your package locally](#install-your-package-locally)
28-
- [Pre-commit hooks](#pre-commit-hooks)
29-
- [Versioning](#versioning)
30-
- [Automated versioning](#automated-versioning)
31-
- [GitHub actions workflow](#github-actions-workflow)
32-
- [Documentation](#documentation)
33-
- [Building the documentation locally](#building-the-documentation-locally)
34-
- [Publishing the documentation](#publishing-the-documentation)
35-
- [Docstrings and API documentation](#docstrings-and-api-documentation)
16+
- [Table of contents](#table-of-contents)
17+
- [Set up](#set-up)
18+
- [Installing Cookiecutter](#installing-cookiecutter)
19+
- [Creating a Cookiecutter project](#creating-a-cookiecutter-project)
20+
- [Make it a git repo](#make-it-a-git-repo)
21+
- [Add your modules and tests](#add-your-modules-and-tests)
22+
- [Add dependencies](#add-dependencies)
23+
- [Write tests](#write-tests)
24+
- [Before committing your changes](#before-committing-your-changes)
25+
- [Run the tests](#run-the-tests)
26+
- [Install your package locally](#install-your-package-locally)
27+
- [Pre-commit hooks](#pre-commit-hooks)
28+
- [Versioning](#versioning)
29+
- [Automated versioning](#automated-versioning)
30+
- [GitHub actions workflow](#github-actions-workflow)
31+
- [Documentation](#documentation)
32+
- [Building the documentation locally](#building-the-documentation-locally)
33+
- [Publishing the documentation](#publishing-the-documentation)
34+
- [Docstrings and API documentation](#docstrings-and-api-documentation)
3635

3736
## Set up
3837

@@ -223,15 +222,19 @@ add_two_integers(1, 2)
223222

224223
Running `pre-commit install` will set up [pre-commit hooks](https://pre-commit.com/) to ensure the code is
225224
formatted correctly. Currently, these are:
226-
* [black](https://black.readthedocs.io/en/stable/) for code structure formatting (maximum line length set to 88)
225+
* [ruff](https://github.com/charliermarsh/ruff) does a number of jobs, including linting, auto-formatting code (with `ruff-format`), and sorting import statements.
227226
* [mypy](https://mypy.readthedocs.io/en/stable/index.html) a static type checker
228-
* [ruff](https://github.com/charliermarsh/ruff) does a number of jobs, including enforcing PEP8 and sorting imports
227+
* [check-manifest](https://github.com/mgedmin/check-manifest) to ensure that the right files are included in the pip package.
228+
* [codespell](https://github.com/codespell-project/codespell) to check for common misspellings.
229+
229230

230231
These will prevent code from being committed if any of these hooks fail. To run them individually:
231232
```bash
232-
ruff .
233-
black ./
233+
ruff check --fix # Lint all files in the current directory, and fix any fixable errors.
234+
ruff format # Format all files in the current directory.
234235
mypy -p my_awesome_software
236+
check-manifest
237+
codespell
235238
```
236239

237240
You can also execute all the hooks using `pre-commit run`. The best time to run this is after you have staged your changes, but before you commit them.

pyproject.toml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
[tool.black]
2-
target-version = ['py39', 'py310', 'py311']
3-
skip-string-normalization = false
4-
line-length = 88
5-
61
[tool.ruff]
72
line-length = 88
8-
exclude = ["__init__.py","build",".eggs"]
3+
exclude = ["__init__.py", "build", ".eggs"]
94
lint.select = ["I", "E", "F"]
105
fix = true
116

7+
[tool.ruff.format]
8+
docstring-code-format = true # Also format code in docstrings
9+
1210
[tool.codespell]
1311
# Ref: https://github.com/codespell-project/codespell#using-a-config-file
1412
skip = '.git'

tests/test_cookiecutter.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ def test_pyproject_toml(package_path_config_dict):
269269
"pytest-cov",
270270
"coverage",
271271
"tox",
272-
"black",
273272
"mypy",
274273
"pre-commit",
275274
"ruff",
@@ -295,7 +294,7 @@ def test_pyproject_toml(package_path_config_dict):
295294
project_toml["tool"]["pytest"]["ini_options"]["addopts"]
296295
== f"--cov={config_dict['module_name']}"
297296
)
298-
assert project_toml["tool"]["black"]
297+
assert project_toml["tool"]["ruff"]
299298

300299
assert "legacy_tox_ini" in project_toml["tool"]["tox"]
301300

{{cookiecutter.package_name}}/.pre-commit-config.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ repos:
2323
rev: v0.3.0
2424
hooks:
2525
- id: ruff
26-
- repo: https://github.com/psf/black
27-
rev: 24.2.0
28-
hooks:
29-
- id: black
26+
- id: ruff-format
3027
- repo: https://github.com/pre-commit/mirrors-mypy
3128
rev: v1.8.0
3229
hooks:

{{cookiecutter.package_name}}/pyproject.toml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ dev = [
6464
"pytest-cov",
6565
"coverage",
6666
"tox",
67-
"black",
6867
"mypy",
6968
"pre-commit",
7069
"ruff",
@@ -96,11 +95,6 @@ filterwarnings = [
9695
"error",
9796
]
9897

99-
[tool.black]
100-
target-version = ['py39', 'py310', 'py311']
101-
skip-string-normalization = false
102-
line-length = 88
103-
10498
[tool.setuptools_scm]
10599

106100
[tool.check-manifest]
@@ -126,10 +120,23 @@ ignore = [
126120

127121
[tool.ruff]
128122
line-length = 88
129-
exclude = ["__init__.py","build",".eggs"]
130-
lint.select = ["I", "E", "F"]
123+
exclude = ["__init__.py", "build", ".eggs"]
124+
lint.select = [
125+
"E", # pycodestyle errors
126+
"F", # Pyflakes
127+
"I", # isort
128+
# You can see what all the rules do here: https://docs.astral.sh/ruff/rules/
129+
# Some additional ruff rules that might be useful (uncomment to enable)
130+
#"UP", # pyupgrade
131+
#"B", # flake8 bugbear
132+
#"SIM", # flake8 simplify
133+
#"C90", # McCabe complexity
134+
]
131135
fix = true
132136

137+
[tool.ruff.format]
138+
docstring-code-format = true # Also format code in docstrings (e.g. examples)
139+
133140
[tool.tox]
134141
legacy_tox_ini = """
135142
[tox]

0 commit comments

Comments
 (0)