Skip to content

Commit 0606648

Browse files
Batalexlwasser
authored andcommitted
docs: Add ruff section in code style page
1 parent a8146a7 commit 0606648

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

package-structure-code/code-style-linting-format.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
77
* pyOpenSci requires authors to follow PEP 8 code format guidelines
88
* 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
1010
* You can also setup pre-commit hooks which will run code formatters locally
1111
each time you make a commit.
1212
* [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.
1314
```
1415

1516
Consistent code format and style is useful to both your package
@@ -50,6 +51,7 @@ Setting up a code format suite of tools will:
5051
- Ensure that format and style is consistent across your entire code-base.
5152
- Avoid lengthy discussions with contributors and other
5253
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
5355

5456
Many packages use a suite of tools to apply code format rules, taking
5557
the work out of manually implementing code format requirements.
@@ -92,7 +94,7 @@ some exceptions. A few examples of those exceptions are below:
9294

9395
- 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.
9496
- 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).
9698

9799
Blue addresses a few format decisions in Black that some maintainers do not like.
98100
[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
163165
all imports. This leaves you with one less thing to think about when cleaning
164166
up your code.
165167

166-
### Example application of isort
168+
#### Example application of isort
167169

168170
Code imports before `isort` is run:
169171

@@ -197,6 +199,46 @@ from stravalib import exc
197199
from stravalib import unithelper as uh
198200
```
199201

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+
200242
## How to use code formatter in your local workflow
201243

202244
### Linters, code formatters and your favorite coding tools

0 commit comments

Comments
 (0)