Skip to content

Commit

Permalink
TG-1015 Replace cookiecut project's black with ruff format (#290)
Browse files Browse the repository at this point in the history
Co-authored-by: Paolo Melchiorre <paolo@20tab.com>
  • Loading branch information
niccolomineo and Paolo Melchiorre authored Jan 11, 2024
1 parent 68b065d commit 96a5194
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 80 deletions.
17 changes: 7 additions & 10 deletions {{cookiecutter.project_dirname}}/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,24 @@ repos:
args: ["--remove"]
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.1.11
hooks:
- id: ruff
args:
- --fix
- repo: https://github.com/adamchainz/django-upgrade
rev: "1.15.0"
hooks:
- id: django-upgrade
args: [--target-version, "5.0"]
- repo: https://github.com/psf/black
rev: "23.12.1"
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.1.11
hooks:
- id: black
- id: ruff
args:
- --fix
- id: ruff-format
- repo: https://github.com/tox-dev/pyproject-fmt
rev: "1.5.3"
hooks:
- id: pyproject-fmt
- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v4.0.0-alpha.8"
rev: "v3.1.0"
hooks:
- id: prettier
exclude_types: [html]
Expand Down
20 changes: 12 additions & 8 deletions {{cookiecutter.project_dirname}}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ dumpgroups: ## Django dump auth.Group data
python3 -m manage dumpdata auth.Group --natural-foreign --natural-primary --output fixtures/auth_groups.json

.PHONY: fix
fix: ## Fix code formatting, linting and sorting imports
python3 -m black .
fix: ## Fix Python code formatting, linting and sorting imports
python3 -m ruff format .
python3 -m ruff --fix .
python3 -m mypy --no-site-packages .

Expand Down Expand Up @@ -73,11 +73,11 @@ outdated: ## Check outdated requirements and dependencies

.PHONY: pip
pip: pip_update ## Compile requirements
python3 -m piptools compile --generate-hashes --no-header --quiet --resolver=backtracking --upgrade --output-file requirements/base.txt requirements/base.in
python3 -m piptools compile --generate-hashes --no-header --quiet --resolver=backtracking --upgrade --output-file requirements/common.txt requirements/common.in
python3 -m piptools compile --generate-hashes --no-header --quiet --resolver=backtracking --upgrade --output-file requirements/local.txt requirements/local.in
python3 -m piptools compile --generate-hashes --no-header --quiet --resolver=backtracking --upgrade --output-file requirements/remote.txt requirements/remote.in
python3 -m piptools compile --generate-hashes --no-header --quiet --resolver=backtracking --upgrade --output-file requirements/test.txt requirements/test.in
python3 -m piptools compile --generate-hashes --no-header --quiet --resolver=backtracking --strip-extras --upgrade --output-file requirements/base.txt requirements/base.in
python3 -m piptools compile --generate-hashes --no-header --quiet --resolver=backtracking --strip-extras --upgrade --output-file requirements/common.txt requirements/common.in
python3 -m piptools compile --generate-hashes --no-header --quiet --resolver=backtracking --strip-extras --upgrade --output-file requirements/local.txt requirements/local.in
python3 -m piptools compile --generate-hashes --no-header --quiet --resolver=backtracking --strip-extras --upgrade --output-file requirements/remote.txt requirements/remote.in
python3 -m piptools compile --generate-hashes --no-header --quiet --resolver=backtracking --strip-extras --upgrade --output-file requirements/test.txt requirements/test.in

.PHONY: pip_update
pip_update: ## Update requirements and dependencies
Expand All @@ -87,12 +87,16 @@ pip_update: ## Update requirements and dependencies
precommit: ## Fix code formatting, linting and sorting imports
python3 -m pre_commit run --all-files

.PHONY: precommit_install
precommit_install: ## Install pre_commit
python3 -m pre_commit install

.PHONY: precommit_update
precommit_update: ## Update pre_commit
python3 -m pre_commit autoupdate

.PHONY: pytest
pytest: ## Run debugging test
pytest: ## Run debugging test with pytest
python3 -m pytest --capture=no --dc=Testing --durations 10

.PHONY: remote
Expand Down
28 changes: 15 additions & 13 deletions {{cookiecutter.project_dirname}}/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# {{ cookiecutter.project_name }}

[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/python/black)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)

A [Django](https://docs.djangoproject.com) project using [uvicorn](https://www.uvicorn.org/#running-with-gunicorn) ASGI server.

Expand All @@ -25,36 +25,36 @@ A [Django](https://docs.djangoproject.com) project using [uvicorn](https://www.u
## Conventions

- replace `projects` with your actual projects directory
- replace `git_repository_url` with your actual git repository url
- replace `git_repository_url` with your actual git repository URL

## Initialization

We suggest updating pip to the latest version and using a virtual environment to wrap all your libraries.

### Virtual environment

**IMPORTANT**: Please, create an empty virtual environment, with the right python version, and activate it.
To install and use virtualenv, please, visit the official [Python tutorial](https://docs.python.org/3/tutorial/venv.html)
**IMPORTANT**: Please, create an empty virtual environment, with the right Python version, and activate it.
To install and use a virtual environment, please, visit the official [Python tutorial](https://docs.python.org/3/tutorial/venv.html)

## Git

### Git clone

To get the existing project, change directory, clone the project repository and enter the newly created **{{ cookiecutter.project_slug }}** directory.
To get the existing project, change the directory, clone the project repository and enter the newly created `{{ cookiecutter.project_slug }}` directory.

### Git hooks

To install pre-commit into your git hooks run the below command. Pre-commit will now run on every commit. Every time you clone a project using pre-commit, running pre-commit install should always be the first thing you do.
To install pre-commit into your git hooks run the below command. Pre-commit will now run on every commit. Every time you clone a project using pre-commit, running `pre-commit` install should always be the first thing you do.

```shell
$ pre-commit install
$ make precommit_install
```

## Libraries

### Self documentation of Makefile commands
### Self-documentation of Makefile commands

To show the Makefile self documentation help:
To show the Makefile self-documentation help:

```shell
$ make
Expand All @@ -80,10 +80,10 @@ $ make pip

### Install libraries

To install the just updated requirements (e.g. `requirements/dev.txt`), execute:
To install the just updated requirements (e.g. `requirements/local.txt`), execute:

```shell
$ make dev
$ make local
```

## Testing
Expand All @@ -106,7 +106,7 @@ To run a single test suite, without coverage calculation, execute:
$ make simpletest app.tests.single.Test.to_execute
```

The _simpletest_ command accept dashed arguments with a particular syntax, such as:
The `simpletest` command accepts dashed arguments with a particular syntax, such as:

```shell
$ make simpletest app.tests.single.Test.to_execute -- --keepdb
Expand All @@ -130,8 +130,10 @@ The configuration file `.gitlab-ci.yml` should work as it is, needing no further

### The Kubernetes resource limits

The Kubernetes deployment service limits should be adapted to the expected load of the other services and to the size of the available nodes.
The Kubernetes deployment service limits should be adapted to the expected load of the other services and the size of the available nodes.

By default, the `s-1vcpu-1gb-amd` DigitalOcean droplet is used (https://slugs.do-api.dev/), which allocates 900.00m of CPU capacity and 1.54Gi of memory capacity.

The following default values are calculated assuming 2 deployments and 2 stacks on a single node.

| tfvars name | default value |
Expand Down
63 changes: 28 additions & 35 deletions {{cookiecutter.project_dirname}}/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[tool.black]
target-version = ["py312"]

[tool.pytest.ini_options]
DJANGO_CONFIGURATION = "Testing"
DJANGO_SETTINGS_MODULE = "{{ cookiecutter.django_settings_dirname }}.settings"
Expand All @@ -22,7 +19,7 @@ omit = [
"{{cookiecutter.django_settings_dirname}}/asgi.py",
"{{cookiecutter.django_settings_dirname}}/workers.py",
"{{cookiecutter.django_settings_dirname}}/wsgi.py",
"manage*.py",
"manage.py",
"pacts/*",
"venv/*",
"*/pact_states.py",
Expand All @@ -44,45 +41,41 @@ exclude_dirs = [
]

[tool.ruff]
extend-exclude = [
"__pycache__",
".pytest_cache",
".vscode*",
"*/migrations/*",
]
target-version = "py312"

[tool.ruff.lint]
ignore = [
"D203",
"D212",
"D213",
"D214",
"D215",
"D404",
"D405",
"D406",
"D407",
"D408",
"D409",
"D410",
"D411",
"D413",
"D415",
"D416",
"D417",
]
select = [
"B",
"C",
"D",
"E",
"F",
"I",
"W",
"B9"
"A", # flake8-builtins
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"C90", # McCabe cyclomatic complexity
"D", # pydocstyle
"DJ", # flake8-django
"E", # pycodestyle errors
"F", # Pyflakes
"I", # isort
"Q", # flake8-quotes
"UP", # pyupgrade
"W", # pycodestyle warnings
]
target-version = "py312"

[tool.ruff.per-file-ignores]
"{{ cookiecutter.django_settings_dirname }}/*sgi.py" = ["E402"]
"{{ cookiecutter.django_settings_dirname }}/*sgi.py" = [
"E402",
]
"*/migrations/*.py" = [
"D100",
"D101",
"D102",
"D104",
]

[tool.ruff.format]
docstring-code-format = true

[tool.ruff.isort]
known-first-party = [
Expand Down
4 changes: 2 additions & 2 deletions {{cookiecutter.project_dirname}}/requirements/common.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
-r base.in
django-configurations[cache,database,email]~=2.4.0
django~=5.0
django-configurations[cache,database,email]~=2.5.0
django~=5.0.0
4 changes: 2 additions & 2 deletions {{cookiecutter.project_dirname}}/requirements/remote.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
argon2-cffi~=23.1.0
{% if "s3" in cookiecutter.media_storage %}django-storages[boto3]~=1.14.0
{% endif %}gunicorn~=21.2.0
{% if cookiecutter.use_redis == "true" %}redis~=5.0.1
{% endif %}sentry-sdk~=1.19.0
{% if cookiecutter.use_redis == "true" %}redis~=5.0.0
{% endif %}sentry-sdk~=1.39.0
uvicorn[standard]~=0.25.0
whitenoise[brotli]~=6.6.0
3 changes: 1 addition & 2 deletions {{cookiecutter.project_dirname}}/requirements/test.in
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
-r common.in
bandit[toml]~=1.7.0
behave-django~=1.4.0
black~=23.12.0
coverage[toml]~=7.4.0
mypy~=1.8.0
pactman~=2.30.0
pip-audit~=2.6.0
pytest-django~=4.7.0
pytest-dotenv~=0.5.0
ruff~=0.0.0
ruff~=0.1.0
tblib~=3.0.0
time-machine~=2.13.0
2 changes: 1 addition & 1 deletion {{cookiecutter.project_dirname}}/scripts/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -euo pipefail

python3 -m manage check
python3 -m manage makemigrations --dry-run --check
python3 -m black --check .
python3 -m ruff format --check .
python3 -m ruff check .
python3 -m mypy --no-site-packages .
python3 -m bandit --configfile pyproject.toml --quiet --recursive .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class Local(ProjectDefault):
# https://django-debug-toolbar.readthedocs.io/en/stable/configuration.html

try:
import debug_toolbar # noqa: F401
import debug_toolbar
except ModuleNotFoundError: # pragma: no cover
pass
else: # pragma: no cover
Expand All @@ -221,7 +221,7 @@ class Local(ProjectDefault):
# https://django-extensions.readthedocs.io/en/stable/graph_models.html

try:
import django_extensions # noqa: F401
import django_extensions
except ModuleNotFoundError: # pragma: no cover
pass
else: # pragma: no cover
Expand Down Expand Up @@ -306,7 +306,7 @@ class Testing(ProjectDefault):
# https://behave-django.readthedocs.io/en/latest/installation.html

try:
import behave_django # noqa: F401
import behave_django
except ModuleNotFoundError: # pragma: no cover
pass
else: # pragma: no cover
Expand Down Expand Up @@ -346,7 +346,7 @@ def MIDDLEWARE(self): # pragma: no cover
return middleware

# DB Transaction pooling and server-side cursors
# https://docs.djangoproject.com/en/stable/ref/databases/#transaction-pooling-and-server-side-cursors # noqa
# https://docs.djangoproject.com/en/stable/ref/databases/#transaction-pooling-and-server-side-cursors

DISABLE_SERVER_SIDE_CURSORS = values.BooleanValue(False)

Expand Down Expand Up @@ -426,7 +426,7 @@ def STORAGES(self): # pragma: no cover
# https://sentry.io/for/django/

try:
import sentry_sdk # noqa: F401
import sentry_sdk
except ModuleNotFoundError: # pragma: no cover
pass
else: # pragma: no cover{% if cookiecutter.use_redis == "true" %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""{{ cookiecutter.project_name }} URL Configuration.
"""
{{ cookiecutter.project_name }} URL Configuration.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/stable/topics/http/urls/
Examples:
Examples
--------
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Expand Down

0 comments on commit 96a5194

Please sign in to comment.