Skip to content

Commit

Permalink
chore: add Ruff and use it for lint checks (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
crccheck authored Sep 10, 2024
1 parent 39df677 commit 8478467
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 40 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ jobs:
- run: make test

lint:
name: "Black"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11"
- run: pip install black
- run: black --check .
python-version: "3.12"
- run: pip install poetry
- run: poetry install --with=dev
- run: make lint
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ install: ## Install development dependencies
poetry install
pip install Django

lint: ## Check the project for lint errors
poetry run ruff check .
poetry run ruff format --diff .

tdd: ## Run tests with a file watcher
PYTHONPATH=. nodemon --ext py -x sh -c "poetry run python -W ignore::RuntimeWarning $(MANAGE) test --failfast django_object_actions || true"

Expand Down
8 changes: 8 additions & 0 deletions django_object_actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@
takes_instance_or_queryset,
action,
)


__all__ = [
"BaseDjangoObjectActions",
"DjangoObjectActions",
"takes_instance_or_queryset",
"action",
]
10 changes: 5 additions & 5 deletions django_object_actions/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CommentTests(LoggedInTestCase):
def test_action_on_a_model_with_uuid_pk_works(self):
comment = CommentFactory()
comment_url = reverse("admin:polls_comment_change", args=(comment.pk,))
action_url = "/admin/polls/comment/{0}/actions/hodor/".format(comment.pk)
action_url = f"/admin/polls/comment/{comment.pk}/actions/hodor/"
# sanity check that url has a uuid
self.assertIn("-", action_url)
response = self.client.get(action_url)
Expand All @@ -28,7 +28,7 @@ def test_action_on_a_model_with_uuid_pk_works(self):
@patch("django_object_actions.utils.ChangeActionView.dispatch")
def test_action_on_a_model_with_arbitrary_pk_works(self, mock_view):
mock_view.return_value = HttpResponse()
action_url = "/admin/polls/comment/{0}/actions/hodor/".format(" i am a pk ")
action_url = "/admin/polls/comment/{}/actions/hodor/".format(" i am a pk ")

self.client.get(action_url)

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

self.client.get(action_url)

Expand All @@ -52,8 +52,8 @@ def test_action_on_a_model_with_complex_id(self):
related_data_url = reverse(
"admin:polls_relateddata_change", args=(related_data.pk,)
)
action_url = "/admin/polls/relateddata/{}/actions/fill_up/".format(
quote(related_data.pk)
action_url = (
f"/admin/polls/relateddata/{quote(related_data.pk)}/actions/fill_up/"
)

response = self.client.get(action_url)
Expand Down
2 changes: 1 addition & 1 deletion django_object_actions/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_get_button_attrs_custom_attrs_get_partitioned(self):

class BaseActionViewTests(TestCase):
def setUp(self):
super(BaseActionViewTests, self).setUp()
super().setUp()
self.view = BaseActionView()

@mock.patch("django_object_actions.utils.messages")
Expand Down
2 changes: 1 addition & 1 deletion django_object_actions/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class LoggedInTestCase(TestCase):
def setUp(self):
super(LoggedInTestCase, self).setUp()
super().setUp()
UserFactory.create(
is_staff=True, is_superuser=True, username="admin", password="admin"
)
Expand Down
20 changes: 8 additions & 12 deletions django_object_actions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
DEFAULT_BUTTON_TYPE = "a"


class BaseDjangoObjectActions(object):
class BaseDjangoObjectActions:
"""
ModelAdmin mixin to add new actions just like adding admin actions.
Expand Down Expand Up @@ -43,7 +43,7 @@ class BaseDjangoObjectActions(object):

def get_urls(self):
"""Prepend `get_urls` with our own patterns."""
urls = super(BaseDjangoObjectActions, self).get_urls()
urls = super().get_urls()
return self._get_action_urls() + urls

def change_view(self, request, object_id, form_url="", extra_context=None):
Expand All @@ -57,9 +57,7 @@ def change_view(self, request, object_id, form_url="", extra_context=None):
"tools_view_name": self.tools_view_name,
}
)
return super(BaseDjangoObjectActions, self).change_view(
request, object_id, form_url, extra_context
)
return super().change_view(request, object_id, form_url, extra_context)

def changelist_view(self, request, extra_context=None):
extra_context = extra_context or {}
Expand All @@ -72,9 +70,7 @@ def changelist_view(self, request, extra_context=None):
"tools_view_name": self.tools_view_name,
}
)
return super(BaseDjangoObjectActions, self).changelist_view(
request, extra_context
)
return super().changelist_view(request, extra_context)

# USER OVERRIDABLE
##################
Expand Down Expand Up @@ -112,9 +108,9 @@ def _get_action_urls(self):

model_name = self.model._meta.model_name
# e.g.: polls_poll
base_url_name = "%s_%s" % (self.model._meta.app_label, model_name)
base_url_name = f"{self.model._meta.app_label}_{model_name}"
# e.g.: polls_poll_actions
model_actions_url_name = "%s_actions" % base_url_name
model_actions_url_name = f"{base_url_name}_actions"

self.tools_view_name = "admin:" + model_actions_url_name

Expand All @@ -131,7 +127,7 @@ def _get_action_urls(self):
ChangeActionView.as_view(
model=self.model,
actions=actions,
back="admin:%s_change" % base_url_name,
back=f"admin:{base_url_name}_change",
current_app=self.admin_site.name,
)
),
Expand All @@ -144,7 +140,7 @@ def _get_action_urls(self):
ChangeListActionView.as_view(
model=self.model,
actions=actions,
back="admin:%s_changelist" % base_url_name,
back=f"admin:{base_url_name}_changelist",
current_app=self.admin_site.name,
)
),
Expand Down
8 changes: 3 additions & 5 deletions example_project/polls/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class PollAdmin(DjangoObjectActions, admin.ModelAdmin):

def changelist_view(self, request, extra_context=None):
extra_context = {"foo": "changelist_view"}
return super(PollAdmin, self).changelist_view(request, extra_context)
return super().changelist_view(request, extra_context)

# Detail
########
Expand All @@ -99,7 +99,7 @@ def changelist_view(self, request, extra_context=None):

def change_view(self, request, object_id, form_url="", extra_context=None):
extra = {"foo": "change_view"}
return super(PollAdmin, self).change_view(request, object_id, form_url, extra)
return super().change_view(request, object_id, form_url, extra)

# Object actions
################
Expand All @@ -123,9 +123,7 @@ def question_mark(self, request, obj):
change_actions = ("delete_all_choices", "question_mark")

def get_change_actions(self, request, object_id, form_url):
actions = super(PollAdmin, self).get_change_actions(
request, object_id, form_url
)
actions = super().get_change_actions(request, object_id, form_url)
actions = list(actions)
if not request.user.is_superuser:
return []
Expand Down
7 changes: 1 addition & 6 deletions example_project/polls/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ def get_random_string(length):

class RelatedDataFactory(factory.django.DjangoModelFactory):
id = factory.lazy_attribute(
lambda __: "{}:{}-{}!{}".format(
get_random_string(2),
get_random_string(2),
get_random_string(2),
get_random_string(2),
)
lambda __: f"{get_random_string(2)}:{get_random_string(2)}-{get_random_string(2)}!{get_random_string(2)}"
)

class Meta:
Expand Down
2 changes: 0 additions & 2 deletions example_project/polls/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.2 on 2016-02-25 17:25
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
Expand Down
3 changes: 1 addition & 2 deletions example_project/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django import VERSION
from django.urls import include, path
from django.urls import path
from django.contrib import admin
from example_project.polls.admin import support_admin

Expand Down
29 changes: 28 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ classifiers = [
[tool.poetry.dependencies]
python = "^3.7"

[tool.poetry.dev-dependencies]
[tool.poetry.group.dev.dependencies]
coverage = "7.*"
django-extensions = "3.*"
factory-boy = "3.*"
dj-database-url = "2.*"
ruff = "*"

[tool.semantic_release]
version_toml = ["pyproject.toml:tool.poetry.version"]
Expand All @@ -54,3 +55,9 @@ exclude_lines = [
"__unicode__",
"raise NotImplementedError",
]

[tool.ruff]
target-version = "py37"

[tool.ruff.lint]
extend-select = ["UP"]

0 comments on commit 8478467

Please sign in to comment.