@@ -644,23 +644,17 @@ def is_model_view_subclass_method_shouldnt_be_function(node):
644
644
return parent is not None and node_is_subclass (parent , * subclass )
645
645
646
646
647
- def is_model_view_subclass_unused_argument ( node ):
647
+ def ignore_unused_argument_warnings_for_request ( orig_method , self , stmt , name ):
648
648
"""
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" .
650
650
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.
652
653
"""
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
657
656
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 )
664
658
665
659
666
660
def is_model_field_display_method (node ):
@@ -742,7 +736,7 @@ def is_class(class_name):
742
736
def wrap (orig_method , with_method ):
743
737
@functools .wraps (orig_method )
744
738
def wrap_func (* args , ** kwargs ):
745
- with_method (orig_method , * args , ** kwargs )
739
+ return with_method (orig_method , * args , ** kwargs )
746
740
return wrap_func
747
741
748
742
@@ -759,6 +753,31 @@ def pylint_newstyle_classdef_compat(linter, warning_name, augment):
759
753
suppress_message (linter , getattr (NewStyleConflictChecker , 'visit_classdef' ), warning_name , augment )
760
754
761
755
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
+
762
781
# augment things
763
782
def apply_augmentations (linter ):
764
783
"""Apply augmentation and suppression rules."""
@@ -826,10 +845,6 @@ def apply_augmentations(linter):
826
845
# Method could be a function (get, post)
827
846
suppress_message (linter , ClassChecker .leave_functiondef , 'no-self-use' ,
828
847
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 )
833
848
834
849
# django-mptt
835
850
suppress_message (linter , DocStringChecker .visit_classdef , 'missing-docstring' , is_model_mpttmeta_subclass )
@@ -841,15 +856,7 @@ def apply_augmentations(linter):
841
856
suppress_message (linter , TypeChecker .visit_attribute , 'no-member' , is_model_factory )
842
857
suppress_message (linter , ClassChecker .visit_functiondef , 'no-self-argument' , is_factory_post_generation_method )
843
858
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
-
854
859
# wsgi.py
855
860
suppress_message (linter , NameChecker .visit_assignname , 'invalid-name' , is_wsgi_application )
861
+
862
+ apply_wrapped_augmentations ()
0 commit comments