Skip to content

Commit

Permalink
Monkey patch VariablesChecker._is_name_ignored (Fixes pylint-dev#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
psrb committed Nov 2, 2019
1 parent c817213 commit 5e8ae71
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
20 changes: 19 additions & 1 deletion pylint_django/augmentations/__init__.py
Expand Up @@ -644,6 +644,19 @@ def is_model_view_subclass_method_shouldnt_be_function(node):
return parent is not None and node_is_subclass(parent, *subclass)


def ignore_unused_argument_warnings_for_request(orig_method, self, stmt, name):
"""
Ignore unused-argument warnings for function arguments named "request".
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':
return True

return orig_method(self, stmt, name)


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 @@ -723,7 +736,7 @@ def is_class(class_name):
def wrap(orig_method, with_method):
@functools.wraps(orig_method)
def wrap_func(*args, **kwargs):
with_method(orig_method, *args, **kwargs)
return with_method(orig_method, *args, **kwargs)
return wrap_func


Expand Down Expand Up @@ -807,6 +820,11 @@ 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)
current_is_name_ignored = VariablesChecker._is_name_ignored # pylint: disable=protected-access
if current_is_name_ignored.__name__ == '_is_name_ignored':
# pylint: disable=protected-access
VariablesChecker._is_name_ignored = wrap(current_is_name_ignored, ignore_unused_argument_warnings_for_request)

# django-mptt
suppress_message(linter, DocStringChecker.visit_classdef, 'missing-docstring', is_model_mpttmeta_subclass)
Expand Down
13 changes: 0 additions & 13 deletions pylint_django/plugin.py
@@ -1,7 +1,5 @@
"""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 @@ -19,17 +17,6 @@ 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
6 changes: 6 additions & 0 deletions pylint_django/tests/input/func_unused_arguments.py
Expand Up @@ -7,6 +7,12 @@
from django.http import JsonResponse
from django.views import View

# Pylint generates the warning `redefined-outer-name` if an argument name shadows
# a variable name from an outer scope. But if that argument name is ignored this
# warning will not be generated.
# Therefore define request here to cover this behaviour in this test case.

request = None # pylint: disable=invalid-name

def user_detail(request, user_id): # [unused-argument]
# nothing is done with user_id
Expand Down
4 changes: 2 additions & 2 deletions pylint_django/tests/input/func_unused_arguments.txt
@@ -1,2 +1,2 @@
unused-argument:11:user_detail:Unused argument 'user_id':HIGH
unused-argument:17:UserView.get:Unused argument 'user_id':INFERENCE
unused-argument:17:user_detail:Unused argument 'user_id':HIGH
unused-argument:23:UserView.get:Unused argument 'user_id':INFERENCE

0 comments on commit 5e8ae71

Please sign in to comment.