Skip to content

Commit fa2b4d7

Browse files
authored
refactor: add more lint rules (#181)
Bringing in rules I've had success with elsewhere. More consistent style helps with readability and maintainability. Some of the rules help with code simplicity and with reducing bugs too.
1 parent 080ee8c commit fa2b4d7

File tree

11 files changed

+34
-23
lines changed

11 files changed

+34
-23
lines changed

django_object_actions/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
from .utils import (
88
BaseDjangoObjectActions,
99
DjangoObjectActions,
10-
takes_instance_or_queryset,
1110
action,
11+
takes_instance_or_queryset,
1212
)
1313

14-
1514
__all__ = [
1615
"BaseDjangoObjectActions",
1716
"DjangoObjectActions",
18-
"takes_instance_or_queryset",
1917
"action",
18+
"takes_instance_or_queryset",
2019
]

django_object_actions/tests/test_admin.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22
Integration tests that actually try and use the tools setup in admin.py
33
"""
44

5+
from unittest.mock import patch
6+
57
from django.contrib.admin.utils import quote
68
from django.http import HttpResponse
79
from django.urls import reverse
8-
from unittest.mock import patch
910

10-
from .tests import LoggedInTestCase
1111
from example_project.polls.factories import (
1212
CommentFactory,
1313
PollFactory,
1414
RelatedDataFactory,
1515
)
1616

17+
from .tests import LoggedInTestCase
18+
1719

1820
class CommentTests(LoggedInTestCase):
1921
def test_action_on_a_model_with_uuid_pk_works(self):

django_object_actions/tests/test_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
from unittest import mock
22

33
from django.test import TestCase
4+
45
from example_project.polls.models import Poll
56

67
from ..utils import (
7-
BaseDjangoObjectActions,
88
BaseActionView,
9-
takes_instance_or_queryset,
9+
BaseDjangoObjectActions,
1010
action,
11+
takes_instance_or_queryset,
1112
)
1213

1314

django_object_actions/tests/tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from django.urls import reverse
21
from django.test import TestCase
2+
from django.urls import reverse
33

44
from example_project.polls.factories import UserFactory
55
from example_project.polls.models import Choice

django_object_actions/utils.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
from django.db.models.query import QuerySet
77
from django.http import Http404, HttpResponseRedirect
88
from django.http.response import HttpResponseBase, HttpResponseNotAllowed
9+
from django.urls import re_path, reverse
910
from django.views.generic import View
1011
from django.views.generic.detail import SingleObjectMixin
1112
from django.views.generic.list import MultipleObjectMixin
12-
from django.urls import re_path, reverse
1313

1414
DEFAULT_METHODS_ALLOWED = ("GET", "POST")
1515
DEFAULT_BUTTON_TYPE = "a"
@@ -245,8 +245,8 @@ def dispatch(self, request, tool, **kwargs):
245245

246246
try:
247247
view = self.actions[tool]
248-
except KeyError:
249-
raise Http404("Action does not exist")
248+
except KeyError as exc:
249+
raise Http404("Action does not exist") from exc
250250

251251
allowed_methods = getattr(view, "methods", DEFAULT_METHODS_ALLOWED)
252252
if request.method.upper() not in allowed_methods:
@@ -363,5 +363,4 @@ def decorator(func):
363363

364364
if function is None:
365365
return decorator
366-
else:
367-
return decorator(function)
366+
return decorator(function)

example_project/polls/admin.py

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

77
from django_object_actions import (
88
DjangoObjectActions,
9-
takes_instance_or_queryset,
109
action,
10+
takes_instance_or_queryset,
1111
)
1212

13-
from .models import Choice, Poll, Comment, RelatedData
13+
from .models import Choice, Comment, Poll, RelatedData
1414

1515

1616
class ChoiceAdmin(DjangoObjectActions, admin.ModelAdmin):
@@ -110,7 +110,7 @@ def delete_all_choices(self, request, obj):
110110

111111
if request.method == "POST":
112112
obj.choice_set.all().delete()
113-
return
113+
return None
114114

115115
self.message_user(request, "All choices deleted")
116116
return render(request, "clear_choices.html", {"object": obj})

example_project/polls/factories.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ class Meta:
4343

4444
def get_random_string(length):
4545
letters = string.ascii_lowercase
46-
result_str = "".join(random.choice(letters) for i in range(length))
47-
return result_str
46+
return "".join(random.choice(letters) for i in range(length))
4847

4948

5049
class RelatedDataFactory(factory.django.DjangoModelFactory):

example_project/polls/migrations/0001_initial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Generated by Django 1.9.2 on 2016-02-25 17:25
22

3-
from django.db import migrations, models
43
import django.db.models.deletion
4+
from django.db import migrations, models
55

66

77
class Migration(migrations.Migration):

example_project/polls/migrations/0002_auto_20200805_0239.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Generated by Django 3.1 on 2020-08-05 02:39
22

3-
from django.db import migrations, models
43
import uuid
54

5+
from django.db import migrations, models
6+
67

78
class Migration(migrations.Migration):
89
dependencies = [

example_project/urls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from django.urls import path
21
from django.contrib import admin
3-
from example_project.polls.admin import support_admin
2+
from django.urls import path
43

4+
from example_project.polls.admin import support_admin
55

66
urlpatterns = [
77
path("admin/", admin.site.urls),

0 commit comments

Comments
 (0)