From 0d7514d76a66d2a805bcf27a9106bdf669e09782 Mon Sep 17 00:00:00 2001 From: odrling Date: Sat, 16 Mar 2024 00:48:59 +0100 Subject: [PATCH 1/6] setup ruff as a flake8 replacement ruff runs pretty fast, which is nice when used as a pre-commit hook. It also implements more checks that can be selected as needed, in this commit I add the bugbear (B) checks which were not present in the previous configuration. --- .pre-commit-config.yaml | 6 +++--- .ruff.toml | 5 +++++ requirements_dev.txt | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 .ruff.toml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 60861bf9..6c2d873c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -23,8 +23,8 @@ repos: entry: black . --check language: system pass_filenames: false - - id: flake8-check - name: Check PEP8 - entry: flake8 + - id: ruff-check + name: Run ruff linter + entry: ruff check . language: system pass_filenames: false diff --git a/.ruff.toml b/.ruff.toml new file mode 100644 index 00000000..e816a6ae --- /dev/null +++ b/.ruff.toml @@ -0,0 +1,5 @@ +line-length = 88 +ignore = [] + +[lint] +select = ["E", "F", "W", "B"] diff --git a/requirements_dev.txt b/requirements_dev.txt index a16a1ddb..4431e9d1 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -8,3 +8,4 @@ pytest-cov>=4.0.0,<4.1.0 pytest-django>=4.5.2,<4.6.0 pytest-mock>=3.10.0,<3.11.0 pytest>=7.2.0,<7.3.0 +ruff From 1a1ef6380f1ed9ab588a7f3836a2934146eae21c Mon Sep 17 00:00:00 2001 From: odrling Date: Sat, 16 Mar 2024 00:58:32 +0100 Subject: [PATCH 2/6] fix issues reported by bugbear checks The first one (not binding a loop variable in a local function) can actually result in bugs. Others are mostly minor changes. --- dakara_server/internal/cache_model.py | 32 +++++++++++++++------------ 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/dakara_server/internal/cache_model.py b/dakara_server/internal/cache_model.py index ad10cba8..b94cd5d6 100644 --- a/dakara_server/internal/cache_model.py +++ b/dakara_server/internal/cache_model.py @@ -37,17 +37,8 @@ def _manage_on_delete_fields(self): cache model. Instead, we execute the on-delete action with the `pre_delete` signal of the related field. """ - for field in self.model._meta.concrete_fields: - # only consider foreign key fields with the callable attribute - # "cache_on_delete" - if not isinstance(field, models.ForeignKey): - continue - - on_delete = getattr(field, "cache_on_delete", None) - - if not callable(on_delete): - raise TypeError("cache_on_delete must be callable") + def handle_decorator(field): # register the handle to the pre_delete signal @receiver( pre_delete, @@ -59,10 +50,23 @@ def _manage_on_delete_fields(self): ) def handle(sender, **kwargs): instance = kwargs.get("instance") - on_delete(instance, self) + field.on_delete(instance, self) + + return handle + + for field in self.model._meta.concrete_fields: + # only consider foreign key fields with the callable attribute + # "cache_on_delete" + if not isinstance(field, models.ForeignKey): + continue + + on_delete = getattr(field, "cache_on_delete", None) + + if not callable(on_delete): + raise TypeError("cache_on_delete must be callable") # store the handle - self._on_delete_funcs[field.name] = handle + self._on_delete_funcs[field.name] = handle_decorator(field) @contextmanager def _access_store(self): @@ -233,7 +237,7 @@ def __new__(cls, name, bases, attrs): # create and connect cache manager manager = CacheManager() manager._connect(new_class) - setattr(new_class, "cache", manager) + new_class.cache = manager return new_class @@ -279,7 +283,7 @@ class CacheOnDeleteMixin: """ def __init__(self, to, on_delete, *args, **kwargs): - super().__init__(to, on_delete=models.DO_NOTHING, *args, **kwargs) + super().__init__(to, *args, on_delete=models.DO_NOTHING, **kwargs) self.cache_on_delete = on_delete From 4b89980f337c7e700bac4eb8905b38362ab4b5f3 Mon Sep 17 00:00:00 2001 From: odrling Date: Sat, 16 Mar 2024 01:12:40 +0100 Subject: [PATCH 3/6] remove flake8 configuration --- dakara_server/internal/cache_model.py | 2 +- setup.cfg | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/dakara_server/internal/cache_model.py b/dakara_server/internal/cache_model.py index b94cd5d6..757c43f7 100644 --- a/dakara_server/internal/cache_model.py +++ b/dakara_server/internal/cache_model.py @@ -50,7 +50,7 @@ def handle_decorator(field): ) def handle(sender, **kwargs): instance = kwargs.get("instance") - field.on_delete(instance, self) + field.cache_on_delete(instance, self) return handle diff --git a/setup.cfg b/setup.cfg index 3e7d4607..619cbdba 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,7 +1,3 @@ -[flake8] -max-line-length = 88 -ignore = E203, W503 - [coverage:run] omit = */tests/*, */conftest.py From 7ef7869d9676c06ab38bb5e252f0c1d943962545 Mon Sep 17 00:00:00 2001 From: odrling Date: Sat, 16 Mar 2024 01:28:22 +0100 Subject: [PATCH 4/6] Use ruff in CI workflow --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 15626570..033619b1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -63,5 +63,5 @@ jobs: - name: Run style checks run: python -m black . --check - - name: Run PEP8 style checks - run: python -m flake8 + - name: Run ruff linter + run: python -m ruff check . From 878b6093afba3fbc6e8cd1960254b56de8ff5e41 Mon Sep 17 00:00:00 2001 From: odrling Date: Sat, 16 Mar 2024 15:01:58 +0100 Subject: [PATCH 5/6] ruff: move `ignore` key in the lint section of the config --- .ruff.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ruff.toml b/.ruff.toml index e816a6ae..cd53a468 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -1,5 +1,5 @@ line-length = 88 -ignore = [] [lint] select = ["E", "F", "W", "B"] +ignore = [] From d4a64c820bc97da27b446b53835d907318a7bc1e Mon Sep 17 00:00:00 2001 From: odrling Date: Sat, 16 Mar 2024 15:04:21 +0100 Subject: [PATCH 6/6] lock ruff to version 0.3.x --- requirements_dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements_dev.txt b/requirements_dev.txt index 4431e9d1..23f30d1d 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -8,4 +8,4 @@ pytest-cov>=4.0.0,<4.1.0 pytest-django>=4.5.2,<4.6.0 pytest-mock>=3.10.0,<3.11.0 pytest>=7.2.0,<7.3.0 -ruff +ruff>=0.3.0,<0.4.0