|
6 | 6 |
|
7 | 7 | * pyOpenSci requires authors to follow PEP 8 code format guidelines
|
8 | 8 | * Setting up a code formatters like Black and isort will help you enforce PEP 8 style guidelines and also consistent, readable code format
|
9 |
| -* Some commonly used tools are: Black/Blue, Isort, flake8 |
| 9 | +* Some commonly used tools are: Black/Blue, Isort, flake8, ruff |
10 | 10 | * You can also setup pre-commit hooks which will run code formatters locally
|
11 | 11 | each time you make a commit.
|
12 | 12 | * [precommit.ci](https://pre-commit.ci/) is a bot that you can add to your GitHub repository. It will automagically apply code format to every PR using the tools specified in your pre-commit-config.yaml file. It can save significant time and make contributions easier for new contributors.
|
| 13 | +* Automation is good! By making code quality tools care of your code, you can focus on structural and high values tasks. |
13 | 14 | ```
|
14 | 15 |
|
15 | 16 | Consistent code format and style is useful to both your package
|
@@ -50,6 +51,7 @@ Setting up a code format suite of tools will:
|
50 | 51 | - Ensure that format and style is consistent across your entire code-base.
|
51 | 52 | - Avoid lengthy discussions with contributors and other
|
52 | 53 | maintainers about personalized code format preferences during reviews.
|
| 54 | +- Avoid pure visual edits in the code base so that code reviews focus on added value |
53 | 55 |
|
54 | 56 | Many packages use a suite of tools to apply code format rules, taking
|
55 | 57 | the work out of manually implementing code format requirements.
|
@@ -92,7 +94,7 @@ some exceptions. A few examples of those exceptions are below:
|
92 | 94 |
|
93 | 95 | - Black defaults to a line length of 88 (79 + 10%) rather than the 79 character `PEP 8` specification. However, line length is a setting can be manually overwritten in your Black configuration.
|
94 | 96 | - Black and Blue will not adjust line length in your comments or docstrings.
|
95 |
| -- Neither tool will review and fix import order (you need _isort_ to do that - see below). |
| 97 | +- Neither tool will review and fix import order (you need _isort_ or _ruff_ to do that - see below). |
96 | 98 |
|
97 | 99 | Blue addresses a few format decisions in Black that some maintainers do not like.
|
98 | 100 | [You can compare the differences here](https://blue.readthedocs.io/en/latest/#so-what-s-different) and decide which tool you prefer!
|
@@ -163,7 +165,7 @@ order. It will then modify your code, automatically reordering
|
163 | 165 | all imports. This leaves you with one less thing to think about when cleaning
|
164 | 166 | up your code.
|
165 | 167 |
|
166 |
| -### Example application of isort |
| 168 | +#### Example application of isort |
167 | 169 |
|
168 | 170 | Code imports before `isort` is run:
|
169 | 171 |
|
@@ -197,6 +199,46 @@ from stravalib import exc
|
197 | 199 | from stravalib import unithelper as uh
|
198 | 200 | ```
|
199 | 201 |
|
| 202 | +### ruff |
| 203 | + |
| 204 | +[ruff](https://beta.ruff.rs) is a new addition to the code quality ecosystem, |
| 205 | +gaining some traction since its release. |
| 206 | +`ruff` is a linter for Python, aiming to replace several tools behind a single interface. |
| 207 | +As such, `ruff` can be used instead of `flake8` and `isort`. |
| 208 | + |
| 209 | +`ruff` has some interesting features that distinguish it from other linters: |
| 210 | +- Linter configuration in `pyproject.toml` |
| 211 | +- Several hundred rules included, many of which are automatically fixable |
| 212 | +- Rules explanation, see [F403](https://beta.ruff.rs/docs/rules/undefined-local-with-import-star/) for an example |
| 213 | +- Fast execution time, makes a quick feedback loop possible even on large projects. |
| 214 | + |
| 215 | +Here is a simple configuration to get started with `ruff`: |
| 216 | + |
| 217 | +```toml |
| 218 | +# pyproject.toml |
| 219 | + |
| 220 | +[tool.ruff] |
| 221 | +select = [ |
| 222 | + "E", # pycodestyle errors |
| 223 | + "W", # pycodestyle warnings |
| 224 | + "F", # pyflakes. "E" + "W" + "F" + "C90" (mccabe complexity) is equivalent to flake8 |
| 225 | + "I", # isort |
| 226 | +] |
| 227 | +ignore = [ |
| 228 | + "E501", # line >79, handled by black/blue |
| 229 | +] |
| 230 | +``` |
| 231 | + |
| 232 | +Depending on your project, you might want to add the following to sort imports correctly: |
| 233 | + |
| 234 | +```toml |
| 235 | +# pyproject.toml |
| 236 | + |
| 237 | +[tool.ruff.isort] |
| 238 | +known-first-party = ["<package_folder>"] |
| 239 | +``` |
| 240 | + |
| 241 | + |
200 | 242 | ## How to use code formatter in your local workflow
|
201 | 243 |
|
202 | 244 | ### Linters, code formatters and your favorite coding tools
|
|
0 commit comments