You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/clean_code.rst
+15-4
Original file line number
Diff line number
Diff line change
@@ -16,10 +16,16 @@ tools that can help us with this task. The ones we use are:
16
16
17
17
* `Black <https://github.com/psf/black>`_ which will reformat your code
18
18
in-place to conform to the PEP8 standard.
19
-
* `Flake8 <https://flake8.pycqa.org/en/latest/>`__ which is a *linter* that
19
+
* `Flake8 <https://flake8.pycqa.org/en/latest/>`_ which is a *linter* that
20
20
will check your code for errors and style violations, but not reformat it. For
21
21
example, for me it has identified code where I have unused variables or
22
22
scripts / functions that are too long.
23
+
* `isort <https://pycqa.github.io/isort/>`_ which will sort your imports
24
+
alphabetically and group them by type.
25
+
26
+
There are many alternatives for there tools. An increasingly popular alternative
27
+
is `ruff <https://docs.astral.sh/ruff//>`_, which is written in Rust and is meant
28
+
to replace Flake8, Black, and isort.
23
29
24
30
While you can use these tools manually, it is much more convenient to use them
25
31
as pre-commit hooks. This means that before you commit your code, these tools
@@ -34,7 +40,7 @@ A few files are needed to setup pre-commit hooks:
34
40
pre-commit hooks. It specifies which tools to use, and how to use them.
35
41
* `.flake8 <https://github.com/ebezzam/python-dev-tips/blob/main/.flake8>`_: This file contains the configuration for Flake8. It specifies
36
42
e.g. which errors to ignore, and which line length to use.
37
-
* `pyproject.toml <https://github.com/ebezzam/python-dev-tips/blob/main/pyproject.toml>`_: This file contains the configuration for Black. It
43
+
* `pyproject.toml <https://github.com/ebezzam/python-dev-tips/blob/main/pyproject.toml>`_: This file contains the configuration for Black and isort. It
38
44
specifies e.g. which line length to use.
39
45
40
46
You can then install the pre-commit hooks for your project by running the
@@ -43,13 +49,18 @@ following commands:
43
49
.. code:: bash
44
50
45
51
# inside virtual environment
46
-
(project_env) pip install pre-commit
47
-
(project_env) pip install black
52
+
# -- black, flake8, isort are in the dev group
53
+
(project_env) poetry install --with dev
54
+
55
+
# -- if not using Poetry
56
+
# (project_env) pip install pre-commit black flake8 isort
48
57
49
58
# Install git hooks
50
59
(project_env) pre-commit install
51
60
# pre-commit installed at .git/hooks/pre-commit
52
61
62
+
More pre-commit hooks are available provided by `Poetry <https://python-poetry.org/docs/pre-commit-hooks/>`_.
63
+
53
64
54
65
Avoiding long ``if-else`` statements with object instantiation
0 commit comments