@@ -644,23 +644,17 @@ def is_model_view_subclass_method_shouldnt_be_function(node):
644644 return parent is not None and node_is_subclass (parent , * subclass )
645645
646646
647- def is_model_view_subclass_unused_argument ( node ):
647+ def ignore_unused_argument_warnings_for_request ( orig_method , self , stmt , name ):
648648 """
649- Checks that node is get or post method of the View class and it has valid arguments .
649+ Ignore unused-argument warnings for function arguments named "request" .
650650
651- TODO: Bad checkings, need to be more smart.
651+ The signature of Django view functions require the request argument but it is okay if the request is not used.
652+ This function should be used as a wrapper for the `VariablesChecker._is_name_ignored` method.
652653 """
653- if not is_model_view_subclass_method_shouldnt_be_function (node ):
654- return False
655-
656- return is_argument_named_request (node )
654+ if name == 'request' :
655+ return True
657656
658-
659- def is_argument_named_request (node ):
660- """
661- If an unused-argument is named 'request' ignore that!
662- """
663- return 'request' in node .argnames ()
657+ return orig_method (self , stmt , name )
664658
665659
666660def is_model_field_display_method (node ):
@@ -742,7 +736,7 @@ def is_class(class_name):
742736def wrap (orig_method , with_method ):
743737 @functools .wraps (orig_method )
744738 def wrap_func (* args , ** kwargs ):
745- with_method (orig_method , * args , ** kwargs )
739+ return with_method (orig_method , * args , ** kwargs )
746740 return wrap_func
747741
748742
@@ -759,6 +753,31 @@ def pylint_newstyle_classdef_compat(linter, warning_name, augment):
759753 suppress_message (linter , getattr (NewStyleConflictChecker , 'visit_classdef' ), warning_name , augment )
760754
761755
756+ def apply_wrapped_augmentations ():
757+ """
758+ Apply augmentation and supression rules through monkey patching of pylint.
759+ """
760+ # NOTE: The monkey patching is done with wrap and needs to be done in a thread safe manner to support the
761+ # parallel option of pylint (-j).
762+ # This is achieved by comparing __name__ of the monkey patched object to the original value and only patch it if
763+ # these are equal.
764+
765+ # Unused argument 'request' (get, post)
766+ current_is_name_ignored = VariablesChecker ._is_name_ignored # pylint: disable=protected-access
767+ if current_is_name_ignored .__name__ == '_is_name_ignored' :
768+ # pylint: disable=protected-access
769+ VariablesChecker ._is_name_ignored = wrap (current_is_name_ignored , ignore_unused_argument_warnings_for_request )
770+
771+ # ForeignKey and OneToOneField
772+ current_leave_module = VariablesChecker .leave_module
773+ if current_leave_module .__name__ == 'leave_module' :
774+ # current_leave_module is not wrapped
775+ # Two threads may hit the next assignment concurrently, but the result is the same
776+ VariablesChecker .leave_module = wrap (current_leave_module , ignore_import_warnings_for_related_fields )
777+ # VariablesChecker.leave_module is now wrapped
778+ # else VariablesChecker.leave_module is already wrapped
779+
780+
762781# augment things
763782def apply_augmentations (linter ):
764783 """Apply augmentation and suppression rules."""
@@ -826,10 +845,6 @@ def apply_augmentations(linter):
826845 # Method could be a function (get, post)
827846 suppress_message (linter , ClassChecker .leave_functiondef , 'no-self-use' ,
828847 is_model_view_subclass_method_shouldnt_be_function )
829- # Unused argument 'request' (get, post)
830- suppress_message (linter , VariablesChecker .leave_functiondef , 'unused-argument' ,
831- is_model_view_subclass_unused_argument )
832- suppress_message (linter , VariablesChecker .leave_functiondef , 'unused-argument' , is_argument_named_request )
833848
834849 # django-mptt
835850 suppress_message (linter , DocStringChecker .visit_classdef , 'missing-docstring' , is_model_mpttmeta_subclass )
@@ -841,15 +856,7 @@ def apply_augmentations(linter):
841856 suppress_message (linter , TypeChecker .visit_attribute , 'no-member' , is_model_factory )
842857 suppress_message (linter , ClassChecker .visit_functiondef , 'no-self-argument' , is_factory_post_generation_method )
843858
844- # ForeignKey and OneToOneField
845- # Must update this in a thread safe way to support the parallel option on pylint (-j)
846- current_leave_module = VariablesChecker .leave_module
847- if current_leave_module .__name__ == 'leave_module' :
848- # current_leave_module is not wrapped
849- # Two threads may hit the next assignment concurrently, but the result is the same
850- VariablesChecker .leave_module = wrap (current_leave_module , ignore_import_warnings_for_related_fields )
851- # VariablesChecker.leave_module is now wrapped
852- # else VariablesChecker.leave_module is already wrapped
853-
854859 # wsgi.py
855860 suppress_message (linter , NameChecker .visit_assignname , 'invalid-name' , is_wsgi_application )
861+
862+ apply_wrapped_augmentations ()
0 commit comments