Skip to content

Commit

Permalink
Merge 29d3f48 into d00dafb
Browse files Browse the repository at this point in the history
  • Loading branch information
psrb committed Oct 27, 2019
2 parents d00dafb + 29d3f48 commit d9266cf
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 25 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
* [matusvalo](https://github.com/matusvalo)
* [fadedDexofan](https://github.com/fadeddexofan)
* [imomaliev](https://github.com/imomaliev)
* [psrb](https://github.com/psrb)
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ a test name, and insert into it some code. The tests will run pylint
against these modules. If the idea is that no messages now occur, then
that is fine, just check to see if it works by running ``scripts/test.sh``.

You can pass any pytest commaned line argument to ``scripts/test.sh``.
For example if you want to debug the tests you can execute ``scripts/test.sh --capture=no``.

Ideally, add some pylint error suppression messages to the file to prevent
spurious warnings, since these are all tiny little modules not designed to
do anything so there's no need to be perfect.
Expand All @@ -132,7 +135,7 @@ It is possible to make tests with expected error output, for example, if
adding a new message or simply accepting that pylint is supposed to warn.
A ``test_file_name.txt`` file contains a list of expected error messages in the
format
``error-type:line number:class name or empty:1st line of detailed error text``.
``error-type:line number:class name or empty:1st line of detailed error text:confidence or empty``.


License
Expand Down
23 changes: 0 additions & 23 deletions pylint_django/augmentations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,25 +644,6 @@ def is_model_view_subclass_method_shouldnt_be_function(node):
return parent is not None and node_is_subclass(parent, *subclass)


def is_model_view_subclass_unused_argument(node):
"""
Checks that node is get or post method of the View class and it has valid arguments.
TODO: Bad checkings, need to be more smart.
"""
if not is_model_view_subclass_method_shouldnt_be_function(node):
return False

return is_argument_named_request(node)


def is_argument_named_request(node):
"""
If an unused-argument is named 'request' ignore that!
"""
return 'request' in node.argnames()


def is_model_field_display_method(node):
"""Accept model's fields with get_*_display names."""
if not node.attrname.endswith('_display'):
Expand Down Expand Up @@ -826,10 +807,6 @@ def apply_augmentations(linter):
# Method could be a function (get, post)
suppress_message(linter, ClassChecker.leave_functiondef, 'no-self-use',
is_model_view_subclass_method_shouldnt_be_function)
# Unused argument 'request' (get, post)
suppress_message(linter, VariablesChecker.leave_functiondef, 'unused-argument',
is_model_view_subclass_unused_argument)
suppress_message(linter, VariablesChecker.leave_functiondef, 'unused-argument', is_argument_named_request)

# django-mptt
suppress_message(linter, DocStringChecker.visit_classdef, 'missing-docstring', is_model_mpttmeta_subclass)
Expand Down
13 changes: 13 additions & 0 deletions pylint_django/plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Common Django module."""
import re
from pylint.checkers.base import NameChecker
from pylint.checkers.variables import VariablesChecker
from pylint_plugin_utils import get_checker

from pylint_django.checkers import register_checkers
Expand All @@ -17,6 +19,17 @@ def load_configuration(linter):
name_checker = get_checker(linter, NameChecker)
name_checker.config.good_names += ('qs', 'urlpatterns', 'register', 'app_name', 'handler500')

# We want to ignore the unused-argument warning for arguments named `request`. The signature of Django view
# functions require the request argument but it is okay if the request is not used in the function.
# https://github.com/PyCQA/pylint-django/issues/155
variables_checker = get_checker(linter, VariablesChecker)
old_ignored_argument_names_pattern = variables_checker.config.ignored_argument_names.pattern
new_ignored_argument_names_pattern = "request"
if old_ignored_argument_names_pattern:
new_ignored_argument_names_pattern = old_ignored_argument_names_pattern + "|request"

variables_checker.config.ignored_argument_names = re.compile(new_ignored_argument_names_pattern)

# we don't care about South migrations
linter.config.black_list += ('migrations', 'south_migrations')

Expand Down
19 changes: 19 additions & 0 deletions pylint_django/tests/input/func_unused_arguments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
Checks that Pylint still complains about unused-arguments if a function/method
contains an argument named `request`.
"""
# pylint: disable=missing-docstring

from django.http import JsonResponse
from django.views import View


def user_detail(request, user_id): # [unused-argument]
# nothing is done with user_id
return JsonResponse({'username': 'steve'})


class UserView(View):
def get(self, request, user_id): #[unused-argument]
# nothing is done with user_id
return JsonResponse({'username': 'steve'})
2 changes: 2 additions & 0 deletions pylint_django/tests/input/func_unused_arguments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
unused-argument:11:user_detail:Unused argument 'user_id':HIGH
unused-argument:17:UserView.get:Unused argument 'user_id':INFERENCE
2 changes: 2 additions & 0 deletions pylint_django/tests/test_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class PylintDjangoLintModuleTest(test_functional.LintModuleTest):
def __init__(self, test_file):
super(PylintDjangoLintModuleTest, self).__init__(test_file)
self._linter.load_plugin_modules(['pylint_django'])
self._linter.load_plugin_configuration()


class PylintDjangoDbPerformanceTest(PylintDjangoLintModuleTest):
Expand All @@ -32,6 +33,7 @@ class PylintDjangoDbPerformanceTest(PylintDjangoLintModuleTest):
def __init__(self, test_file):
super(PylintDjangoDbPerformanceTest, self).__init__(test_file)
self._linter.load_plugin_modules(['pylint_django.checkers.db_performance'])
self._linter.load_plugin_configuration()


def get_tests(input_dir='input', sort=False):
Expand Down
2 changes: 1 addition & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
python pylint_django/tests/test_func.py -v
python pylint_django/tests/test_func.py -v "$@"

0 comments on commit d9266cf

Please sign in to comment.