Skip to content

Commit 1466281

Browse files
committed
📝 Add ruff formatter
1 parent 1746f7b commit 1466281

File tree

5 files changed

+150
-4
lines changed

5 files changed

+150
-4
lines changed

docs/performance/perflint.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ perflint
66
========
77

88
`perflint <https://github.com/tonybaloney/perflint>`_ ist eine Erweiterung für
9-
`pylint <https://pylint.org/>`_ für Performance-Anti-Patternsr.
9+
`pylint <https://pylint.org/>`_ für Performance-Anti-Patterns.
1010

1111
Installation
1212
------------

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ Python Code Quality Authority
110110
`isort <https://github.com/PyCQA/isort>`_
111111
sortiert Python-Importe
112112

113-
`nbQA <https://github.com/nbQA-dev/nbQA>`_
113+
.. _nbqa:
114+
115+
`nbQA <https://github.com/nbQA-dev/nbQA>`__
114116
führt isort, pyupgrade, mypy, pylint, flake8 und mehr auf Jupyter Notebooks
115117
aus:
116118

docs/productive/qa/index.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Checker
4343
ist ein Wrapper um `PyFlakes <https://pypi.org/project/pyflakes/>`_,
4444
`pycodestyle <https://pypi.org/project/pycodestyle/>`_ und `McCabe
4545
<https://pypi.org/project/mccabe/>`_. Eine automatische Formatierung,
46-
:abbr:`(zum Beispiel)` mit :doc:`black`, ist jedoch noch bequemer.
46+
:abbr:`(zum Beispiel)` mit :doc:`ruff`, ist jedoch noch bequemer.
4747
:doc:`mypy`
4848
ist ein statischer Typ-Checker.
4949
:doc:`pytype`
@@ -80,6 +80,11 @@ Checker
8080
Formatter
8181
---------
8282

83+
:doc:`ruff`
84+
ist ein extrem schneller Python-Linter und Code-Formatierer, geschrieben in
85+
Rust, der :abbr:`u.a. (unter anderem)` die Regeln von :doc:`flake8`,
86+
:doc:`isort`, :doc:`black` und `Bandit <https://github.com/PyCQA/bandit>`_
87+
ausführen kann.
8388
:doc:`black`
8489
formatiert euren Code in ein schönes und deterministisches Format.
8590
:doc:`isort`
@@ -92,6 +97,7 @@ Formatter
9297
:titlesonly:
9398
:maxdepth: 0
9499

100+
ruff
95101
black
96102
isort
97103
prettier

docs/productive/qa/ruff.rst

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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/>`_ ist ein extrem schneller Python-Linter
9+
und Code-Formatierer, geschrieben in Rust, der :abbr:`u.a. (unter anderem)` die
10+
Regeln von :doc:`flake8`, :doc:`isort`, :doc:`/performance/perflint`,
11+
:doc:`black` und :ref:`Bandit <bandit>` ausführen kann. Insgesamt kann Ruff
12+
`über 800 Regeln <https://docs.astral.sh/ruff/rules/>`_ überprüfen.
13+
14+
Installation
15+
------------
16+
17+
.. code-block:: console
18+
19+
$ uv add --dev ruff
20+
21+
Überprüfen
22+
----------
23+
24+
Anschließend könnt ihr die Installation überprüfen mit
25+
26+
.. code-block:: console
27+
28+
$ uv run ruff check /PATH/TO/YOUR/SOURCE/FILE
29+
30+
Shell-Autovervollständigung
31+
---------------------------
32+
33+
Ruff unterstützt Autovervollständigung für die meisten Shells. Ein
34+
shellspezifisches Skript kann mit :samp:`uv run ruff generate-shell-completion
35+
{SHELL}` generiert werden, wobei :samp:`{SHELL}` entweder ``bash``, ``elvish``,
36+
``fig``, ``fish``, ``powershell`` oder ``zsh`` ist, :abbr:`z. B. (zum Beispiel)`
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+
Anschließend müssen dann die folgenden Zeilen in eure :file:`~/.zshrc`
51+
eingefügt werden, falls sie dort noch nicht vorhanden sind:
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+
Konfiguration
70+
-------------
71+
72+
Im Gegensatz zur Standardformatierung von :doc:`black` mit 88 Zeichen bevorzuge
73+
ich eine Zeilenlänge von 79 Zeichen. Hierfür könnt ihr in die
74+
:file:`pyproject.toml`-Datei folgendes eintragen:
75+
76+
.. code-block:: toml
77+
78+
[tool.ruff]
79+
line-length = 79
80+
81+
.. tip::
82+
Üblicherweise fügen wir ``ruff lint`` zunächst alle Regeln hinzu, bevor wir
83+
dann einzelne wieder ausschließen, also :abbr:`z. B. (zum Beispiel)`:
84+
85+
.. code-block:: toml
86+
87+
[tool.ruff.lint]
88+
select = ["ALL"]
89+
ignore = [
90+
"A", # Shaddowing is fine
91+
]
92+
93+
Ruff unterstützt auch auch Monorepos mit unterschiedlichen Regeln durch
94+
`hierarchische und kaskadierende Konfigurationen
95+
<https://docs.astral.sh/ruff/configuration/#config-file-discovery>`_.
96+
97+
.. seealso::
98+
Weitere Informationen zur Konfiguration von ruff in der
99+
:file:`pyproject.toml`-Datei erhaltet ihr in `Configuring Ruff
100+
<https://docs.astral.sh/ruff/configuration/>`_.
101+
102+
Integration
103+
-----------
104+
105+
Jupyter Notebooks
106+
~~~~~~~~~~~~~~~~~
107+
108+
Ruff unterstützt das Linting und Formatieren von :doc:`Jupyter Notebooks
109+
<jupyter-tutorial:notebook/index>` mit :ref:`nbQA <nbqa>`. Mit `jupyter-ruff
110+
<https://github.com/leotaku/jupyter-ruff>`_ könnt ihr ruff auch in euren
111+
Notebooks verwenden.
112+
113+
IDE
114+
~~~
115+
116+
Auch die Integration in andere Editoren wie Visual Studio Code, PyCharm oder Vim
117+
ist möglich, :abbr:`s. (siehe)` `Editor Integrations
118+
<https://docs.astral.sh/ruff/editors/>`_.
119+
120+
pre-commit
121+
~~~~~~~~~~
122+
123+
Ruff kann über `ruff-pre-commit <https://github.com/astral-sh/ruff-pre-commit>`_
124+
als :doc:`Pre-Commit-Hook </productive/git/advanced/hooks/pre-commit>` verwendet
125+
werden:
126+
127+
.. code-block:: yaml
128+
129+
repos:
130+
- repo: https://github.com/astral-sh/ruff-pre-commit
131+
rev: v0.12.10
132+
hooks:
133+
- id: ruff-check
134+
args: [--fix, --exit-non-zero-on-fix]
135+
exclude: docs
136+
- id: ruff-format

docs/productive/security.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,10 @@ Risiko: Mittel
202202
testen den Quellcode, bevor die Anwendung ausgeführt wird. Dies kann verhindern,
203203
dass bekannte Fehlerklassen versehentlich in die Codebasis eingeführt werden.
204204

205+
.. _bandit:
206+
205207
Um Schwachstellen zu überprüfen, könnt ihr `bandit
206-
<https://github.com/PyCQA/bandit>`_ verwenden, das ihr auch in eure
208+
<https://github.com/PyCQA/bandit>`__ verwenden, das ihr auch in eure
207209
:file:`.pre-commit-hooks.yaml` integrieren könnt:
208210

209211
.. code-block:: yaml

0 commit comments

Comments
 (0)