Skip to content

Commit a41476a

Browse files
Merge pull request #395 from HackSoftware/update-python
[Housekeeping] Update Python & third party packages. Add `ruff`.
2 parents b94df65 + 1090f60 commit a41476a

File tree

16 files changed

+182
-149
lines changed

16 files changed

+182
-149
lines changed

.github/workflows/django.yml

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ jobs:
77
- uses: actions/checkout@v3
88
- name: Build docker
99
run: docker compose build
10-
- name: Run isort
11-
run: docker compose run django isort styleguide_example/ --check
12-
- name: Run black
13-
run: docker compose run django black styleguide_example/ --check
14-
- name: Run flake8
15-
run: docker compose run django flake8
10+
- name: Run ruff
11+
run: docker compose run django ruff check styleguide_example/
1612
- name: Run mypy
1713
run: docker compose run django mypy --config mypy.ini styleguide_example/
1814
- name: Run tests
@@ -50,12 +46,8 @@ jobs:
5046
key: python-${{ hashFiles('requirements/local.txt') }}-${{ hashFiles('requirements/base.txt') }}
5147
- name: Install dependencies
5248
run: pip install -r requirements/local.txt
53-
- name: Run isort
54-
uses: isort/isort-action@master
55-
- name: Run black
56-
uses: psf/black@stable
57-
- name: Run flake8
58-
run: flake8
49+
- name: Run ruff
50+
run: ruff check .
5951
- name: Type check
6052
run: mypy --config mypy.ini styleguide_example/
6153
- name: Run tests

.pre-commit-config.yaml

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1+
default_language_version:
2+
python: python3.12.0
13
repos:
2-
- repo: https://github.com/pycqa/flake8
3-
rev: 6.0.0
4-
hooks:
5-
- id: flake8
6-
name: flake8
7-
8-
- repo: https://github.com/pycqa/isort
9-
rev: 5.12.0
10-
hooks:
11-
- id: isort
12-
name: isort
13-
args: [--check]
14-
15-
- repo: https://github.com/psf/black
16-
rev: 23.1.0
17-
hooks:
18-
- id: black
19-
name: black
20-
args: [--check]
4+
- repo: https://github.com/astral-sh/ruff-pre-commit
5+
# Ruff version.
6+
rev: v0.2.2
7+
hooks:
8+
# Run the linter.
9+
- id: ruff
10+
args: [ --fix ]
11+
# Run the formatter.
12+
- id: ruff-format

README.md

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -388,61 +388,33 @@ _Coming soon_
388388

389389
In all our Django projects we use:
390390

391-
- [flake8](https://flake8.pycqa.org/en/latest/) - a linter that ensures we follow the PEP8 conventions.
392-
- [black](https://github.com/psf/black) - a code formatter that ensures we have the same code style everywhere.
393-
- [isort](https://github.com/PyCQA/isort) - a code formatter that ensures we have the same import style everywhere.
391+
- [ruff](https://docs.astral.sh/ruff/) - an extremely fast Python linter and code formatter, written in Rust.
394392
- [pre-commit](https://pre-commit.com/) - a tool that triggers the linters before each commit.
395393

396394
To make sure all of the above tools work in symbiosis, you'd need to add some configuration:
397395

398396
1. Add `.pre-commit-config.yaml` file to the root of your project. There you can add the instructions for `pre-commit`
399-
2. Add `pyproject.toml` file to the root of your project. There you can add the `black` config. **NOTE:** `black` [does not respect any other config files.](https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html)
400-
3. Add the following to `setup.cfg` for the `isort` config:
401-
402-
```
403-
[isort]
404-
profile = black
405-
```
406-
407-
This will tell `isort` to follow the `black` guidelines.
408-
409-
```
410-
[isort]
411-
filter_files = true
412-
skip_glob = */migrations/*
413-
```
414-
415-
This will tell `pre-commit` to respect the `isort` config.
416-
417-
4. You can add a custom `flake8` configuration to `setup.cfg` as well. We usually have the following config in all our projects:
418-
419-
```
420-
[flake8]
421-
max-line-length = 120
422-
extend-ignore = E203
423-
exclude =
424-
.git,
425-
__pycache__,
426-
*/migrations/*
427-
```
428-
429-
5. Make sure the linters are run against each PR on your CI. This is the config you need if you use GH actions:
397+
2. Add `pyproject.toml` file to the root of your project. There you can add the `ruff` config.
398+
3. Make sure the linters are run against each PR on your CI. This is the config you need if you use GH actions:
430399

400+
- If you are running it as a separate step in the build process:
431401
```
432402
build:
433403
runs-on: ubuntu-latest
434404
steps:
435-
- name: Run isort
436-
uses: isort/isort-action@master
437-
- name: Run black
438-
uses: psf/black@stable
439-
- name: Run flake8
440-
run: flake8
405+
- name: Run ruff
406+
uses: chartboost/ruff-action@v1
407+
```
408+
409+
- If you would like to run it as a part of another step, which has already ran the package installation commands:
410+
```
411+
- name: Run ruff
412+
run: ruff check .
441413
```
442414

443-
6. Last but not least, we highly recommend you to setup you editor to run `black` and `isort` every time you save a new Python file.
415+
4. Last but not least, we highly recommend you to setup you editor to run `ruff` every time you save a new Python file.
444416

445417
In order to test if your local setup is up to date, you can either:
446418

447419
1. Try making a commit, to see if `pre-commit` is going to be triggered.
448-
1. Or run `black --check .` and `isort --check .` in the project root directory.
420+
2. Or run `ruff check .` in the project root directory.

config/django/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import os
1414

15-
from config.env import BASE_DIR, APPS_DIR, env
15+
from config.env import APPS_DIR, BASE_DIR, env
1616

1717
env.read_env(os.path.join(BASE_DIR, ".env"))
1818

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "3.9"
1+
version: "3.12.0"
22

33
services:
44
db:

docker/local.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This docker file is used for local development via docker-compose
22
# Creating image based on official python3 image
3-
FROM python:3.10.8
3+
FROM python:3.12.0
44

55
# Fix python printing
66
ENV PYTHONUNBUFFERED 1

docker/production.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This docker file is used for production
22
# Creating image based on official python3 image
3-
FROM python:3.10.8
3+
FROM python:3.12.0
44

55
# Installing all python dependencies
66
ADD requirements/ requirements/

pyproject.toml

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,66 @@
1-
[tool.black]
1+
[tool.ruff]
2+
# Exclude a variety of commonly ignored directories.
3+
exclude = [
4+
"migrations",
5+
".bzr",
6+
".direnv",
7+
".eggs",
8+
".git",
9+
".git-rewrite",
10+
".hg",
11+
".ipynb_checkpoints",
12+
".mypy_cache",
13+
".nox",
14+
".pants.d",
15+
".pyenv",
16+
".pytest_cache",
17+
".pytype",
18+
".ruff_cache",
19+
".svn",
20+
".tox",
21+
".venv",
22+
".vscode",
23+
"__pypackages__",
24+
"_build",
25+
"buck-out",
26+
"build",
27+
"dist",
28+
"node_modules",
29+
"site-packages",
30+
"venv",
31+
]
32+
33+
# Same as Black.
234
line-length = 120
3-
# By default, `black` will ignore skip configuration when paths are explicitly provided.
4-
# In order for `pre-commit` to respect this configuration, `force-exclude` needs to be explicitly set.
5-
force-exclude = 'migrations'
6-
7-
[tool.isort]
8-
# https://black.readthedocs.io/en/stable/guides/using_black_with_other_tools.html#isort
9-
profile = "black"
10-
# By default, `isort` will ignore skip configuration when paths are explicitly provided.
11-
# In order for `pre-commit` to respect this configuration, `filter_files` needs to be set to true.
12-
# https://jugmac00.github.io/blog/isort-and-pre-commit-a-friendship-with-obstacles/
13-
filter_files = true
14-
skip_glob = ["*/migrations/*", "config/*"]
35+
indent-width = 4
36+
37+
# Assume Python 3.12
38+
target-version = "py312"
39+
40+
[tool.ruff.lint]
41+
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
42+
select = ["E4", "E7", "E9", "F", "I"]
43+
ignore = ["E722"]
44+
45+
# Allow fix for all enabled rules (when `--fix`) is provided.
46+
fixable = ["ALL"]
47+
unfixable = []
48+
49+
# Allow unused variables when underscore-prefixed.
50+
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
51+
52+
[tool.ruff.format]
53+
# Like Black, use double quotes for strings.
54+
quote-style = "double"
55+
56+
# Like Black, indent with spaces, rather than tabs.
57+
indent-style = "space"
58+
59+
# Like Black, respect magic trailing commas.
60+
skip-magic-trailing-comma = false
61+
62+
# Like Black, automatically detect the appropriate line ending.
63+
line-ending = "auto"
64+
65+
[tool.ruff.lint.isort]
66+
case-sensitive = true

requirements/base.txt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
Django==4.1.9
2-
django-environ==0.9.0
3-
psycopg2==2.9.5
1+
Django==4.2.10
2+
django-environ==0.11.2
3+
psycopg2==2.9.9
44
djangorestframework==3.14.0
55

6-
celery==5.2.7
7-
django-celery-results==2.4.0
8-
django-celery-beat==2.4.0
6+
celery==5.3.6
7+
django-celery-results==2.5.1
8+
django-celery-beat==2.5.0
99

1010
whitenoise==6.5.0
1111

12-
django-filter==23.2
13-
django-extensions==3.2.1
14-
django-cors-headers==4.1.0
15-
django-storages==1.13.1
12+
django-filter==23.5
13+
django-extensions==3.2.3
14+
django-cors-headers==4.3.1
15+
django-storages==1.14.2
1616

1717
drf-jwt==1.19.2
1818

19-
boto3==1.26.0
20-
attrs==22.1.0
19+
boto3==1.34.44
20+
attrs==23.2.0
2121

22-
gunicorn==20.1.0
23-
sentry-sdk==1.14.0
22+
gunicorn==21.2.0
23+
sentry-sdk==1.40.5
2424

2525
requests==2.31.0
2626

@@ -30,4 +30,4 @@ google-auth-httplib2==0.1.0
3030
google-auth-oauthlib==1.0.0
3131

3232
pyotp==2.8.0
33-
qrcode==7.4.2
33+
qrcode==7.4.2

requirements/local.txt

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
-r base.txt
22

3-
pytest==7.4.0
4-
pytest-django==4.5.2
3+
pytest==8.0.1
4+
pytest-django==4.8.0
55

6-
factory-boy==3.2.1
7-
Faker==18.6.0
6+
factory-boy==3.3.0
7+
Faker==23.2.1
88

99
ipdb==0.13.13
1010
ipython==8.10.0
1111

12-
django-debug-toolbar==3.8.1
12+
django-debug-toolbar==4.3.0
1313

14-
mypy==1.2.0
14+
mypy==1.8.0
1515

16-
django-stubs==1.14.0
17-
djangorestframework-stubs==1.8.0
18-
boto3-stubs==1.26.81
16+
django-stubs==4.2.7
17+
djangorestframework-stubs==3.14.5
18+
boto3-stubs==1.34.48
1919

20-
flake8==6.0.0
21-
isort==5.12.0
22-
black==23.3.0
23-
pre-commit==3.2.2
20+
ruff==0.2.2
21+
pre-commit==3.6.2

runtime.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
python-3.10.8
1+
python-3.12.0

setup.cfg

Lines changed: 0 additions & 10 deletions
This file was deleted.

styleguide_example/common/models.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.db import models
2-
from django.db.models.query import F, Q
2+
from django.db.models import F, Q
33
from django.utils import timezone
44

55

@@ -29,7 +29,13 @@ class RandomModel(BaseModel):
2929
start_date = models.DateField()
3030
end_date = models.DateField()
3131

32-
simple_objects = models.ManyToManyField(SimpleModel, blank=True, related_name="random_objects")
32+
simple_objects = models.ManyToManyField(
33+
SimpleModel, blank=True, related_name="random_objects"
34+
)
3335

3436
class Meta:
35-
constraints = [models.CheckConstraint(name="start_date_before_end_date", check=Q(start_date__lt=F("end_date")))]
37+
constraints = [
38+
models.CheckConstraint(
39+
name="start_date_before_end_date", check=Q(start_date__lt=F("end_date"))
40+
)
41+
]

0 commit comments

Comments
 (0)