From 29bd939ae4d3314be15cdbec6a14a5e45b48d9db Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 15 Jul 2023 15:10:05 +0100 Subject: [PATCH] Replace flake8 and isort with ruff --- .gitlab-ci.yml | 9 ++------- dev-requirements.txt | 9 +-------- justfile | 5 ++--- pyproject.toml | 15 +++++++-------- setup.cfg | 4 ---- website/common/models.py | 9 ++++----- website/contrib/code_block/utils.py | 6 +++--- website/contrib/unsplash/wagtail_hooks.py | 4 ++-- website/search/models.py | 4 ++-- .../commands/refresh_spotify_playlists.py | 4 +++- 10 files changed, 26 insertions(+), 43 deletions(-) delete mode 100644 setup.cfg diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7ab6685..74e4d21 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -88,15 +88,10 @@ black: script: - black --check . -isort: +ruff: extends: .python_test_template script: - - isort --check . - -flake8: - extends: .python_test_template - script: - - flake8 + - ruff check . mypy: extends: .python_test_template diff --git a/dev-requirements.txt b/dev-requirements.txt index 3a7a83b..7c73067 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,8 +1,6 @@ -r requirements.txt honcho==1.1.0 -flake8==5.0.4 -isort==5.12.0 black==23.3.0 django-browser-reload==1.8.0 django-debug-toolbar @@ -10,11 +8,6 @@ types-requests==2.31.0.1 mypy==1.3.0 wagtail-factories==4.0.0 coverage==7.2.1 -flake8-bugbear==22.12.6 -flake8-builtins==2.1.0 -flake8-comprehensions==3.10.0 -flake8-mutable==1.2.0 -flake8-print==5.0.0 -flake8-tuple==0.4.1 djlint==1.31.0 types-pyyaml==6.0.12.9 +ruff==0.0.278 diff --git a/justfile b/justfile index 3975fd0..46a5c83 100644 --- a/justfile +++ b/justfile @@ -32,17 +32,16 @@ coverage: coverage html format: + ruff check . --fix black . - isort . djlint website/ --reformat npm run format lint: lint_python lint_node lint_python: + ruff check . black --check . - isort --check . - flake8 mypy . --show-error-codes djlint website/ --lint --check diff --git a/pyproject.toml b/pyproject.toml index a4101b6..af03c83 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,14 +12,6 @@ custom_blocks="cache" [tool.djlint.per-file-ignores] "_snippet_link.html" = "H025" -[tool.isort] -multi_line_output = 3 -include_trailing_comma = true -force_grid_wrap = 0 -use_parentheses = true -line_length = 88 -skip_glob = "env/*" - [tool.mypy] no_implicit_optional = true warn_unused_ignores = true @@ -42,3 +34,10 @@ exclude_lines = [ omit = [ "*/migrations/*", ] + +[tool.ruff] +select = ["E", "F", "I", "W", "N", "B", "A", "C4", "T20", "DJ"] +ignore = ["E501", "DJ008"] + +[tool.ruff.per-file-ignores] +"*/migrations/*" = ["N806"] diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 8fe1522..0000000 --- a/setup.cfg +++ /dev/null @@ -1,4 +0,0 @@ -# flake8 doesn't support pyproject.toml yet https://github.com/PyCQA/flake8/issues/234 -[flake8] -extend_ignore=E128,E501 -extend_exclude=env diff --git a/website/common/models.py b/website/common/models.py index afc684d..5a6f8aa 100644 --- a/website/common/models.py +++ b/website/common/models.py @@ -3,9 +3,8 @@ from typing import Any, Optional, Type from django.contrib.humanize.templatetags.humanize import NaturalTimeFormatter from django.contrib.syndication.views import Feed -from django.core.paginator import EmptyPage +from django.core.paginator import EmptyPage, Paginator from django.core.paginator import Page as PaginatorPage -from django.core.paginator import Paginator from django.db import models from django.http.request import HttpRequest from django.http.response import Http404, HttpResponse, HttpResponseBadRequest @@ -48,7 +47,7 @@ class BasePage(Page): abstract = True @classproperty - def body_class(cls) -> str: + def body_class(cls) -> str: # noqa: N805 return "page-" + slugify(cls.__name__) def get_parent_pages(self) -> PageQuerySet: @@ -225,8 +224,8 @@ class BaseListingPage(RoutablePageMixin, BaseContentPage): paginator = Paginator(self.get_listing_pages(), per_page=self.PAGE_SIZE) try: return paginator.page(self.serializer.validated_data["page"]) - except EmptyPage: - raise Http404 + except EmptyPage as e: + raise Http404 from e def get_context(self, request: HttpRequest) -> dict: context = super().get_context(request) diff --git a/website/contrib/code_block/utils.py b/website/contrib/code_block/utils.py index fd46fdd..a43a7de 100644 --- a/website/contrib/code_block/utils.py +++ b/website/contrib/code_block/utils.py @@ -19,7 +19,7 @@ def get_linguist_colours() -> dict[str, str]: linguist_data = yaml.safe_load(response.text) return { - language.lower(): l["color"] - for language, l in linguist_data.items() - if l.get("color") + language.lower(): data["color"] + for language, data in linguist_data.items() + if data.get("color") } diff --git a/website/contrib/unsplash/wagtail_hooks.py b/website/contrib/unsplash/wagtail_hooks.py index 4169b43..4bd7fbd 100644 --- a/website/contrib/unsplash/wagtail_hooks.py +++ b/website/contrib/unsplash/wagtail_hooks.py @@ -19,7 +19,7 @@ class UnsplashPhotoCreateView(CreateView): """ Modify form class to validate unsplash id and save data to model1 """ - EditHandlerForm: Type[ + EditHandlerForm: Type[ # noqa: N806 WagtailAdminModelForm ] = self.edit_handler.get_form_class() @@ -31,7 +31,7 @@ class UnsplashPhotoCreateView(CreateView): cleaned_data["unsplash_id"] ) except ValueError as e: - raise ValidationError(str(e)) + raise ValidationError(str(e)) from e def save(self, commit: bool = True) -> UnsplashPhoto: self.instance.data = self._unsplash_photo_data diff --git a/website/search/models.py b/website/search/models.py index daebdae..a683974 100644 --- a/website/search/models.py +++ b/website/search/models.py @@ -80,8 +80,8 @@ class SearchPage(RoutablePageMixin, BaseContentPage): # HACK: Search results aren't a queryset, so we can't call `.specific` on it. This forces it to one as efficiently as possible results.object_list = results.object_list.get_queryset().specific() - except EmptyPage: - raise Http404 + except EmptyPage as e: + raise Http404 from e context["results"] = results diff --git a/website/spotify/management/commands/refresh_spotify_playlists.py b/website/spotify/management/commands/refresh_spotify_playlists.py index 8e6ca0a..5b54aff 100644 --- a/website/spotify/management/commands/refresh_spotify_playlists.py +++ b/website/spotify/management/commands/refresh_spotify_playlists.py @@ -8,7 +8,9 @@ from website.utils.queue import enqueue_or_sync def refresh_cache(page_id: int) -> None: page = SpotifyPlaylistPage.objects.get(id=page_id) cache.delete(page.playlist_cache_key) - page.playlist_data # Prime cache + + # Prime cache + page.playlist_data # noqa: B018 class Command(BaseCommand):