Skip to content

Commit 0bf3b65

Browse files
committed
Add test & fix for unused arguments on class based views
1 parent 24c3f31 commit 0bf3b65

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

pylint_django/augmentations/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -608,15 +608,17 @@ def is_attribute(node):
608608
def is_model_view_subclass_method_shouldnt_be_function(node):
609609
"""Checks that node is get or post method of the View class."""
610610
if node.name not in ('get', 'post'):
611-
612611
return False
613612

614613
parent = node.parent
615614
while parent and not isinstance(parent, ScopedClass):
616615
parent = parent.parent
617616

618-
subclass = '.View'
619-
return parent is not None and parent.name.endswith('View') and node_is_subclass(parent, subclass)
617+
subclass = ('django.views.View',
618+
'django.views.generic.View',
619+
'django.views.generic.base.View',)
620+
621+
return parent is not None and node_is_subclass(parent, *subclass)
620622

621623

622624
def is_model_view_subclass_unused_argument(node):
@@ -635,7 +637,7 @@ def is_argument_named_request(node):
635637
"""
636638
If an unused-argument is named 'request' ignore that!
637639
"""
638-
return 'request'in node.argnames()
640+
return 'request' in node.argnames()
639641

640642

641643
def is_model_field_display_method(node):
@@ -795,7 +797,7 @@ def apply_augmentations(linter):
795797

796798
# View
797799
# Method could be a function (get, post)
798-
suppress_message(linter, ClassChecker.leave_functiondef, 'R0201',
800+
suppress_message(linter, ClassChecker.leave_functiondef, 'no-self-use',
799801
is_model_view_subclass_method_shouldnt_be_function)
800802
# Unused argument 'request' (get, post)
801803
suppress_message(linter, VariablesChecker.leave_functiondef, 'unused-argument',

pylint_django/tests/input/func_noerror_classviews.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"""
55
# pylint: disable=missing-docstring
66

7-
from django.views.generic import TemplateView
7+
from django.http import JsonResponse
8+
from django.views.generic import TemplateView, View
89

910

1011
class BoringView(TemplateView):
@@ -15,3 +16,10 @@ def get_context_data(self, **kwargs):
1516
'args': self.args,
1617
'kwargs': self.kwargs
1718
}
19+
20+
21+
class JsonView(View):
22+
def post(self, request):
23+
# do something with objects but don't use
24+
# self or request
25+
return JsonResponse({'rc': 0, 'response': 'ok'})

0 commit comments

Comments
 (0)