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