|
| 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 |
0 commit comments