Skip to content

Commit cdc5e2f

Browse files
Update tutorial to match revised Ruff defaults (#8066)
## Summary We don't enable E501 by default, but `line-length` is a useful example for configuration, so we now set `--extend-select` in the tutorial with a note to that effect. I've also updated all the outputs to match the latest CLI behavior, and changed the example from `List` to `Sequence` because `List` now spits out two diagnostics (one for the import, one for the usage), which IMO is confusing for beginners.
1 parent b5d3caf commit cdc5e2f

File tree

3 files changed

+54
-54
lines changed

3 files changed

+54
-54
lines changed

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,8 @@ linting command.
237237
isort, pyupgrade, and others. Regardless of the rule's origin, Ruff re-implements every rule in
238238
Rust as a first-party feature.
239239

240-
By default, Ruff enables Flake8's `E` and `F` rules. Ruff supports all rules from the `F` category,
241-
and a [subset](https://docs.astral.sh/ruff/rules/#error-e) of the `E` category, omitting those
242-
stylistic rules made obsolete by the use of a formatter, like
240+
By default, Ruff enables Flake8's `F` rules, along with a subset of the `E` rules, omitting any
241+
stylistic rules that overlap with the use of a formatter, like
243242
[Black](https://github.com/psf/black).
244243

245244
If you're just getting started with Ruff, **the default rule set is a great place to start**: it

docs/tutorial.md

+52-50
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ numbers
1616
Where `numbers.py` contains the following code:
1717

1818
```py
19-
from typing import List
19+
from typing import Iterable
2020

2121
import os
2222

2323

24-
def sum_even_numbers(numbers: List[int]) -> int:
25-
"""Given a list of integers, return the sum of all even numbers in the list."""
24+
def sum_even_numbers(numbers: Iterable[int]) -> int:
25+
"""Given a sequence of integers, return the sum of all even numbers in the sequence."""
2626
return sum(num for num in numbers if num % 2 == 0)
2727
```
2828

@@ -32,17 +32,17 @@ To start, we'll install Ruff through PyPI (or with your [preferred package manag
3232
> pip install ruff
3333
```
3434

35-
We can then run Ruff over our project via:
35+
We can then run Ruff over our project via `ruff check`:
3636

3737
```shell
3838
❯ ruff check .
3939
numbers/numbers.py:3:8: F401 [*] `os` imported but unused
4040
Found 1 error.
41-
[*] 1 potentially fixable with the --fix option.
41+
[*] 1 fixable with the `--fix` option.
4242
```
4343

4444
Ruff identified an unused import, which is a common error in Python code. Ruff considers this a
45-
"fixable" error, so we can resolve the issue automatically by running:
45+
"fixable" error, so we can resolve the issue automatically by running `ruff check --fix`:
4646

4747
```shell
4848
❯ ruff check --fix .
@@ -55,13 +55,13 @@ Running `git diff` shows the following:
5555
--- a/numbers/numbers.py
5656
+++ b/numbers/numbers.py
5757
@@ -1,7 +1,5 @@
58-
from typing import List
58+
from typing import Iterable
5959

6060
-import os
6161
-
6262

63-
def sum_even_numbers(numbers: List[int]) -> int:
64-
"""Given a list of integers, return the sum of all even numbers in the list."""
63+
def sum_even_numbers(numbers: Iterable[int]) -> int:
64+
"""Given a sequence of integers, return the sum of all even numbers in the sequence."""
6565
return sum(num for num in numbers if num % 2 == 0)
6666
```
6767

@@ -77,15 +77,19 @@ Let's create a `pyproject.toml` file in our project's root directory:
7777

7878
```toml
7979
[tool.ruff]
80-
# Decrease the maximum line length to 79 characters.
80+
# Add the `line-too-long` rule to the enforced rule set. By default, Ruff omits rules that
81+
# overlap with the use of a formatter, like Black, but we can override this behavior by
82+
# explicitly adding the rule.
83+
extend-select = ["E501"]
84+
# Set the maximum line length to 79 characters.
8185
line-length = 79
8286
```
8387

8488
Running Ruff again, we can see that it now enforces a line length of 79 characters:
8589

8690
```shell
8791
❯ ruff check .
88-
numbers/numbers.py:6:80: E501 Line too long (83 > 79 characters)
92+
numbers/numbers.py:5:80: E501 Line too long (90 > 79 characters)
8993
Found 1 error.
9094
```
9195

@@ -98,9 +102,10 @@ specifically, we'll want to make note of the minimum supported Python version:
98102
requires-python = ">=3.10"
99103

100104
[tool.ruff]
101-
# Decrease the maximum line length to 79 characters.
105+
# Add the `line-too-long` rule to the enforced rule set.
106+
extend-select = ["E501"]
107+
# Set the maximum line length to 79 characters.
102108
line-length = 79
103-
src = ["src"]
104109
```
105110

106111
### Rule Selection
@@ -109,45 +114,47 @@ Ruff supports [over 700 lint rules](rules.md) split across over 50 built-in plug
109114
determining the right set of rules will depend on your project's needs: some rules may be too
110115
strict, some are framework-specific, and so on.
111116

112-
By default, Ruff enforces the `E`- and `F`-prefixed rules, which correspond to those derived from
113-
pycodestyle and Pyflakes, respectively.
117+
By default, Ruff enables Flake8's `F` rules, along with a subset of the `E` rules, omitting any
118+
stylistic rules that overlap with the use of a formatter, like
119+
[Black](https://github.com/psf/black).
114120

115121
If you're introducing a linter for the first time, **the default rule set is a great place to
116122
start**: it's narrow and focused while catching a wide variety of common errors (like unused
117123
imports) with zero configuration.
118124

119125
If you're migrating to Ruff from another linter, you can enable rules that are equivalent to
120126
those enforced in your previous configuration. For example, if we want to enforce the pyupgrade
121-
rules, we can add the following to our `pyproject.toml`:
127+
rules, we can set our `pyproject.toml` to the following:
122128

123129
```toml
130+
[project]
131+
requires-python = ">=3.10"
132+
124133
[tool.ruff]
125-
select = [
126-
"E", # pycodestyle
127-
"F", # pyflakes
134+
extend-select = [
128135
"UP", # pyupgrade
129136
]
130137
```
131138

132139
If we run Ruff again, we'll see that it now enforces the pyupgrade rules. In particular, Ruff flags
133-
the use of `List` instead of its standard-library variant:
140+
the use of the deprecated `typing.Iterable` instead of `collections.abc.Iterable`:
134141

135142
```shell
136143
❯ ruff check .
137-
numbers/numbers.py:5:31: UP006 [*] Use `list` instead of `List` for type annotations
138-
numbers/numbers.py:6:80: E501 Line too long (83 > 79 characters)
139-
Found 2 errors.
140-
[*] 1 potentially fixable with the --fix option.
144+
numbers/numbers.py:1:1: UP035 [*] Import from `collections.abc` instead: `Iterable`
145+
Found 1 error.
146+
[*] 1 fixable with the `--fix` option.
141147
```
142148

143149
Over time, we may choose to enforce additional rules. For example, we may want to enforce that
144150
all functions have docstrings:
145151

146152
```toml
153+
[project]
154+
requires-python = ">=3.10"
155+
147156
[tool.ruff]
148-
select = [
149-
"E", # pycodestyle
150-
"F", # pyflakes
157+
extend-select = [
151158
"UP", # pyupgrade
152159
"D", # pydocstyle
153160
]
@@ -161,52 +168,49 @@ If we run Ruff again, we'll see that it now enforces the pydocstyle rules:
161168
```shell
162169
❯ ruff check .
163170
numbers/__init__.py:1:1: D104 Missing docstring in public package
171+
numbers/numbers.py:1:1: UP035 [*] Import from `collections.abc` instead: `Iterable`
164172
numbers/numbers.py:1:1: D100 Missing docstring in public module
165-
numbers/numbers.py:5:31: UP006 [*] Use `list` instead of `List` for type annotations
166-
numbers/numbers.py:5:80: E501 Line too long (83 > 79 characters)
167173
Found 3 errors.
168-
[*] 1 potentially fixable with the --fix option.
174+
[*] 1 fixable with the `--fix` option.
169175
```
170176

171177
### Ignoring Errors
172178

173179
Any lint rule can be ignored by adding a `# noqa` comment to the line in question. For example,
174-
let's ignore the `UP006` rule for the `List` import:
180+
let's ignore the `UP035` rule for the `Iterable` import:
175181

176182
```py
177-
from typing import List
183+
from typing import Iterable # noqa: UP035
178184

179185

180-
def sum_even_numbers(numbers: List[int]) -> int: # noqa: UP006
181-
"""Given a list of integers, return the sum of all even numbers in the list."""
186+
def sum_even_numbers(numbers: Iterable[int]) -> int:
187+
"""Given a sequence of integers, return the sum of all even numbers in the sequence."""
182188
return sum(num for num in numbers if num % 2 == 0)
183189
```
184190

185-
Running Ruff again, we'll see that it no longer flags the `List` import:
191+
Running Ruff again, we'll see that it no longer flags the `Iterable` import:
186192

187193
```shell
188194
❯ ruff check .
189195
numbers/__init__.py:1:1: D104 Missing docstring in public package
190196
numbers/numbers.py:1:1: D100 Missing docstring in public module
191-
numbers/numbers.py:5:80: E501 Line too long (83 > 79 characters)
192197
Found 3 errors.
193198
```
194199

195200
If we want to ignore a rule for an entire file, we can add a `# ruff: noqa` comment to the top of
196201
the file:
197202

198203
```py
199-
# ruff: noqa: UP006
200-
from typing import List
204+
# ruff: noqa: UP035
205+
from typing import Iterable
201206

202207

203-
def sum_even_numbers(numbers: List[int]) -> int:
204-
"""Given a list of integers, return the sum of all even numbers in the list."""
208+
def sum_even_numbers(numbers: Iterable[int]) -> int:
209+
"""Given a sequence of integers, return the sum of all even numbers in the sequence."""
205210
return sum(num for num in numbers if num % 2 == 0)
206211
```
207212

208-
For more in-depth instructions on ignoring errors,
209-
please see [_Configuration_](configuration.md#error-suppression).
213+
For more in-depth instructions on ignoring errors, see [_Configuration_](configuration.md#error-suppression).
210214

211215
### Adding Rules
212216

@@ -215,10 +219,10 @@ violations of that rule and instead focus on enforcing it going forward.
215219

216220
Ruff enables this workflow via the `--add-noqa` flag, which will adds a `# noqa` directive to each
217221
line based on its existing violations. We can combine `--add-noqa` with the `--select` command-line
218-
flag to add `# noqa` directives to all existing `UP006` violations:
222+
flag to add `# noqa` directives to all existing `UP035` violations:
219223

220224
```shell
221-
❯ ruff check --select UP006 --add-noqa .
225+
❯ ruff check --select UP035 --add-noqa .
222226
Added 1 noqa directive.
223227
```
224228

@@ -229,14 +233,12 @@ diff --git a/tutorial/src/main.py b/tutorial/src/main.py
229233
index b9291c5ca..b9f15b8c1 100644
230234
--- a/numbers/numbers.py
231235
+++ b/numbers/numbers.py
232-
@@ -1,6 +1,6 @@
233-
from typing import List
236+
@@ -1,4 +1,4 @@
237+
-from typing import Iterable
238+
+from typing import Iterable # noqa: UP035
234239

235240

236-
-def sum_even_numbers(numbers: List[int]) -> int:
237-
+def sum_even_numbers(numbers: List[int]) -> int: # noqa: UP006
238-
"""Given a list of integers, return the sum of all even numbers in the list."""
239-
return sum(num for num in numbers if num % 2 == 0)
241+
def sum_even_numbers(numbers: Iterable[int]) -> int:
240242
```
241243

242244
## Continuous Integration

scripts/generate_mkdocs.py

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class Section(NamedTuple):
4848
),
4949
"https://docs.astral.sh/ruff/installation/": "installation.md",
5050
"https://docs.astral.sh/ruff/rules/": "rules.md",
51-
"https://docs.astral.sh/ruff/rules/#error-e": "rules.md#error-e",
5251
"https://docs.astral.sh/ruff/settings/": "settings.md",
5352
}
5453

0 commit comments

Comments
 (0)