1
1
---
2
2
layout : post
3
3
category : python
4
- title : How to use Black, Pylint and Mypy in Pre-commit?
4
+ title : How to use Ruff, Mypy, Black and Isort in Pre-commit?
5
5
permalink : /python/pre-commit
6
6
redirect_from :
7
7
- /black
@@ -22,43 +22,52 @@ of quality.
22
22
23
23
I use the following code quality checks:
24
24
25
- - * Black* to ensure code is formatted,
26
- - * Pylint* to disallow unused imports, and
27
- - * Mypy* for type checking.
25
+ - * Ruff* for linting
26
+ - * Mypy* for type checking
27
+ - * Black* to ensure code is formatted
28
+ - * Isort* to ensure imports are grouped and sorted
28
29
29
- These Pre -commit hooks will check your code when you try to commit, catching problems
30
- before they reach your repository.
30
+ These pre -commit hooks will check your code when you try to commit, catching
31
+ problems before they reach your repository.
31
32
32
33
## Install the Pre-commit hooks
33
34
34
35
Add the following ` .pre-commit-config.yaml ` file to the root of your
35
36
repository.
36
37
37
38
``` yaml
38
- fail_fast : true
39
+ default_language_version :
40
+ python : python3.7
39
41
40
42
repos :
43
+ - repo : https://github.com/astral-sh/ruff-pre-commit
44
+ rev : v0.1.8
45
+ hooks :
46
+ - id : ruff
47
+ types : [python]
48
+
49
+ - repo : https://github.com/pre-commit/mirrors-mypy
50
+ rev : v0.971
51
+ hooks :
52
+ - id : mypy
53
+ types : [python]
54
+ args : [--strict]
55
+
41
56
- repo : https://github.com/ambv/black
42
- rev : 22.3 .0
57
+ rev : 22.8 .0
43
58
hooks :
44
59
- id : black
45
- args : [--diff, --check]
60
+ types : [python]
61
+ args : [--check]
46
62
47
63
- repo : local
48
64
hooks :
49
- - id : pylint
50
- name : pylint
51
- entry : pylint
65
+ - id : isort
66
+ name : isort
67
+ entry : isort
52
68
language : system
53
69
types : [python]
54
- require_serial : true
55
-
56
- - repo : https://github.com/pre-commit/mirrors-mypy
57
- rev : v0.902
58
- hooks :
59
- - id : mypy
60
- exclude : ^tests/
61
- args : [--strict]
70
+ args : [--check,--profile=black]
62
71
` ` `
63
72
64
73
Install [Pre-commit](https://pre-commit.com) and add the git hooks:
@@ -69,14 +78,15 @@ pre-commit install
69
78
70
79
## Notes
71
80
72
- - Since Pylint needs to import modules and dependencies to work correctly, the
73
- hook only works with a local installation of pylint (in your environment).
81
+ - Isort needs to know about your project's dependencies, therefore the hook
82
+ only works with a local installation of isort, (i.e. it's installed in your
83
+ environment).
74
84
- Be consistent with the Black version across your tooling.
75
85
The formatting can change between versions, so what's considered "formatted"
76
86
in one version may not be in another. Note as of 2022 Black has a
77
87
[ Stability Policy] ( https://black.readthedocs.io/en/stable/the_black_code_style/index.html )
78
88
which states the formatting will not change in a calendar year.
79
- - If you have an existing project with unformatted code, _ format the entire
80
- codebase all at once_ . Don't do it gradually.
89
+ - If you have an existing project with un-blackened code, _ format the entire
90
+ project all at once_ . Don't do it gradually. Do it in a single dedicated pull request .
81
91
82
- See also: [ How to use Black, Pylint and Mypy in Github Actions?] ( /python/github-actions )
92
+ See also: [ How to use Ruff, Mypy, Black, Isort and Pytest in Github Actions?] ( /python/github-actions )
0 commit comments