Skip to content

Commit 8e9a285

Browse files
authored
Migrate from flake8 and isort to ruff. (#629)
1 parent 6ce44f5 commit 8e9a285

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+644
-351
lines changed

.github/workflows/pythonpackage.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,10 @@ jobs:
3939
run: |
4040
python -m pip install --upgrade pip
4141
pip install -r requirements/production.txt
42-
- name: Lint with flake8
42+
- name: Install Linter
4343
run: |
44-
pip install flake8
45-
flake8 .
46-
- name: iSort
47-
run: |
48-
pip install isort
49-
isort --check-only --diff cab comments_spamfighter djangosnippets ratings
44+
pip install ruff
45+
ruff check .
5046
- name: Run migrations
5147
run: python manage.py migrate
5248
- name: Collect static files

.pre-commit-config.yaml

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
# See https://pre-commit.com for more information
22
# See https://pre-commit.com/hooks.html for more hooks
33
repos:
4-
- repo: https://github.com/pre-commit/pre-commit-hooks
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
55
rev: v3.2.0
66
hooks:
77
- id: trailing-whitespace
88
- id: end-of-file-fixer
99
- id: check-yaml
1010
- id: check-added-large-files
1111

12-
- repo: https://github.com/pycqa/isort
13-
rev: 5.13.2
12+
- repo: https://github.com/astral-sh/ruff-pre-commit
13+
rev: v0.14.3
1414
hooks:
15-
- id: isort
16-
17-
- repo: https://github.com/psf/black
18-
rev: 25.1.0
19-
hooks:
20-
- id: black
21-
22-
- repo: https://github.com/pycqa/flake8
23-
rev: '7.2.0' # pick a git hash / tag to point to
24-
hooks:
25-
- id: flake8
15+
- id: ruff-check
16+
args: [--fix, --exit-non-zero-on-fix]
17+
- id: ruff-format

base/components/__init__.py

Whitespace-only changes.

base/components/components.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Literal, Optional
1+
from typing import Literal
22

33
from django_components import Component, register
44
from pydantic import BaseModel
@@ -10,7 +10,6 @@
1010

1111
@register("icon")
1212
class Icon(Component):
13-
1413
class Kwargs(BaseModel):
1514
kind: Literal["heart", "bookmark"]
1615
color: str
@@ -29,8 +28,8 @@ def get_template_data(self, args, kwargs, slots, context):
2928

3029
class PaginationItem(BaseModel):
3130
kind: Literal["current", "ellipsis", "number"]
32-
text: Optional[str | int] = None
33-
attrs: Optional[dict] = None
31+
text: str | int | None = None
32+
attrs: dict | None = None
3433

3534

3635
@register("pagination")
@@ -48,21 +47,26 @@ def pagination_number(self, pagination: Pagination, num: int) -> PaginationItem:
4847
"""
4948
if num == pagination.paginator.ELLIPSIS:
5049
return PaginationItem(kind="ellipsis", text=str(pagination.paginator.ELLIPSIS))
51-
elif num == pagination.page_num:
50+
if num == pagination.page_num:
5251
return PaginationItem(kind="current", text=num)
53-
else:
54-
link = querystring(None, {**pagination.params, PAGE_VAR: num})
55-
return PaginationItem(
56-
kind="number",
57-
text=num,
58-
attrs={"href": link},
59-
)
52+
link = querystring(None, {**pagination.params, PAGE_VAR: num})
53+
return PaginationItem(
54+
kind="number",
55+
text=num,
56+
attrs={"href": link},
57+
)
6058

6159
def get_template_data(self, args, kwargs, slots, context):
6260
pagination = kwargs.pagination_obj
63-
page_elements = [self.pagination_number(pagination, page_num) for page_num in pagination.page_range]
64-
previous_page_link = f"?{PAGE_VAR}={pagination.page_num - 1}" if pagination.page.has_previous() else ""
65-
next_page_link = f"?{PAGE_VAR}={pagination.page_num + 1}" if pagination.page.has_next() else ""
61+
page_elements = [
62+
self.pagination_number(pagination, page_num) for page_num in pagination.page_range
63+
]
64+
previous_page_link = (
65+
f"?{PAGE_VAR}={pagination.page_num - 1}" if pagination.page.has_previous() else ""
66+
)
67+
next_page_link = (
68+
f"?{PAGE_VAR}={pagination.page_num + 1}" if pagination.page.has_next() else ""
69+
)
6670
return {
6771
"pagination": pagination,
6872
"previous_page_link": previous_page_link,
@@ -74,7 +78,7 @@ def get_template_data(self, args, kwargs, slots, context):
7478
class TabItem(BaseModel):
7579
text: str
7680
is_current: bool
77-
attrs: Optional[dict]
81+
attrs: dict | None
7882

7983

8084
@register("sorting_tabs")
@@ -95,8 +99,7 @@ def create_tab(self, object_list: ObjectList, tab: str) -> TabItem:
9599
return TabItem(text=verbose_text, is_current=is_current, attrs=attrs)
96100

97101
def create_all_tabs(self, object_list: ObjectList):
98-
tabs = [self.create_tab(object_list, tab) for tab in object_list.sorting_tabs]
99-
return tabs
102+
return [self.create_tab(object_list, tab) for tab in object_list.sorting_tabs]
100103

101104
def get_template_data(self, args, kwargs, slots, context):
102105
object_list = kwargs.object_list

base/exceptions.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
class IncorectLookupParameter(Exception):
1+
class IncorrectLookupParameterError(Exception):
22
"""
33
Raised when a query parameter contains an incorrect value.
44
"""
5-
6-
pass

base/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,4 @@ def paginate(self, request, queryset):
4949

5050
def get_objects(self, request, queryset):
5151
tab_result = self.tab_sort(queryset)
52-
paginate_result = self.paginate(request, tab_result)
53-
return paginate_result
52+
return self.paginate(request, tab_result)

base/pagination.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django.core.paginator import InvalidPage, Paginator
22

3-
from .exceptions import IncorectLookupParameter
3+
from .exceptions import IncorrectLookupParameterError
44

55
PAGE_VAR = "page"
66

@@ -49,6 +49,6 @@ def get_objects(self):
4949
else:
5050
try:
5151
result_list = self.paginator.page(self.page_num).object_list
52-
except InvalidPage:
53-
raise IncorectLookupParameter
52+
except InvalidPage as err:
53+
raise IncorrectLookupParameterError from err
5454
return result_list

base/templatetags/base_templatetags.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,16 @@ def querystring(context, *args, **kwargs):
4949
params = QueryDict(mutable=True)
5050
for d in [*args, kwargs]:
5151
if not isinstance(d, Mapping):
52+
msg = f"querystring requires mappings for positional arguments (got {d!r} instead)."
5253
raise TemplateSyntaxError(
53-
"querystring requires mappings for positional arguments (got " "%r instead)." % d
54+
msg,
5455
)
5556
for key, value in d.items():
5657
if not isinstance(key, str):
57-
raise TemplateSyntaxError("querystring requires strings for mapping keys (got %r " "instead)." % key)
58+
msg = f"querystring requires strings for mapping keys (got {key!r} instead)."
59+
raise TemplateSyntaxError(
60+
msg,
61+
)
5862
if value is None:
5963
params.pop(key, None)
6064
elif isinstance(value, Iterable) and not isinstance(value, str):

base/tests/migrations/0001_initial.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77

88
class Migration(migrations.Migration):
9-
109
initial = True
1110

1211
dependencies = [
@@ -17,26 +16,56 @@ class Migration(migrations.Migration):
1716
migrations.CreateModel(
1817
name="Beverage",
1918
fields=[
20-
("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
19+
(
20+
"id",
21+
models.AutoField(
22+
auto_created=True,
23+
primary_key=True,
24+
serialize=False,
25+
verbose_name="ID",
26+
),
27+
),
2128
("name", models.CharField(max_length=50)),
2229
],
2330
),
2431
migrations.CreateModel(
2532
name="Food",
2633
fields=[
27-
("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
34+
(
35+
"id",
36+
models.AutoField(
37+
auto_created=True,
38+
primary_key=True,
39+
serialize=False,
40+
verbose_name="ID",
41+
),
42+
),
2843
("name", models.CharField(max_length=50)),
2944
],
3045
),
3146
migrations.CreateModel(
3247
name="BeverageRating",
3348
fields=[
34-
("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
49+
(
50+
"id",
51+
models.AutoField(
52+
auto_created=True,
53+
primary_key=True,
54+
serialize=False,
55+
verbose_name="ID",
56+
),
57+
),
3558
("score", models.FloatField(db_index=True, default=0)),
36-
("hashed", models.CharField(db_index=True, editable=False, max_length=40)),
59+
(
60+
"hashed",
61+
models.CharField(db_index=True, editable=False, max_length=40),
62+
),
3763
(
3864
"content_object",
39-
models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="tests.Beverage"),
65+
models.ForeignKey(
66+
on_delete=django.db.models.deletion.CASCADE,
67+
to="tests.Beverage",
68+
),
4069
),
4170
(
4271
"user",

base/tests/migrations/0002_fish.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55

66
class Migration(migrations.Migration):
7-
87
dependencies = [
98
("tests", "0001_initial"),
109
]
@@ -13,7 +12,15 @@ class Migration(migrations.Migration):
1312
migrations.CreateModel(
1413
name="Fish",
1514
fields=[
16-
("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
15+
(
16+
"id",
17+
models.AutoField(
18+
auto_created=True,
19+
primary_key=True,
20+
serialize=False,
21+
verbose_name="ID",
22+
),
23+
),
1724
("name", models.CharField(max_length=255)),
1825
("price", models.IntegerField()),
1926
],

0 commit comments

Comments
 (0)