Skip to content

Commit 3d31419

Browse files
docs: Adding section on Ruff in code style tools page (#592)
* docs: Adding section on Ruff in code style tools page * docs: Removing sections on Black, isort and Flake8 in code style tools page * docs: Updating based on review comments * docs: Harmonising documentation with changes made to code style tool page * docs: Updating required-standard based on review comment * Update doc/source/coding-style/required-standard.rst --------- Co-authored-by: Sébastien Morais <[email protected]>
1 parent c0c4b7d commit 3d31419

File tree

5 files changed

+101
-135
lines changed

5 files changed

+101
-135
lines changed

doc/source/coding-style/formatting-tools.rst

Lines changed: 54 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -11,92 +11,68 @@ Most of the tools presented can be configured using :ref:`the
1111
\`\`pyproject.toml\`\` file`. Avoiding dotfiles leads to a much
1212
cleaner root project directory.
1313

14-
Black
15-
-----
14+
Ruff
15+
----
1616

17-
`Black`_ is the most popular code formatter in the Python community because it is
18-
maintained by the Python Software Foundation. It allows for a minimum
19-
configuration to ensure that the Python code format looks almost the same across
20-
projects.
17+
`Ruff`_ is a Python linter and code formatter written in Rust. It aims to be
18+
orders of magnitude faster than alternative tools while integrating more
19+
functionality behind a single, common interface. Ruff can therefore be used
20+
to replace the previously preferred alternatives that were `Flake8`_
21+
(natively re-implementing its popular plugins), `Black`_ and `isort`_.
2122

22-
While `PEP 8`_ imposes a default line length of 79 characters, Black has
23-
a default line length of 88 characters.
23+
It is actively developed, used in major open-source projects, and offers the following
24+
features and advantages:
2425

25-
The minimum Black configuration for a PyAnsys project should look like this:
26+
- Can be installed via ``pip install ruff``
2627

27-
.. code-block:: toml
28-
29-
[tool.black]
30-
line-length = "<length>"
31-
32-
33-
The ``isort`` tool
34-
------------------
35-
36-
The goal of `isort`_ is to properly format ``import`` statements by making sure
37-
that they follow the standard order:
38-
39-
#. Library
40-
#. Third-party libraries
41-
#. Custom libraries
28+
- ``pyproject.toml`` support
4229

43-
When using `isort`_ with `Black`_, it is important to properly configure both
44-
tools so that no conflicts arise. To accomplish this, use the
45-
``--profile black`` flag in ``isort``.
30+
- Python 3.7 to 3.13 compatibility
4631

47-
.. code-block:: toml
48-
49-
[tool.isort]
50-
profile = "black"
51-
force_sort_within_sections = true
52-
line_length = "<length>"
53-
src_paths = ["doc", "src", "tests"]
54-
55-
Flake8
56-
------
32+
- Built-in caching, to avoid re-analyzing unchanged files
5733

58-
The goal of `Flake8`_ is to act as a `PEP 8`_ compliance checker. Again, if
59-
this tool is being used with `Black`_, it is important to make sure that no
60-
conflicts arise.
34+
- Over 800 built-in rules
6135

62-
The following configuration is the minimum one to set up Flake8 together with
63-
Black.
36+
- Editor integrations for VS Code or PyCharm
6437

65-
The configuration for Flake8 must be specified in a ``.flake8`` file.
38+
A minimum Ruff configuration for a PyAnsys project (to be included in the ``pyproject.toml``)
39+
may look like this:
6640

6741
.. code-block:: toml
6842
69-
[flake8]
70-
max-line-length = 88
71-
extend-ignore = 'E203'
72-
73-
Flake8 has many options that can be set within the configuration file.
74-
For more information, see `Full Listing of Options and Their Descriptions
75-
<https://flake8.pycqa.org/en/latest/user/options.html>`__ in the Flake8
76-
documentation.
77-
78-
The example configuration defines these options:
79-
80-
- ``exclude``
81-
Subdirectories and files to exclude when checking.
82-
83-
- ``select``
84-
Sequence of error codes that Flake8 is to report errors
85-
for. The set in the preceding configuration is a basic set of errors
86-
for checking and is not an exhaustive list. For more information, see
87-
`Error/Violation Codes <https://flake8.pycqa.org/en/3.9.2/user/error-codes.html>`__
88-
in the Flake8 documentation.
89-
90-
- ``count``
91-
Total number of errors to print when checking ends.
92-
93-
- ``max-complexity``
94-
Maximum allowed McCabe complexity value for a block of code.
95-
The value of 10 was chosen because it is a common default.
96-
97-
- ``statistics``
98-
Number of occurrences of each error or warning code
99-
to print as a report when checking ends.
43+
[tool.ruff]
44+
line-length = 100
45+
fix = true
46+
47+
[tool.ruff.format]
48+
quote-style = "double"
49+
indent-style = "space"
50+
51+
[tool.ruff.lint]
52+
select = [
53+
"E", # pycodestyle, see https://docs.astral.sh/ruff/rules/#pycodestyle-e-w
54+
"D", # pydocstyle, see https://docs.astral.sh/ruff/rules/#pydocstyle-d
55+
"F", # pyflakes, see https://docs.astral.sh/ruff/rules/#pyflakes-f
56+
"I", # isort, see https://docs.astral.sh/ruff/rules/#isort-i
57+
"N", # pep8-naming, see https://docs.astral.sh/ruff/rules/#pep8-naming-n
58+
"PTH", # flake8-use-pathlib, https://docs.astral.sh/ruff/rules/#flake8-use-pathlib-pth
59+
"TD", # flake8-todos, https://docs.astral.sh/ruff/rules/#flake8-todos-td
60+
]
61+
ignore = [
62+
"TD003", # Missing issue link in TODOs comment
63+
]
64+
65+
[tool.ruff.lint.pydocstyle]
66+
convention = "numpy"
67+
68+
[tool.ruff.lint.isort]
69+
combine-as-imports = true
70+
force-sort-within-sections = true
71+
72+
Linting and formatting rules shall be added step by step when migrating a project to Ruff,
73+
gradually resolving the triggered errors. For more information about configuring Ruff, as
74+
well as a complete description of the available rules and settings, please refer to the
75+
`tool's documentation <https://docs.astral.sh/ruff/configuration/>`__.
10076

10177

10278
The ``Add-license-headers`` pre-commit hook
@@ -151,20 +127,11 @@ configuration that includes both code and documentation formatting tools.
151127
152128
repos:
153129
154-
- repo: https://github.com/psf/black
155-
rev: X.Y.Z
156-
hooks:
157-
- id: black
158-
159-
- repo: https://github.com/pycqa/isort
160-
rev: X.Y.Z
161-
hooks:
162-
- id: isort
163-
164-
- repo: https://github.com/PyCQA/flake8
165-
rev: X.Y.Z
130+
- repo: https://github.com/astral-sh/ruff-pre-commit
131+
rev: vX.Y.Z
166132
hooks:
167-
- id: flake8
133+
- id: ruff
134+
- id: ruff-format
168135
169136
- repo: https://github.com/codespell-project/codespell
170137
rev: vX.Y.Z

doc/source/coding-style/required-standard.rst

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,33 @@ Required ``pyproject.toml`` file configuration
1313

1414
.. code-block:: toml
1515
16-
[tool.black]
17-
line-length = "<length>"
18-
19-
[tool.isort]
20-
profile = "black"
21-
force_sort_within_sections = true
22-
line_length = "<length>"
23-
src_paths = ["doc", "src", "tests"]
16+
[tool.ruff]
17+
line-length = 100
18+
fix = true
19+
20+
[tool.ruff.format]
21+
quote-style = "double"
22+
indent-style = "space"
23+
24+
[tool.ruff.lint]
25+
select = [
26+
"E", # pycodestyle, see https://docs.astral.sh/ruff/rules/#pycodestyle-e-w
27+
"D", # pydocstyle, see https://docs.astral.sh/ruff/rules/#pydocstyle-d
28+
"F", # pyflakes, see https://docs.astral.sh/ruff/rules/#pyflakes-f
29+
"I", # isort, see https://docs.astral.sh/ruff/rules/#isort-i
30+
"N", # pep8-naming, see https://docs.astral.sh/ruff/rules/#pep8-naming-n
31+
"PTH", # flake8-use-pathlib, https://docs.astral.sh/ruff/rules/#flake8-use-pathlib-pth
32+
"TD", # flake8-todos, https://docs.astral.sh/ruff/rules/#flake8-todos-td
33+
]
34+
ignore = []
35+
36+
[tool.ruff.lint.pydocstyle]
37+
convention = "numpy"
2438
39+
[tool.ruff.lint.isort]
40+
combine-as-imports = true
41+
force-sort-within-sections = true
42+
2543
[tool.coverage.run]
2644
source = ["ansys.<product>"]
2745
@@ -34,17 +52,6 @@ Required ``pyproject.toml`` file configuration
3452
[tool.pydocstyle]
3553
convention = "numpy"
3654
37-
Required flake8 configuration
38-
-----------------------------
39-
40-
The following ``.flake8`` file is also required:
41-
42-
.. code-block:: toml
43-
44-
[flake8]
45-
max-line-length = 88
46-
extend-ignore = 'E203'
47-
4855
Required ``pre-commit`` configuration
4956
-------------------------------------
5057

@@ -54,22 +61,13 @@ You can take advantage of `pre-commit`_ by including a
5461
.. code-block:: yaml
5562
5663
repos:
57-
58-
- repo: https://github.com/psf/black
59-
rev: X.Y.Z
60-
hooks:
61-
- id: black
62-
63-
- repo: https://github.com/pycqa/isort
64-
rev: X.Y.Z
65-
hooks:
66-
- id: isort
6764
68-
- repo: https://github.com/PyCQA/flake8
69-
rev: X.Y.Z
65+
- repo: https://github.com/astral-sh/ruff-pre-commit
66+
rev: vX.Y.Z
7067
hooks:
71-
- id: flake8
72-
68+
- id: ruff
69+
- id: ruff-format
70+
7371
- repo: https://github.com/codespell-project/codespell
7472
rev: vX.Y.Z
7573
hooks:

doc/source/content-writing/content-how-tos/create-PR.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ Run pre-commit locally
2828

2929
`pre-commit <pre-commit_>`_ is a tool for ensuring that all the changes that you make to
3030
files in your project successfully pass all checks run by the code style tools that are
31-
configured as part of the CI/CD process. These tools, which typically include `Black <Black_>`_,
32-
`isort <isort_>`_, and `Flake8 <Flake8_>`_, analyze, format, review, and improve
33-
code quality and security. For more information on the code style tools most commonly
34-
configured for use in PyAnsys projects, see :ref:`code_style_tools`.
31+
configured as part of the CI/CD process. These tools, which typically include `Ruff`_,
32+
analyze, format, review, and improve code quality and security. For more information on
33+
the code style tools most commonly configured for use in PyAnsys projects,
34+
see :ref:`code_style_tools`.
3535

3636
Because you do not want the **Code style** check for your PyAnsys project to fail
3737
when you create or push changes to a PR, you want to periodically run ``pre-commit``

doc/source/content-writing/content-how-tos/resolve-issues-causing-check-failures.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ Actions that you might take include these:
9999
Resolve too long line lengths and broken links
100100
----------------------------------------------
101101

102-
In PyAnsys projects, `Flake8 <Flake8_>`_ is a code style tool in the CI/CD process
103-
that checks the quality of the Python code. When you run ``pre-commit`` locally,
104-
Flake8 is one of the tools that it is configured to run. If Flake8 finds a line in a
105-
Python file that is too long, it raises an error. Providing that this line is a
106-
docstring or message string, you can manually change it in the PY file.
102+
In PyAnsys projects, `Ruff`_ is a code style tool in the CI/CD process that checks the
103+
quality of the Python code. When you run ``pre-commit`` locally, Ruff is one of the tools
104+
that it is configured to run. If Ruff finds a line in a Python file that is too long,
105+
it raises an error. Providing that this line is a docstring or message string, you can
106+
manually change it in the PY file.
107107

108108
Sometimes, however, the line that is too long is for a URL added to the ``linkcheck_ignore``
109109
variable in the Sphinx configuration (``doc/source/conf.py``) file. Here is an example of how
@@ -150,23 +150,23 @@ Here is what adding these lines looks like:
150150
"38-comments-and-docstrings",
151151
]
152152
153-
If you committed the preceding changes, Sphinx would no longer find any broken links. However, Flake8
153+
If you committed the preceding changes, Sphinx would no longer find any broken links. However, Ruff
154154
would throw line length errors for the two lines that define the items for the ``linkcheck_ignore`` variable
155155
in the Sphinx :file:`config.py` file. Because you cannot modify the length of these lines, you must follow
156-
each of these URLs (and any comment about it) with a space and then ``# noqa: 501``.
156+
each of these URLs (and any comment about it) with a space and then ``# noqa: E501``.
157157

158-
You can scroll to the end of these lines to see how they now conclude with ``# noqa: 501``:
158+
You can scroll to the end of these lines to see how they now conclude with ``# noqa: E501``:
159159

160160
.. code::
161161
162162
# Linkcheck ignore too long lines
163163
164164
linkcheck_ignore = [
165-
"https://myapps.microsoft.com/signin/8f67c59b-83ac-4318-ae96-f0588382ddc0?tenantId=34c6ce67-15b8-4eff-80e9-52da8be89706", # Join Ansys GitHub account # noqa: 501
166-
"https://myapps.microsoft.com/signin/42c0fa04-03f2-4407-865e-103af6973dae?tenantId=34c6ce67-15b8-4eff-80e9-52da8be89706", # Join Ansys internal GitHub account # noqa: 501
165+
"https://myapps.microsoft.com/signin/8f67c59b-83ac-4318-ae96-f0588382ddc0?tenantId=34c6ce67-15b8-4eff-80e9-52da8be89706", # Join Ansys GitHub account # noqa: E501
166+
"https://myapps.microsoft.com/signin/42c0fa04-03f2-4407-865e-103af6973dae?tenantId=34c6ce67-15b8-4eff-80e9-52da8be89706", # Join Ansys internal GitHub account # noqa: E501
167167
]
168168
169-
When you commit these changes, Flake sees the ``# noqa: 501`` comments at the end of these lines
169+
When you commit these changes, Ruff sees the ``# noqa: E501`` comments at the end of these lines
170170
and knows to ignore their long line lengths.
171171

172172
.. _resolve_mismatched_message_strings:

doc/source/links.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
.. _numpy: https://numpy.org/
8383
.. _pandas: https://pandas.pydata.org/
8484
.. _pre-commit: https://pre-commit.com/
85+
.. _Ruff: https://docs.astral.sh/ruff/
8586
.. _scipy: https://scipy.org/
8687
.. _tox: https://tox.wiki/en/latest/
8788
.. _tox_repo: https://github.com/tox-dev/tox

0 commit comments

Comments
 (0)