Skip to content

Commit 93538ad

Browse files
committed
📝 Add ruff formatter
1 parent 48b75b3 commit 93538ad

File tree

4 files changed

+146
-2
lines changed

4 files changed

+146
-2
lines changed

docs/productive/git/advanced/hooks/hooks.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ Python Code Quality Authority
102102
`isort <https://github.com/PyCQA/isort>`_
103103
sorts Python imports
104104

105+
.. _nbqa:
106+
105107
`nbQA <https://github.com/nbQA-dev/nbQA>`_
106108
runs isort, pyupgrade, mypy, pylint, flake8 and more on Jupyter notebooks:
107109

docs/productive/qa/index.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Checker
4040
is a wrapper around `PyFlakes <https://pypi.org/project/pyflakes/>`_,
4141
`pycodestyle <https://pypi.org/project/pycodestyle/>`_ and `McCabe
4242
<https://pypi.org/project/mccabe/>`_. However, automatic formatting, for
43-
example with :doc:`black`, is even more convenient.
43+
example with :doc:`ruff`, is even more convenient.
4444
:doc:`mypy`
4545
is a static type checker.
4646
:doc:`pytype`
@@ -76,6 +76,10 @@ Checker
7676
Formatter
7777
---------
7878

79+
:doc:`ruff`
80+
is an extremely fast Python linter and code formatter written in Rust that
81+
can enforce the rules of :doc:`flake8`, :doc:`isort`, :doc:`black`, `Bandit
82+
<https://github.com/PyCQA/bandit>`_, and others.
7983
:doc:`black`
8084
formats your code in a nice and deterministic format.
8185
:doc:`isort`
@@ -88,6 +92,7 @@ Formatter
8892
:titlesonly:
8993
:maxdepth: 0
9094

95+
ruff
9196
black
9297
isort
9398
prettier

docs/productive/qa/ruff.rst

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
.. SPDX-FileCopyrightText: 2025 Veit Schiele
2+
..
3+
.. SPDX-License-Identifier: BSD-3-Clause
4+
5+
Ruff
6+
====
7+
8+
`Ruff <https://docs.astral.sh/ruff/>`_ is an extremely fast Python linter and
9+
code formatter written in Rust that can enforce the rules of :doc:`flake8`,
10+
:doc:`isort`, :doc:`/performance/perflint`, :doc:`black`, :ref:`Bandit
11+
<bandit>`, and others. In total, Ruff can `check over 800 rules
12+
<https://docs.astral.sh/ruff/rules/>`_.
13+
14+
Installation
15+
------------
16+
17+
.. code-block:: console
18+
19+
$ uv add --dev ruff
20+
21+
Check
22+
-----
23+
24+
You can then check the installation with
25+
26+
.. code-block:: console
27+
28+
$ uv run ruff check /PATH/TO/YOUR/SOURCE/FILE
29+
30+
Shell auto-completion
31+
---------------------
32+
33+
Ruff supports autocompletion for most shells. A shell-specific script can be
34+
generated with :samp:`uv run ruff generate-shell-completion {SHELL}`, where
35+
:samp:`{SHELL}` is either ``bash``, ``elvish``, ``fig``, ``fish``,
36+
``powershell`` or ``zsh``, for example
37+
38+
.. tab:: Bash
39+
40+
.. code-block:: console
41+
42+
$ ruff generate-shell-completion zsh >> ~/.bash_completion
43+
44+
.. tab:: Zsh
45+
46+
.. code-block:: console
47+
48+
% ruff generate-shell-completion zsh > ~/.zfunc/_ruff
49+
50+
Then, the following lines must be added to your :file:`~/.zshrc` file, if
51+
they are not already there:
52+
53+
.. code-block:: zsh
54+
55+
fpath+=~/.zfunc
56+
autoload -Uz compinit && compinit
57+
58+
.. tab:: Oh My Zsh
59+
60+
.. code-block:: console
61+
62+
% mkdir $ZSH_CUSTOM/plugins/ruff
63+
% ruff generate-shell-completion zsh > $ZSH_CUSTOM/plugins/ruff/_ruff
64+
65+
.. seealso::
66+
`Shell autocompletion
67+
<https://docs.astral.sh/ruff/configuration/#shell-autocompletion>`_
68+
69+
Configuration
70+
-------------
71+
72+
Unlike :doc:`black`’s default formatting of 88 characters, I prefer a line
73+
length of 79 characters. To do this, you can enter the following in the
74+
:file:`pyproject.toml` file:
75+
76+
.. code-block:: toml
77+
78+
[tool.ruff]
79+
line-length = 79
80+
81+
.. tip::
82+
Usually, we first add all rules to ``ruff lint`` before excluding individual
83+
ones, for example:
84+
85+
.. code-block:: toml
86+
87+
[tool.ruff.lint]
88+
select = ["ALL"]
89+
ignore = [
90+
"A", # Shaddowing is fine
91+
]
92+
93+
Ruff also supports monorepos with different rules through `hierarchical and
94+
cascading configurations
95+
<https://docs.astral.sh/ruff/configuration/#config-file-discovery>`_.
96+
97+
.. seealso::
98+
For more information on configuring ruff in the :file:`pyproject.toml` file,
99+
see `Configuring Ruff <https://docs.astral.sh/ruff/configuration/>`_.
100+
101+
Integration
102+
-----------
103+
104+
Jupyter Notebooks
105+
~~~~~~~~~~~~~~~~~
106+
107+
Ruff supports linting and formatting :doc:`Jupyter Notebooks
108+
<jupyter-tutorial:notebook/index>` with :ref:`nbQA <nbqa>`. With `jupyter-ruff,
109+
<https://github.com/leotaku/jupyter-ruff>`_ you can also use Ruff in your
110+
notebooks.
111+
112+
IDE
113+
~~~
114+
115+
Integration with other editors such as Visual Studio Code, PyCharm or Vim is
116+
also possible, see `Editor Integrations
117+
<https://docs.astral.sh/ruff/editors/>`_.
118+
119+
pre-commit
120+
~~~~~~~~~~
121+
122+
Ruff can be used as a :doc:`pre-commit hook
123+
</productive/git/advanced/hooks/pre-commit>` via `ruff-pre-commit
124+
<https://github.com/astral-sh/ruff-pre-commit>`_:
125+
126+
.. code-block:: yaml
127+
128+
repos:
129+
- repo: https://github.com/astral-sh/ruff-pre-commit
130+
rev: v0.12.10
131+
hooks:
132+
- id: ruff-check
133+
args: [--fix, --exit-non-zero-on-fix]
134+
exclude: docs
135+
- id: ruff-format

docs/productive/security.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,10 @@ Risk: Medium
184184
tests the source code before the application is executed. This can prevent known
185185
bug classes from being accidentally introduced into the code base.
186186

187+
.. _bandit:
188+
187189
To check for vulnerabilities, you can use `bandit
188-
<https://github.com/PyCQA/bandit>`_, which you can also integrate into your
190+
<https://github.com/PyCQA/bandit>`__, which you can also integrate into your
189191
:file:`.pre-commit-hooks.yaml`:
190192

191193
.. code-block:: yaml

0 commit comments

Comments
 (0)