Skip to content

Commit 8478467

Browse files
authored
chore: add Ruff and use it for lint checks (#175)
1 parent 39df677 commit 8478467

File tree

13 files changed

+72
-40
lines changed

13 files changed

+72
-40
lines changed

.github/workflows/ci.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ jobs:
5050
- run: make test
5151

5252
lint:
53-
name: "Black"
5453
runs-on: ubuntu-latest
5554
steps:
5655
- uses: actions/checkout@v3
5756
- name: Set up Python
5857
uses: actions/setup-python@v4
5958
with:
60-
python-version: "3.11"
61-
- run: pip install black
62-
- run: black --check .
59+
python-version: "3.12"
60+
- run: pip install poetry
61+
- run: poetry install --with=dev
62+
- run: make lint

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ install: ## Install development dependencies
2727
poetry install
2828
pip install Django
2929

30+
lint: ## Check the project for lint errors
31+
poetry run ruff check .
32+
poetry run ruff format --diff .
33+
3034
tdd: ## Run tests with a file watcher
3135
PYTHONPATH=. nodemon --ext py -x sh -c "poetry run python -W ignore::RuntimeWarning $(MANAGE) test --failfast django_object_actions || true"
3236

django_object_actions/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,11 @@
1010
takes_instance_or_queryset,
1111
action,
1212
)
13+
14+
15+
__all__ = [
16+
"BaseDjangoObjectActions",
17+
"DjangoObjectActions",
18+
"takes_instance_or_queryset",
19+
"action",
20+
]

django_object_actions/tests/test_admin.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CommentTests(LoggedInTestCase):
1919
def test_action_on_a_model_with_uuid_pk_works(self):
2020
comment = CommentFactory()
2121
comment_url = reverse("admin:polls_comment_change", args=(comment.pk,))
22-
action_url = "/admin/polls/comment/{0}/actions/hodor/".format(comment.pk)
22+
action_url = f"/admin/polls/comment/{comment.pk}/actions/hodor/"
2323
# sanity check that url has a uuid
2424
self.assertIn("-", action_url)
2525
response = self.client.get(action_url)
@@ -28,7 +28,7 @@ def test_action_on_a_model_with_uuid_pk_works(self):
2828
@patch("django_object_actions.utils.ChangeActionView.dispatch")
2929
def test_action_on_a_model_with_arbitrary_pk_works(self, mock_view):
3030
mock_view.return_value = HttpResponse()
31-
action_url = "/admin/polls/comment/{0}/actions/hodor/".format(" i am a pk ")
31+
action_url = "/admin/polls/comment/{}/actions/hodor/".format(" i am a pk ")
3232

3333
self.client.get(action_url)
3434

@@ -38,7 +38,7 @@ def test_action_on_a_model_with_arbitrary_pk_works(self, mock_view):
3838
@patch("django_object_actions.utils.ChangeActionView.dispatch")
3939
def test_action_on_a_model_with_slash_in_pk_works(self, mock_view):
4040
mock_view.return_value = HttpResponse()
41-
action_url = "/admin/polls/comment/{0}/actions/hodor/".format("pk/slash")
41+
action_url = "/admin/polls/comment/{}/actions/hodor/".format("pk/slash")
4242

4343
self.client.get(action_url)
4444

@@ -52,8 +52,8 @@ def test_action_on_a_model_with_complex_id(self):
5252
related_data_url = reverse(
5353
"admin:polls_relateddata_change", args=(related_data.pk,)
5454
)
55-
action_url = "/admin/polls/relateddata/{}/actions/fill_up/".format(
56-
quote(related_data.pk)
55+
action_url = (
56+
f"/admin/polls/relateddata/{quote(related_data.pk)}/actions/fill_up/"
5757
)
5858

5959
response = self.client.get(action_url)

django_object_actions/tests/test_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def test_get_button_attrs_custom_attrs_get_partitioned(self):
8181

8282
class BaseActionViewTests(TestCase):
8383
def setUp(self):
84-
super(BaseActionViewTests, self).setUp()
84+
super().setUp()
8585
self.view = BaseActionView()
8686

8787
@mock.patch("django_object_actions.utils.messages")

django_object_actions/tests/tests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class LoggedInTestCase(TestCase):
99
def setUp(self):
10-
super(LoggedInTestCase, self).setUp()
10+
super().setUp()
1111
UserFactory.create(
1212
is_staff=True, is_superuser=True, username="admin", password="admin"
1313
)

django_object_actions/utils.py

+8-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
DEFAULT_BUTTON_TYPE = "a"
1616

1717

18-
class BaseDjangoObjectActions(object):
18+
class BaseDjangoObjectActions:
1919
"""
2020
ModelAdmin mixin to add new actions just like adding admin actions.
2121
@@ -43,7 +43,7 @@ class BaseDjangoObjectActions(object):
4343

4444
def get_urls(self):
4545
"""Prepend `get_urls` with our own patterns."""
46-
urls = super(BaseDjangoObjectActions, self).get_urls()
46+
urls = super().get_urls()
4747
return self._get_action_urls() + urls
4848

4949
def change_view(self, request, object_id, form_url="", extra_context=None):
@@ -57,9 +57,7 @@ def change_view(self, request, object_id, form_url="", extra_context=None):
5757
"tools_view_name": self.tools_view_name,
5858
}
5959
)
60-
return super(BaseDjangoObjectActions, self).change_view(
61-
request, object_id, form_url, extra_context
62-
)
60+
return super().change_view(request, object_id, form_url, extra_context)
6361

6462
def changelist_view(self, request, extra_context=None):
6563
extra_context = extra_context or {}
@@ -72,9 +70,7 @@ def changelist_view(self, request, extra_context=None):
7270
"tools_view_name": self.tools_view_name,
7371
}
7472
)
75-
return super(BaseDjangoObjectActions, self).changelist_view(
76-
request, extra_context
77-
)
73+
return super().changelist_view(request, extra_context)
7874

7975
# USER OVERRIDABLE
8076
##################
@@ -112,9 +108,9 @@ def _get_action_urls(self):
112108

113109
model_name = self.model._meta.model_name
114110
# e.g.: polls_poll
115-
base_url_name = "%s_%s" % (self.model._meta.app_label, model_name)
111+
base_url_name = f"{self.model._meta.app_label}_{model_name}"
116112
# e.g.: polls_poll_actions
117-
model_actions_url_name = "%s_actions" % base_url_name
113+
model_actions_url_name = f"{base_url_name}_actions"
118114

119115
self.tools_view_name = "admin:" + model_actions_url_name
120116

@@ -131,7 +127,7 @@ def _get_action_urls(self):
131127
ChangeActionView.as_view(
132128
model=self.model,
133129
actions=actions,
134-
back="admin:%s_change" % base_url_name,
130+
back=f"admin:{base_url_name}_change",
135131
current_app=self.admin_site.name,
136132
)
137133
),
@@ -144,7 +140,7 @@ def _get_action_urls(self):
144140
ChangeListActionView.as_view(
145141
model=self.model,
146142
actions=actions,
147-
back="admin:%s_changelist" % base_url_name,
143+
back=f"admin:{base_url_name}_changelist",
148144
current_app=self.admin_site.name,
149145
)
150146
),

example_project/polls/admin.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class PollAdmin(DjangoObjectActions, admin.ModelAdmin):
8686

8787
def changelist_view(self, request, extra_context=None):
8888
extra_context = {"foo": "changelist_view"}
89-
return super(PollAdmin, self).changelist_view(request, extra_context)
89+
return super().changelist_view(request, extra_context)
9090

9191
# Detail
9292
########
@@ -99,7 +99,7 @@ def changelist_view(self, request, extra_context=None):
9999

100100
def change_view(self, request, object_id, form_url="", extra_context=None):
101101
extra = {"foo": "change_view"}
102-
return super(PollAdmin, self).change_view(request, object_id, form_url, extra)
102+
return super().change_view(request, object_id, form_url, extra)
103103

104104
# Object actions
105105
################
@@ -123,9 +123,7 @@ def question_mark(self, request, obj):
123123
change_actions = ("delete_all_choices", "question_mark")
124124

125125
def get_change_actions(self, request, object_id, form_url):
126-
actions = super(PollAdmin, self).get_change_actions(
127-
request, object_id, form_url
128-
)
126+
actions = super().get_change_actions(request, object_id, form_url)
129127
actions = list(actions)
130128
if not request.user.is_superuser:
131129
return []

example_project/polls/factories.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ def get_random_string(length):
4949

5050
class RelatedDataFactory(factory.django.DjangoModelFactory):
5151
id = factory.lazy_attribute(
52-
lambda __: "{}:{}-{}!{}".format(
53-
get_random_string(2),
54-
get_random_string(2),
55-
get_random_string(2),
56-
get_random_string(2),
57-
)
52+
lambda __: f"{get_random_string(2)}:{get_random_string(2)}-{get_random_string(2)}!{get_random_string(2)}"
5853
)
5954

6055
class Meta:

example_project/polls/migrations/0001_initial.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# -*- coding: utf-8 -*-
21
# Generated by Django 1.9.2 on 2016-02-25 17:25
3-
from __future__ import unicode_literals
42

53
from django.db import migrations, models
64
import django.db.models.deletion

example_project/urls.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from django import VERSION
2-
from django.urls import include, path
1+
from django.urls import path
32
from django.contrib import admin
43
from example_project.polls.admin import support_admin
54

poetry.lock

+28-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ classifiers = [
3131
[tool.poetry.dependencies]
3232
python = "^3.7"
3333

34-
[tool.poetry.dev-dependencies]
34+
[tool.poetry.group.dev.dependencies]
3535
coverage = "7.*"
3636
django-extensions = "3.*"
3737
factory-boy = "3.*"
3838
dj-database-url = "2.*"
39+
ruff = "*"
3940

4041
[tool.semantic_release]
4142
version_toml = ["pyproject.toml:tool.poetry.version"]
@@ -54,3 +55,9 @@ exclude_lines = [
5455
"__unicode__",
5556
"raise NotImplementedError",
5657
]
58+
59+
[tool.ruff]
60+
target-version = "py37"
61+
62+
[tool.ruff.lint]
63+
extend-select = ["UP"]

0 commit comments

Comments
 (0)