Skip to content

Commit af2a899

Browse files
committed
Use uv in tutorial
1 parent a365dc5 commit af2a899

File tree

3 files changed

+44
-38
lines changed

3 files changed

+44
-38
lines changed

docs/faq.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ Ruff is installable under any Python version from 3.7 onwards.
220220

221221
## Do I need to install Rust to use Ruff?
222222

223-
Nope! Ruff is available as [`ruff`](https://pypi.org/project/ruff/) on PyPI. We recommend installing Ruff with [uv](https://docs.astral.sh/uv/):
223+
Nope! Ruff is available as [`ruff`](https://pypi.org/project/ruff/) on PyPI. We recommend installing Ruff with [uv](https://docs.astral.sh/uv/),
224+
though it's also installable with `pip`, `pipx`, and a [variety of other package managers](installation.md).
224225

225226
```console
226227
$ # Install Ruff globally.

docs/installation.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
Ruff is available as [`ruff`](https://pypi.org/project/ruff/) on PyPI. We recommend installing Ruff with [uv](https://docs.astral.sh/uv/).
44

5-
To install Ruff globally:
6-
75
```console
86
$ # Install Ruff globally.
97
$ uv tool install ruff@latest

docs/tutorial.md

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,24 @@ your project. For a more detailed overview, see [_Configuring Ruff_](configurati
55

66
## Getting Started
77

8-
To start, we'll install Ruff from PyPI. You can install Ruff with [uv](https://docs.astral.sh/uv/), or
9-
your [preferred package manager](installation.md):
8+
To start, we'll initialize a project using [uv](https://docs.astral.sh/uv/):
109

1110
```console
12-
$ # With uv.
13-
$ uv tool install ruff@latest
14-
15-
$ # With pip.
16-
$ pip install ruff
17-
18-
$ # With pipx.
19-
$ pipx install ruff
11+
$ uv init numbers
2012
```
2113

22-
Let's then assume that our project structure looks like:
14+
This command creates a Python project with the following structure:
2315

2416
```text
2517
numbers
26-
├── __init__.py
27-
└── numbers.py
18+
├── pyproject.toml
19+
└── src
20+
└── numbers
21+
├── __init__.py
22+
└── py.typed
2823
```
2924

30-
...where `numbers.py` contains the following code:
25+
We'll then replace the contents of `src/numbers/__init__.py` with the following code:
3126

3227
```python
3328
from typing import Iterable
@@ -43,28 +38,40 @@ def sum_even_numbers(numbers: Iterable[int]) -> int:
4338
)
4439
```
4540

46-
We can run the Ruff linter over our project via `ruff check`:
41+
Next, we'll add Ruff to our project:
42+
43+
```console
44+
$ uv add --dev ruff
45+
```
46+
47+
We can then run the Ruff linter over our project via `uv run ruff check`:
4748

4849
```console
49-
$ ruff check
50-
numbers/numbers.py:3:8: F401 [*] `os` imported but unused
50+
$ uv run ruff check
51+
src/numbers/__init__.py:3:8: F401 [*] `os` imported but unused
5152
Found 1 error.
5253
[*] 1 fixable with the `--fix` option.
5354
```
5455

56+
!!! note
57+
58+
As an alternative to `uv run`, you can also run Ruff by activating the project's virtual
59+
environment (`source .venv/bin/active` on Linux and macOS, or `.venv\Scripts\activate` on
60+
Windows) and running `ruff check` directly.
61+
5562
Ruff identified an unused import, which is a common error in Python code. Ruff considers this a
5663
"fixable" error, so we can resolve the issue automatically by running `ruff check --fix`:
5764

5865
```console
59-
$ ruff check --fix
66+
$ uv run ruff check --fix
6067
Found 1 error (1 fixed, 0 remaining).
6168
```
6269

6370
Running `git diff` shows the following:
6471

6572
```diff
66-
--- a/numbers/numbers.py
67-
+++ b/numbers/numbers.py
73+
--- a/src/numbers/__init__.py
74+
+++ b/src/numbers/__init__.py
6875
@@ -1,7 +1,5 @@
6976
from typing import Iterable
7077

@@ -82,13 +89,13 @@ def sum_even_numbers(numbers: Iterable[int]) -> int:
8289
Note Ruff runs in the current directory by default, but you can pass specific paths to check:
8390

8491
```console
85-
$ ruff check numbers/numbers.py
92+
$ uv run ruff check src/numbers/__init__.py
8693
```
8794

8895
Now that our project is passing `ruff check`, we can run the Ruff formatter via `ruff format`:
8996

9097
```console
91-
$ ruff format
98+
$ uv run ruff format
9299
1 file reformatted
93100
```
94101

@@ -117,7 +124,7 @@ Ruff's behavior.
117124
To determine the appropriate settings for each Python file, Ruff looks for the first
118125
`pyproject.toml`, `ruff.toml`, or `.ruff.toml` file in the file's directory or any parent directory.
119126

120-
To configure Ruff, let's create a configuration file in our project's root directory:
127+
To configure Ruff, we'll add the following to the configuration file in our project's root directory:
121128

122129
=== "pyproject.toml"
123130

@@ -149,8 +156,8 @@ To configure Ruff, let's create a configuration file in our project's root direc
149156
Running Ruff again, we see that it now enforces a maximum line width, with a limit of 79:
150157

151158
```console
152-
$ ruff check
153-
numbers/numbers.py:5:80: E501 Line too long (90 > 79)
159+
$ uv run ruff check
160+
src/numbers/__init__.py:5:80: E501 Line too long (90 > 79)
154161
Found 1 error.
155162
```
156163

@@ -231,8 +238,8 @@ If we run Ruff again, we'll see that it now enforces the pyupgrade rules. In par
231238
the use of the deprecated `typing.Iterable` instead of `collections.abc.Iterable`:
232239

233240
```console
234-
$ ruff check
235-
numbers/numbers.py:1:1: UP035 [*] Import from `collections.abc` instead: `Iterable`
241+
$ uv run ruff check
242+
src/numbers/__init__.py:1:1: UP035 [*] Import from `collections.abc` instead: `Iterable`
236243
Found 1 error.
237244
[*] 1 fixable with the `--fix` option.
238245
```
@@ -274,10 +281,10 @@ all functions have docstrings:
274281
If we run Ruff again, we'll see that it now enforces the pydocstyle rules:
275282

276283
```console
277-
$ ruff check
284+
$ uv run ruff check
278285
numbers/__init__.py:1:1: D104 Missing docstring in public package
279-
numbers/numbers.py:1:1: UP035 [*] Import from `collections.abc` instead: `Iterable`
280-
numbers/numbers.py:1:1: D100 Missing docstring in public module
286+
src/numbers/__init__.py:1:1: UP035 [*] Import from `collections.abc` instead: `Iterable`
287+
src/numbers/__init__.py:1:1: D100 Missing docstring in public module
281288
Found 3 errors.
282289
[*] 1 fixable with the `--fix` option.
283290
```
@@ -299,9 +306,9 @@ def sum_even_numbers(numbers: Iterable[int]) -> int:
299306
Running `ruff check` again, we'll see that it no longer flags the `Iterable` import:
300307

301308
```console
302-
$ ruff check
309+
$ uv run ruff check
303310
numbers/__init__.py:1:1: D104 Missing docstring in public package
304-
numbers/numbers.py:1:1: D100 Missing docstring in public module
311+
src/numbers/__init__.py:1:1: D100 Missing docstring in public module
305312
Found 3 errors.
306313
```
307314

@@ -330,7 +337,7 @@ line based on its existing violations. We can combine `--add-noqa` with the `--s
330337
flag to add `# noqa` directives to all existing `UP035` violations:
331338

332339
```console
333-
$ ruff check --select UP035 --add-noqa .
340+
$ uv run ruff check --select UP035 --add-noqa .
334341
Added 1 noqa directive.
335342
```
336343

@@ -339,8 +346,8 @@ Running `git diff` shows the following:
339346
```diff
340347
diff --git a/tutorial/src/main.py b/tutorial/src/main.py
341348
index b9291c5ca..b9f15b8c1 100644
342-
--- a/numbers/numbers.py
343-
+++ b/numbers/numbers.py
349+
--- a/src/numbers/__init__.py
350+
+++ b/src/numbers/__init__.py
344351
@@ -1,4 +1,4 @@
345352
-from typing import Iterable
346353
+from typing import Iterable # noqa: UP035

0 commit comments

Comments
 (0)