Skip to content

Commit 866b471

Browse files
committed
Adjust code quality posts
1 parent ec070d9 commit 866b471

File tree

2 files changed

+58
-46
lines changed

2 files changed

+58
-46
lines changed
Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: post
33
category: python
4-
title: How to use Black, Pylint and Mypy in GitHub Actions?
4+
title: How to use Ruff, Mypy, Black, Isort and Pytest in GitHub Actions?
55
permalink: /python/github-actions
66
---
77
<div class="wide-logos" markdown="1">
@@ -15,46 +15,48 @@ of quality.
1515
</div>
1616

1717
I use the following code quality checks:
18-
- *Black* to ensure code is formatted,
19-
- *Pylint* to disallow unused imports, and
20-
- *Mypy* for type checking.
2118

22-
The following Github Actions workflow will check your code when a Pull
23-
Request is created, catching problems before they're merged.
19+
- *Ruff* for linting
20+
- *Mypy* for type checking
21+
- *Black* to ensure code is formatted
22+
- *Isort* to ensure imports are grouped and sorted
23+
24+
The following Github Actions workflow will check your code when a pull request
25+
is created, catching problems before they're merged.
2426

2527
## How to add the Github Actions workflow
2628

2729
Add the following to your repository in `.github/workflows/code-quality.yml`.
2830

2931
```sh
30-
name: Checks
32+
name: Code Quality
3133
on: [pull_request]
3234

3335
jobs:
3436
build:
3537
runs-on: ubuntu-latest
36-
name: Checks
3738
steps:
3839
- uses: actions/checkout@v2
3940
- uses: actions/setup-python@v2
4041
with:
41-
python-version: 3.x
42+
python-version: "3.7"
4243
- run: pip install --upgrade pip
43-
- run: pip install "black<23" pylint==v3.0.0a3 mypy==v0.902
44-
- run: black --diff --check $(git ls-files '*.py')
45-
- run: pylint --disable=all --enable=unused-import $(git ls-files '*.py')
46-
- run: mypy --strict $(git ls-files '*.py')
44+
- run: pip install "ruff<1" "mypy<2" "black<23" "isort<6" pytest
45+
- run: ruff check .
46+
- run: mypy --strict .
47+
- run: black --check .
48+
- run: isort --check --profile black .
49+
- run: pytest .
4750
```
4851

4952
## Notes
5053

51-
- Be consistent with your Black version across your tooling. The formatting can
52-
change between versions, so what's considered "formatted" in one version may
53-
not be in another. Note as of 2022 Black has a
54+
- Be consistent with the Black version across your tooling.
55+
The formatting can change between versions, so what's considered "formatted"
56+
in one version may not be in another. Note as of 2022 Black has a
5457
[Stability Policy](https://black.readthedocs.io/en/stable/the_black_code_style/index.html)
55-
which states the formatting will not change in a calendar year. This is why I use `black<23`
56-
above -- we take all updates from this year but not next.
57-
- If you have an existing project with unformatted code, _format the entire
58-
codebase all at once_. Don't do it gradually.
58+
which states the formatting will not change in a calendar year.
59+
- If you have an existing project with un-blackened code, _format the entire
60+
project all at once_. Don't do it gradually. Do it in a single dedicated pull request.
5961

60-
See also: [How to use Black, Pylint and Mypy in Pre-commit?](/python/pre-commit)
62+
See also: [How to use Ruff, Mypy, Black and Isort in Pre-commit?](/python/pre-commit)
Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: post
33
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?
55
permalink: /python/pre-commit
66
redirect_from:
77
- /black
@@ -22,43 +22,52 @@ of quality.
2222

2323
I use the following code quality checks:
2424

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
2829

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.
3132

3233
## Install the Pre-commit hooks
3334

3435
Add the following `.pre-commit-config.yaml` file to the root of your
3536
repository.
3637

3738
```yaml
38-
fail_fast: true
39+
default_language_version:
40+
python: python3.7
3941

4042
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+
4156
- repo: https://github.com/ambv/black
42-
rev: 22.3.0
57+
rev: 22.8.0
4358
hooks:
4459
- id: black
45-
args: [--diff, --check]
60+
types: [python]
61+
args: [--check]
4662

4763
- repo: local
4864
hooks:
49-
- id: pylint
50-
name: pylint
51-
entry: pylint
65+
- id: isort
66+
name: isort
67+
entry: isort
5268
language: system
5369
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]
6271
```
6372
6473
Install [Pre-commit](https://pre-commit.com) and add the git hooks:
@@ -69,14 +78,15 @@ pre-commit install
6978

7079
## Notes
7180

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).
7484
- Be consistent with the Black version across your tooling.
7585
The formatting can change between versions, so what's considered "formatted"
7686
in one version may not be in another. Note as of 2022 Black has a
7787
[Stability Policy](https://black.readthedocs.io/en/stable/the_black_code_style/index.html)
7888
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.
8191

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

Comments
 (0)