From 4a2034e71254a36f105f361d9da8297ae615fee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Quiroz?= Date: Tue, 30 Mar 2021 14:39:32 -0300 Subject: [PATCH 1/8] Add all default methods of view class to is_model_view_subclass_method_shouldnt_be_function --- pylint_django/augmentations/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pylint_django/augmentations/__init__.py b/pylint_django/augmentations/__init__.py index 1dc332a3..0216de3c 100644 --- a/pylint_django/augmentations/__init__.py +++ b/pylint_django/augmentations/__init__.py @@ -630,8 +630,8 @@ def is_attribute(node): def is_model_view_subclass_method_shouldnt_be_function(node): - """Checks that node is get or post method of the View class.""" - if node.name not in ('get', 'post'): + """Checks that node is a default http method (i.e get, post, put, and more) of the View class.""" + if node.name not in View.http_method_names: return False parent = node.parent From 32a980c65daa94916b3c5a1d39e27d8bc2ff3112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Quiroz?= Date: Tue, 30 Mar 2021 18:13:30 +0000 Subject: [PATCH 2/8] Add tests --- .../tests/input/func_noerror_classviews.py | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/pylint_django/tests/input/func_noerror_classviews.py b/pylint_django/tests/input/func_noerror_classviews.py index c34e738f..176234c2 100644 --- a/pylint_django/tests/input/func_noerror_classviews.py +++ b/pylint_django/tests/input/func_noerror_classviews.py @@ -21,13 +21,54 @@ def get_context_data(self, **kwargs): 'kwargs': self.kwargs } + +class JsonGetView(View): + def get(self, request): + # do something with objects but don't use + # self or request + return JsonResponse({'rc': 0, 'response': 'ok'}) -class JsonView(View): +class JsonPostView(View): def post(self, request): # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) +class JsonPutView(View): + def put(self, request): + # do something with objects but don't use + # self or request + return JsonResponse({'rc': 0, 'response': 'ok'}) + +class JsonPatchView(View): + def patch(self, request): + # do something with objects but don't use + # self or request + return JsonResponse({'rc': 0, 'response': 'ok'}) + +class JsonDeleteView(View): + def delete(self, request): + # do something with objects but don't use + # self or request + return JsonResponse({'rc': 0, 'response': 'ok'}) + +class JsonHeadView(View): + def head(self, request): + # do something with objects but don't use + # self or request + return JsonResponse({'rc': 0, 'response': 'ok'}) + +class JsonOptionsSView(View): + def options(self, request): + # do something with objects but don't use + # self or request + return JsonResponse({'rc': 0, 'response': 'ok'}) + +class JsonTraceView(View): + def trace(self, request): + # do something with objects but don't use + # self or request + return JsonResponse({'rc': 0, 'response': 'ok'}) class Book(models.Model): name = models.CharField(max_length=100) From 3216a70dde45e7ff6ad9003af678a997e4b68105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Quiroz?= Date: Tue, 30 Mar 2021 18:26:09 +0000 Subject: [PATCH 3/8] Fix typo on JsonOptionsSView -> JsonOptionsView --- pylint_django/tests/input/func_noerror_classviews.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylint_django/tests/input/func_noerror_classviews.py b/pylint_django/tests/input/func_noerror_classviews.py index 176234c2..259aa90c 100644 --- a/pylint_django/tests/input/func_noerror_classviews.py +++ b/pylint_django/tests/input/func_noerror_classviews.py @@ -58,7 +58,7 @@ def head(self, request): # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) -class JsonOptionsSView(View): +class JsonOptionsView(View): def options(self, request): # do something with objects but don't use # self or request From 37a3ac8594a1a6c3fb4bd5421081c570b80f7c1d Mon Sep 17 00:00:00 2001 From: Alexander Todorov Date: Thu, 8 Apr 2021 14:17:23 +0300 Subject: [PATCH 4/8] Update changelog and contributors list --- CHANGELOG.rst | 4 +++- CONTRIBUTORS.md | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index d05f7681..f9ddfd69 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,7 +4,9 @@ Changelog Unreleased ---------- -- Start testing with Django 3.2 on Python 3.9 +- Start testing with Django 3.2 on Python 3.9 (Michael K.) +- Teach pylint-django about all HTTP methods from the View class, not only + ``get`` and ``post`` (Nicolás Quiroz) Version 2.4.2 (08 Jan 2021) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 0cdc307a..f0151e7f 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -16,4 +16,5 @@ * [WayneLambert](https://github.com/WayneLambert) * [alejandro-angulo](https://github.com/alejandro-angulo) * [brymut](https://github.com/brymut) - +* [michael-k](https://github.com/michael-k) +* [naquiroz](https://github.com/naquiroz) From e399e74de08f1a023f36d8eec5c4fc65c16de8f4 Mon Sep 17 00:00:00 2001 From: Alexander Todorov Date: Thu, 8 Apr 2021 15:07:55 +0300 Subject: [PATCH 5/8] Make flake8 happy --- pylint_django/tests/input/func_noerror_classviews.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pylint_django/tests/input/func_noerror_classviews.py b/pylint_django/tests/input/func_noerror_classviews.py index 259aa90c..a33eec73 100644 --- a/pylint_django/tests/input/func_noerror_classviews.py +++ b/pylint_django/tests/input/func_noerror_classviews.py @@ -21,55 +21,63 @@ def get_context_data(self, **kwargs): 'kwargs': self.kwargs } - + class JsonGetView(View): def get(self, request): # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) + class JsonPostView(View): def post(self, request): # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) + class JsonPutView(View): def put(self, request): # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) + class JsonPatchView(View): def patch(self, request): # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) + class JsonDeleteView(View): def delete(self, request): # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) + class JsonHeadView(View): def head(self, request): # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) + class JsonOptionsView(View): def options(self, request): # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) + class JsonTraceView(View): def trace(self, request): # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) + class Book(models.Model): name = models.CharField(max_length=100) good = models.BooleanField(default=False) From 364b26beb943e372a1813363c5222067694a16e3 Mon Sep 17 00:00:00 2001 From: Alexander Todorov Date: Thu, 8 Apr 2021 15:12:10 +0300 Subject: [PATCH 6/8] test: Fix method signatures to match Django otherwise pylint complains --- .../tests/input/func_noerror_classviews.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pylint_django/tests/input/func_noerror_classviews.py b/pylint_django/tests/input/func_noerror_classviews.py index a33eec73..075f6138 100644 --- a/pylint_django/tests/input/func_noerror_classviews.py +++ b/pylint_django/tests/input/func_noerror_classviews.py @@ -23,56 +23,56 @@ def get_context_data(self, **kwargs): class JsonGetView(View): - def get(self, request): + def get(self, request, *args, **kwargs): # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) class JsonPostView(View): - def post(self, request): + def post(self, request, *args, **kwargs): # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) class JsonPutView(View): - def put(self, request): + def put(self, request, *args, **kwargs): # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) class JsonPatchView(View): - def patch(self, request): + def patch(self, request, *args, **kwargs): # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) class JsonDeleteView(View): - def delete(self, request): + def delete(self, request, *args, **kwargs): # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) class JsonHeadView(View): - def head(self, request): + def head(self, request, *args, **kwargs): # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) class JsonOptionsView(View): - def options(self, request): + def options(self, request, *args, **kwargs): # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) class JsonTraceView(View): - def trace(self, request): + def trace(self, request, *args, **kwargs): # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'}) From f60f99de194c5bf0bcac2eaa83262a4e9542dfa6 Mon Sep 17 00:00:00 2001 From: Alexander Todorov Date: Thu, 8 Apr 2021 15:35:08 +0300 Subject: [PATCH 7/8] Ignore unused-argument for *args, **kwards in view method signatures --- CHANGELOG.rst | 1 + pylint_django/augmentations/__init__.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f9ddfd69..c12a328c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,7 @@ Unreleased - Start testing with Django 3.2 on Python 3.9 (Michael K.) - Teach pylint-django about all HTTP methods from the View class, not only ``get`` and ``post`` (Nicolás Quiroz) +- Ignore ``unused-argument`` for ``*args``, ``**kwards`` in view method signatures Version 2.4.2 (08 Jan 2021) diff --git a/pylint_django/augmentations/__init__.py b/pylint_django/augmentations/__init__.py index 0216de3c..770c51c4 100644 --- a/pylint_django/augmentations/__init__.py +++ b/pylint_django/augmentations/__init__.py @@ -652,7 +652,7 @@ def ignore_unused_argument_warnings_for_request(orig_method, self, stmt, name): The signature of Django view functions require the request argument but it is okay if the request is not used. This function should be used as a wrapper for the `VariablesChecker._is_name_ignored` method. """ - if name == 'request': + if name in ('request', 'args', 'kwargs'): return True return orig_method(self, stmt, name) From 06737e0145a55c4644ace2b0e7ba1c76c7a22fc2 Mon Sep 17 00:00:00 2001 From: Alexander Todorov Date: Thu, 8 Apr 2021 15:39:29 +0300 Subject: [PATCH 8/8] tests: Add pylint-disable Django's View class doesn't implement a head() method but instead uses self.head = ... if the child class doesn't override it. This leads to the method-hidden checker being triggered. --- pylint_django/tests/input/func_noerror_classviews.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylint_django/tests/input/func_noerror_classviews.py b/pylint_django/tests/input/func_noerror_classviews.py index 075f6138..f8320940 100644 --- a/pylint_django/tests/input/func_noerror_classviews.py +++ b/pylint_django/tests/input/func_noerror_classviews.py @@ -58,7 +58,7 @@ def delete(self, request, *args, **kwargs): class JsonHeadView(View): - def head(self, request, *args, **kwargs): + def head(self, request, *args, **kwargs): # pylint: disable=method-hidden # do something with objects but don't use # self or request return JsonResponse({'rc': 0, 'response': 'ok'})