@@ -5,21 +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 through PyPI (or with your [ preferred package manager ] ( installation.md ) ):
8+ To start, we'll initialize a project using [ uv ] ( https://docs.astral.sh/uv/ ) :
99
1010``` console
11- $ pip install ruff
11+ $ uv init numbers
1212```
1313
14- Let's then assume that our project structure looks like :
14+ This command creates a Python project with the following structure :
1515
1616``` text
1717numbers
18- ├── __init__.py
19- └── numbers.py
18+ ├── pyproject.toml
19+ └── src
20+ └── numbers
21+ ├── __init__.py
22+ └── py.typed
2023```
2124
22- ...where ` numbers.py ` contains the following code:
25+ We'll then replace the contents of ` src/ numbers/__init__ .py` with the following code:
2326
2427``` python
2528from typing import Iterable
@@ -35,28 +38,40 @@ def sum_even_numbers(numbers: Iterable[int]) -> int:
3538 )
3639```
3740
38- We can run the Ruff linter over our project via ` ruff check ` :
41+ Next, we'll add Ruff to our project:
3942
4043``` console
41- $ ruff check
42- numbers/numbers.py:3:8: F401 [*] `os` imported but unused
44+ $ uv add --dev ruff
45+ ```
46+
47+ We can then run the Ruff linter over our project via ` uv run ruff check ` :
48+
49+ ``` console
50+ $ uv run ruff check
51+ src/numbers/__init__.py:3:8: F401 [*] `os` imported but unused
4352Found 1 error.
4453[*] 1 fixable with the `--fix` option.
4554```
4655
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+
4762Ruff identified an unused import, which is a common error in Python code. Ruff considers this a
4863"fixable" error, so we can resolve the issue automatically by running ` ruff check --fix ` :
4964
5065``` console
51- $ ruff check --fix
66+ $ uv run ruff check --fix
5267Found 1 error (1 fixed, 0 remaining).
5368```
5469
5570Running ` git diff ` shows the following:
5671
5772``` diff
58- --- a/numbers /numbers.py
59- +++ b/numbers /numbers.py
73+ --- a/src /numbers/__init__ .py
74+ +++ b/src /numbers/__init__ .py
6075@@ -1,7 +1,5 @@
6176 from typing import Iterable
6277
@@ -74,13 +89,13 @@ def sum_even_numbers(numbers: Iterable[int]) -> int:
7489Note Ruff runs in the current directory by default, but you can pass specific paths to check:
7590
7691``` console
77- $ ruff check numbers /numbers.py
92+ $ uv run ruff check src /numbers/__init__ .py
7893```
7994
8095Now that our project is passing ` ruff check ` , we can run the Ruff formatter via ` ruff format ` :
8196
8297``` console
83- $ ruff format
98+ $ uv run ruff format
84991 file reformatted
85100```
86101
@@ -109,7 +124,7 @@ Ruff's behavior.
109124To determine the appropriate settings for each Python file, Ruff looks for the first
110125` pyproject.toml ` , ` ruff.toml ` , or ` .ruff.toml ` file in the file's directory or any parent directory.
111126
112- 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:
113128
114129=== "pyproject.toml"
115130
@@ -141,8 +156,8 @@ To configure Ruff, let's create a configuration file in our project's root direc
141156Running Ruff again, we see that it now enforces a maximum line width, with a limit of 79:
142157
143158``` console
144- $ ruff check
145- 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)
146161Found 1 error.
147162```
148163
@@ -223,8 +238,8 @@ If we run Ruff again, we'll see that it now enforces the pyupgrade rules. In par
223238the use of the deprecated ` typing.Iterable ` instead of ` collections.abc.Iterable ` :
224239
225240``` console
226- $ ruff check
227- 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`
228243Found 1 error.
229244[*] 1 fixable with the `--fix` option.
230245```
@@ -266,10 +281,10 @@ all functions have docstrings:
266281If we run Ruff again, we'll see that it now enforces the pydocstyle rules:
267282
268283``` console
269- $ ruff check
284+ $ uv run ruff check
270285numbers/__init__.py:1:1: D104 Missing docstring in public package
271- numbers /numbers.py:1:1: UP035 [*] Import from `collections.abc` instead: `Iterable`
272- 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
273288Found 3 errors.
274289[*] 1 fixable with the `--fix` option.
275290```
@@ -291,9 +306,9 @@ def sum_even_numbers(numbers: Iterable[int]) -> int:
291306Running ` ruff check ` again, we'll see that it no longer flags the ` Iterable ` import:
292307
293308``` console
294- $ ruff check
309+ $ uv run ruff check
295310numbers/__init__.py:1:1: D104 Missing docstring in public package
296- numbers /numbers.py:1:1: D100 Missing docstring in public module
311+ src /numbers/__init__ .py:1:1: D100 Missing docstring in public module
297312Found 3 errors.
298313```
299314
@@ -322,7 +337,7 @@ line based on its existing violations. We can combine `--add-noqa` with the `--s
322337flag to add ` # noqa ` directives to all existing ` UP035 ` violations:
323338
324339``` console
325- $ ruff check --select UP035 --add-noqa .
340+ $ uv run ruff check --select UP035 --add-noqa .
326341Added 1 noqa directive.
327342```
328343
@@ -331,8 +346,8 @@ Running `git diff` shows the following:
331346``` diff
332347diff --git a/tutorial/src/main.py b/tutorial/src/main.py
333348index b9291c5ca..b9f15b8c1 100644
334- --- a/numbers /numbers.py
335- +++ b/numbers /numbers.py
349+ --- a/src /numbers/__init__ .py
350+ +++ b/src /numbers/__init__ .py
336351@@ -1,4 +1,4 @@
337352- from typing import Iterable
338353+ from typing import Iterable # noqa: UP035
0 commit comments