-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy path__init__.py
1003 lines (824 loc) · 29.4 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Augmentations."""
# pylint: disable=invalid-name
import functools
import itertools
from astroid import InferenceError
from astroid.nodes import Attribute, ClassDef, ImportFrom
from astroid.nodes.scoped_nodes import ClassDef as ScopedClass
from astroid.nodes.scoped_nodes import Module
from astroid.objects import Super
from django import VERSION as django_version
from django.utils import termcolors
from django.views.generic.base import ContextMixin, RedirectView, View
from django.views.generic.dates import (
DateMixin,
DayMixin,
MonthMixin,
WeekMixin,
YearMixin,
)
from django.views.generic.detail import (
SingleObjectMixin,
SingleObjectTemplateResponseMixin,
TemplateResponseMixin,
)
from django.views.generic.edit import DeletionMixin, FormMixin, ModelFormMixin
from django.views.generic.list import (
MultipleObjectMixin,
MultipleObjectTemplateResponseMixin,
)
from pylint.checkers.base import DocStringChecker, NameChecker
from pylint.checkers.classes import ClassChecker
from pylint.checkers.design_analysis import MisdesignChecker
from pylint.checkers.newstyle import NewStyleConflictChecker
from pylint.checkers.typecheck import TypeChecker
from pylint.checkers.variables import ScopeConsumer, VariablesChecker
from pylint_plugin_utils import augment_visit, suppress_message
from pylint_django.utils import PY3, node_is_subclass
# Note: it would have been nice to import the Manager object from Django and
# get its attributes that way - and this used to be the method - but unfortunately
# there's no guarantee that Django is properly configured at that stage, and importing
# anything from the django.db package causes an ImproperlyConfigured exception.
# Therefore we'll fall back on a hard-coded list of attributes which won't be as accurate,
# but this is not 100% accurate anyway.
MANAGER_ATTRS = {
"none",
"all",
"count",
"dates",
"distinct",
"extra",
"get",
"get_or_create",
"update_or_create",
"get_queryset",
"create",
"bulk_create",
"filter",
"aggregate",
"annotate",
"complex_filter",
"exclude",
"in_bulk",
"iterator",
"latest",
"order_by",
"select_for_update",
"select_related",
"prefetch_related",
"values",
"values_list",
"update",
"reverse",
"defer",
"only",
"using",
"exists",
}
QS_ATTRS = {
"filter",
"exclude",
"annotate",
"order_by",
"reverse",
"distinct",
"values",
"values_list",
"dates",
"datetimes",
"none",
"all",
"select_related",
"prefetch_related",
"extra",
"defer",
"only",
"using",
"select_for_update",
"raw",
"get",
"create",
"get_or_create",
"update_or_create",
"bulk_create",
"count",
"in_bulk",
"iterator",
"latest",
"earliest",
"first",
"last",
"aggregate",
"exists",
"update",
"delete",
"as_manager",
"expression",
"output_field",
}
MODELADMIN_ATTRS = {
# options
"actions",
"actions_on_top",
"actions_on_bottom",
"actions_selection_counter",
"date_hierarchy",
"empty_value_display",
"exclude",
"fields",
"fieldsets",
"filter_horizontal",
"filter_vertical",
"form",
"formfield_overrides",
"inlines",
"list_display",
"list_display_links",
"list_editable",
"list_filter",
"list_max_show_all",
"list_per_page",
"list_select_related",
"ordering",
"paginator",
"prepopulated_fields",
"preserve_filters",
"radio_fields",
"raw_id_fields",
"readonly_fields",
"save_as",
"save_on_top",
"search_fields",
"show_full_result_count",
"view_on_site",
# template options
"add_form_template",
"change_form_template",
"change_list_template",
"delete_confirmation_template",
"delete_selected_confirmation_template",
"object_history_template",
}
MODEL_ATTRS = {
"id",
"DoesNotExist",
"MultipleObjectsReturned",
"_base_manager",
"_default_manager",
"_meta",
"delete",
"get_next_by_date",
"get_previous_by_date",
"objects",
"save",
}
FIELD_ATTRS = {
"null",
"blank",
"choices",
"db_column",
"db_index",
"db_tablespace",
"default",
"editable",
"error_messages",
"help_text",
"primary_key",
"unique",
"unique_for_date",
"unique_for_month",
"unique_for_year",
"verbose_name",
"validators",
}
CHAR_FIELD_ATTRS = {
"max_length",
}
DATE_FIELD_ATTRS = {
"auto_now",
"auto_now_add",
}
DECIMAL_FIELD_ATTRS = {
"max_digits",
"decimal_places",
}
FILE_FIELD_ATTRS = {
"upload_to",
"storage",
}
IMAGE_FIELD_ATTRS = {
"height_field",
"width_field",
}
IP_FIELD_ATTRS = {
"protocol",
"unpack_ipv4",
}
SLUG_FIELD_ATTRS = {
"allow_unicode",
}
FOREIGNKEY_FIELD_ATTRS = {
"limit_choices_to",
"related_name",
"related_query_name",
"to_field",
"db_constraint",
"swappable",
}
MANYTOMANY_FIELD_ATTRS = {
"add",
"clear",
"related_name",
"related_query_name",
"remove",
"set",
"limit_choices_to",
"symmetrical",
"through",
"through_fields",
"db_table",
"db_constraint",
"swappable",
}
ONETOONE_FIELD_ATTRS = {
"parent_link",
}
STYLE_ATTRS = set(itertools.chain.from_iterable(termcolors.PALETTES.values()))
VIEW_ATTRS = {
(
(
f"{cls.__module__}.{cls.__name__}",
f".{cls.__name__}",
),
tuple(cls.__dict__.keys()),
)
for cls in (
View,
RedirectView,
ContextMixin,
DateMixin,
DayMixin,
MonthMixin,
WeekMixin,
YearMixin,
SingleObjectMixin,
SingleObjectTemplateResponseMixin,
TemplateResponseMixin,
DeletionMixin,
FormMixin,
ModelFormMixin,
MultipleObjectMixin,
MultipleObjectTemplateResponseMixin,
)
}
FORM_ATTRS = {
"declared_fields",
}
def ignore_import_warnings_for_related_fields(orig_method, self, node):
"""
Replaces the leave_module method on the VariablesChecker class to
prevent unused-import warnings which are caused by the ForeignKey
and OneToOneField transformations. By replacing the nodes in the
AST with their type rather than the django field, imports of the
form 'from django.db.models import OneToOneField' raise an unused-import
warning
"""
consumer = self._to_consume[0] # pylint: disable=W0212
# we can disable this warning ('Access to a protected member _to_consume of a client class')
# as it's not actually a client class, but rather, this method is being monkey patched
# onto the class and so the access is valid
new_things = {}
iterat = consumer.to_consume.items if PY3 else consumer.to_consume.iteritems
for name, stmts in iterat():
if isinstance(stmts[0], ImportFrom):
if any(n[0] in ("ForeignKey", "OneToOneField") for n in stmts[0].names):
continue
new_things[name] = stmts
# ScopeConsumer changed between pylint 2.12 and 2.13
# see https://github.com/PyCQA/pylint/issues/5970#issuecomment-1078778393
if hasattr(consumer, "consumed_uncertain"):
# this is pylint >= 2.13, and the ScopeConsumer tuple has an additional field
sc_args = (new_things, consumer.consumed, consumer.consumed_uncertain, consumer.scope_type)
else:
# this is <2.13 and does not have the consumer_uncertain field
sc_args = (new_things, consumer.consumed, consumer.scope_type)
consumer._atomic = ScopeConsumer(*sc_args) # pylint: disable=W0212
self._to_consume = [consumer] # pylint: disable=W0212
return orig_method(self, node)
def foreign_key_sets(chain, node):
"""
When a Django model has a ForeignKey to another model, the target
of the foreign key gets a '<modelname>_set' attribute for accessing
a queryset of the model owning the foreign key - eg:
class ModelA(models.Model):
pass
class ModelB(models.Model):
a = models.ForeignKey(ModelA)
Now, ModelA instances will have a modelb_set attribute.
It's also possible to explicitly name the relationship using the related_name argument
to the ForeignKey constructor. As it's impossible to know this without inspecting all
models before processing, we'll instead do a "best guess" approach and see if the attribute
being accessed goes on to be used as a queryset. This is via 'duck typing': if the method
called on the attribute being accessed is something we might find in a queryset, we'll
warn.
"""
quack = False
if node.attrname in MANAGER_ATTRS or node.attrname.endswith("_set"):
# if this is a X_set method, that's a pretty strong signal that this is the default
# Django name, rather than one set by related_name
quack = True
else:
# we will
if isinstance(node.parent, Attribute):
func_name = getattr(node.parent, "attrname", None)
if func_name in MANAGER_ATTRS:
quack = True
if quack:
children = list(node.get_children())
for child in children:
try:
inferred_cls = child.inferred()
except InferenceError:
pass
else:
for cls in inferred_cls:
if node_is_subclass(
cls,
"django.db.models.manager.Manager",
"django.db.models.base.Model",
".Model",
"django.db.models.fields.related.ForeignObject",
):
# This means that we are looking at a subclass of models.Model
# and something is trying to access a <something>_set attribute.
# Since this could exist, we will return so as not to raise an
# error.
return
chain()
def foreign_key_ids(chain, node):
if node.attrname.endswith("_id"):
return
chain()
def is_model_admin_subclass(node):
"""Checks that node is derivative of ModelAdmin class."""
if node.name[-5:] != "Admin" or isinstance(node.parent, ClassDef):
return False
return node_is_subclass(node, "django.contrib.admin.options.ModelAdmin")
def is_model_media_subclass(node):
"""Checks that node is derivative of Media class."""
if node.name != "Media" or not isinstance(node.parent, ClassDef):
return False
parents = (
"django.contrib.admin.options.ModelAdmin",
"django.forms.widgets.Media",
"django.db.models.base.Model",
".Model", # for the transformed version used in this plugin
"django.forms.forms.Form",
".Form",
"django.forms.widgets.Widget",
".Widget",
"django.forms.models.ModelForm",
".ModelForm",
)
return node_is_subclass(node.parent, *parents)
def is_model_meta_subclass(node):
"""Checks that node is derivative of Meta class."""
if node.name != "Meta" or not isinstance(node.parent, ClassDef):
return False
parents = (
".Model", # for the transformed version used here
"django.db.models.base.Model",
".Form",
"django.forms.forms.Form",
".ModelForm",
"django.forms.models.ModelForm",
"rest_framework.serializers.BaseSerializer",
"rest_framework.generics.GenericAPIView",
"rest_framework.viewsets.ReadOnlyModelViewSet",
"rest_framework.viewsets.ModelViewSet",
"django_filters.filterset.FilterSet",
"factory.django.DjangoModelFactory",
)
return node_is_subclass(node.parent, *parents)
def is_model_factory(node):
"""Checks that node is derivative of DjangoModelFactory or SubFactory class."""
try:
parent_classes = node.expr.inferred()
except: # noqa: E722, pylint: disable=bare-except
return False
parents = (
"factory.declarations.LazyFunction",
"factory.declarations.SubFactory",
"factory.django.DjangoModelFactory",
)
for parent_class in parent_classes:
try:
if parent_class.qname() in parents:
return True
if node_is_subclass(parent_class, *parents):
return True
except AttributeError:
continue
return False
def is_factory_post_generation_method(node):
if not node.decorators:
return False
for decorator in node.decorators.get_children():
try:
inferred = decorator.inferred()
except InferenceError:
continue
for target in inferred:
if target.qname() == "factory.helpers.post_generation":
return True
return False
def is_model_mpttmeta_subclass(node):
"""Checks that node is derivative of MPTTMeta class."""
if node.name != "MPTTMeta" or not isinstance(node.parent, ClassDef):
return False
parents = (
"django.db.models.base.Model",
".Model", # for the transformed version used in this plugin
"django.forms.forms.Form",
".Form",
"django.forms.models.ModelForm",
".ModelForm",
)
return node_is_subclass(node.parent, *parents)
def _attribute_is_magic(node, attrs, parents):
"""Checks that node is an attribute used inside one of allowed parents"""
if node.attrname not in attrs:
return False
if not node.last_child():
return False
try:
for cls in node.last_child().inferred():
if isinstance(cls, Super):
cls = cls._self_class # pylint: disable=protected-access
if node_is_subclass(cls, *parents) or cls.qname() in parents:
return True
except InferenceError:
pass
return False
def is_style_attribute(node):
parents = ("django.core.management.color.Style",)
return _attribute_is_magic(node, STYLE_ATTRS, parents)
def is_manager_attribute(node):
"""Checks that node is attribute of Manager or QuerySet class."""
parents = (
"django.db.models.manager.Manager",
".Manager",
"factory.base.BaseFactory.build",
"django.db.models.query.QuerySet",
".QuerySet",
)
return _attribute_is_magic(node, MANAGER_ATTRS.union(QS_ATTRS), parents)
def is_admin_attribute(node):
"""Checks that node is attribute of BaseModelAdmin."""
parents = ("django.contrib.admin.options.BaseModelAdmin", ".BaseModelAdmin")
return _attribute_is_magic(node, MODELADMIN_ATTRS, parents)
def is_model_attribute(node):
"""Checks that node is attribute of Model."""
parents = ("django.db.models.base.Model", ".Model")
return _attribute_is_magic(node, MODEL_ATTRS, parents)
def is_field_attribute(node):
"""Checks that node is attribute of Field."""
parents = ("django.db.models.fields.Field", ".Field")
return _attribute_is_magic(node, FIELD_ATTRS, parents)
def is_charfield_attribute(node):
"""Checks that node is attribute of CharField."""
parents = ("django.db.models.fields.CharField", ".CharField")
return _attribute_is_magic(node, CHAR_FIELD_ATTRS, parents)
def is_datefield_attribute(node):
"""Checks that node is attribute of DateField."""
parents = ("django.db.models.fields.DateField", ".DateField")
return _attribute_is_magic(node, DATE_FIELD_ATTRS, parents)
def is_decimalfield_attribute(node):
"""Checks that node is attribute of DecimalField."""
parents = ("django.db.models.fields.DecimalField", ".DecimalField")
return _attribute_is_magic(node, DECIMAL_FIELD_ATTRS, parents)
def is_filefield_attribute(node):
"""Checks that node is attribute of FileField."""
parents = ("django.db.models.fields.files.FileField", ".FileField")
return _attribute_is_magic(node, FILE_FIELD_ATTRS, parents)
def is_imagefield_attribute(node):
"""Checks that node is attribute of ImageField."""
parents = ("django.db.models.fields.files.ImageField", ".ImageField")
return _attribute_is_magic(node, IMAGE_FIELD_ATTRS, parents)
def is_ipfield_attribute(node):
"""Checks that node is attribute of GenericIPAddressField."""
parents = (
"django.db.models.fields.GenericIPAddressField",
".GenericIPAddressField",
)
return _attribute_is_magic(node, IP_FIELD_ATTRS, parents)
def is_slugfield_attribute(node):
"""Checks that node is attribute of SlugField."""
parents = ("django.db.models.fields.SlugField", ".SlugField")
return _attribute_is_magic(node, SLUG_FIELD_ATTRS, parents)
def is_foreignkeyfield_attribute(node):
"""Checks that node is attribute of ForeignKey."""
parents = ("django.db.models.fields.related.ForeignKey", ".ForeignKey")
return _attribute_is_magic(node, FOREIGNKEY_FIELD_ATTRS, parents)
def is_manytomanyfield_attribute(node):
"""Checks that node is attribute of ManyToManyField."""
parents = ("django.db.models.fields.related.ManyToManyField", ".ManyToManyField")
return _attribute_is_magic(node, MANYTOMANY_FIELD_ATTRS, parents)
def is_onetoonefield_attribute(node):
"""Checks that node is attribute of OneToOneField."""
parents = ("django.db.models.fields.related.OneToOneField", ".OneToOneField")
return _attribute_is_magic(node, ONETOONE_FIELD_ATTRS, parents)
def is_form_attribute(node):
"""Checks that node is attribute of Form."""
parents = ("django.forms.forms.Form", "django.forms.models.ModelForm")
return _attribute_is_magic(node, FORM_ATTRS, parents)
def is_model_test_case_subclass(node):
"""Checks that node is derivative of TestCase class."""
if not node.name.endswith("Test") and not isinstance(node.parent, ClassDef):
return False
return node_is_subclass(node, "django.test.testcases.TestCase")
class IsAttribute: # pylint: disable=too-few-public-methods
def __init__(self, parents, attrs):
self.parents = parents
self.attrs = attrs
def __call__(self, node):
return _attribute_is_magic(node, self.attrs, self.parents)
def is_model_view_subclass_method_shouldnt_be_function(node):
"""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
while parent and not isinstance(parent, ScopedClass):
parent = parent.parent
subclass = (
"django.views.View",
"django.views.generic.View",
"django.views.generic.base.View",
)
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 in ("request", "args", "kwargs"):
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"):
return False
if not node.attrname.startswith("get_"):
return False
if node.last_child():
# TODO: could validate the names of the fields on the model rather than
# blindly accepting get_*_display
try:
for cls in node.last_child().inferred():
if node_is_subclass(cls, "django.db.models.base.Model", ".Model"):
return True
except InferenceError:
return False
return False
def is_model_media_valid_attributes(node):
"""Suppress warnings for valid attributes of Media class."""
if node.name not in ("js",):
return False
parent = node.parent
while parent and not isinstance(parent, ScopedClass):
parent = parent.parent
if parent is None or parent.name != "Media":
return False
return True
def is_templatetags_module_valid_constant(node):
"""Suppress warnings for valid constants in templatetags module."""
if node.name not in ("register",):
return False
parent = node.parent
while not isinstance(parent, Module):
parent = parent.parent
if "templatetags." not in parent.name:
return False
return True
def is_urls_module_valid_constant(node):
"""Suppress warnings for valid constants in urls module."""
if node.name not in ("urlpatterns", "app_name"):
return False
parent = node.parent
while not isinstance(parent, Module):
parent = parent.parent
if not parent.name.endswith("urls"):
return False
return True
def allow_meta_protected_access(node):
if django_version >= (1, 8):
return node.attrname == "_meta"
return False
class IsClass: # pylint: disable=too-few-public-methods
def __init__(self, class_name):
self.class_name = class_name
def __call__(self, node):
return node_is_subclass(node, self.class_name)
def wrap(orig_method, with_method):
@functools.wraps(orig_method)
def wrap_func(*args, **kwargs):
return with_method(orig_method, *args, **kwargs)
return wrap_func
def is_wsgi_application(node):
frame = node.frame()
return (
node.name == "application"
and isinstance(frame, Module)
and (
frame.name == "asgi"
or frame.path[0].endswith("asgi.py")
or frame.file.endswith("asgi.py")
or frame.name == "wsgi"
or frame.path[0].endswith("wsgi.py")
or frame.file.endswith("wsgi.py")
)
)
# Compat helpers
def pylint_newstyle_classdef_compat(linter, warning_name, augment):
if not hasattr(NewStyleConflictChecker, "visit_classdef"):
return
suppress_message(
linter,
getattr(NewStyleConflictChecker, "visit_classdef"),
warning_name,
augment,
)
def apply_wrapped_augmentations():
"""
Apply augmentation and suppression rules through monkey patching of pylint.
"""
# NOTE: The monkey patching is done with wrap and needs to be done in a thread safe manner to support the
# parallel option of pylint (-j).
# This is achieved by comparing __name__ of the monkey patched object to the original value and only patch it if
# these are equal.
# 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)
# ForeignKey and OneToOneField
current_leave_module = VariablesChecker.leave_module
if current_leave_module.__name__ == "leave_module":
# current_leave_module is not wrapped
# Two threads may hit the next assignment concurrently, but the result is the same
VariablesChecker.leave_module = wrap(current_leave_module, ignore_import_warnings_for_related_fields)
# VariablesChecker.leave_module is now wrapped
# else VariablesChecker.leave_module is already wrapped
# augment things
def apply_augmentations(linter):
"""Apply augmentation and suppression rules."""
augment_visit(linter, TypeChecker.visit_attribute, foreign_key_sets)
augment_visit(linter, TypeChecker.visit_attribute, foreign_key_ids)
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_model_field_display_method)
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_style_attribute)
suppress_message(
linter,
NameChecker.visit_assignname,
"invalid-name",
is_urls_module_valid_constant,
)
# supress errors when accessing magical class attributes
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_manager_attribute)
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_admin_attribute)
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_model_attribute)
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_field_attribute)
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_charfield_attribute)
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_datefield_attribute)
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_decimalfield_attribute)
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_filefield_attribute)
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_imagefield_attribute)
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_ipfield_attribute)
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_slugfield_attribute)
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_foreignkeyfield_attribute)
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_manytomanyfield_attribute)
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_onetoonefield_attribute)
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_form_attribute)
for parents, attrs in VIEW_ATTRS:
suppress_message(
linter,
TypeChecker.visit_attribute,
"no-member",
IsAttribute(parents, attrs),
)
# formviews have too many ancestors, there's nothing the user of the library can do about that
suppress_message(
linter,
MisdesignChecker.visit_classdef,
"too-many-ancestors",
IsClass("django.views.generic.edit.FormView"),
)
# class-based generic views just have a longer inheritance chain
suppress_message(
linter,
MisdesignChecker.visit_classdef,
"too-many-ancestors",
IsClass("django.views.generic.detail.BaseDetailView"),
)
suppress_message(
linter,
MisdesignChecker.visit_classdef,
"too-many-ancestors",
IsClass("django.views.generic.edit.ProcessFormView"),
)
# ModelViewSet also suffers from too many ancestors
suppress_message(
linter,
MisdesignChecker.visit_classdef,
"too-many-ancestors",
IsClass("rest_framework.viewsets.ModelViewSet"),
)
# model forms have no __init__ method anywhere in their bases
suppress_message(
linter,
ClassChecker.visit_classdef,
"W0232",
IsClass("django.forms.models.ModelForm"),
)
# Meta
suppress_message(
linter,
DocStringChecker.visit_classdef,
"missing-docstring",
is_model_meta_subclass,
)
pylint_newstyle_classdef_compat(linter, "old-style-class", is_model_meta_subclass)
suppress_message(linter, ClassChecker.visit_classdef, "no-init", is_model_meta_subclass)
suppress_message(
linter,
MisdesignChecker.leave_classdef,
"too-few-public-methods",
is_model_meta_subclass,
)
suppress_message(
linter,
ClassChecker.visit_attribute,
"protected-access",
allow_meta_protected_access,
)
# Media
suppress_message(linter, NameChecker.visit_assignname, "C0103", is_model_media_valid_attributes)
suppress_message(
linter,
DocStringChecker.visit_classdef,
"missing-docstring",
is_model_media_subclass,
)
pylint_newstyle_classdef_compat(linter, "old-style-class", is_model_media_subclass)
suppress_message(linter, ClassChecker.visit_classdef, "no-init", is_model_media_subclass)
suppress_message(
linter,
MisdesignChecker.leave_classdef,
"too-few-public-methods",
is_model_media_subclass,
)
# Admin
# Too many public methods (40+/20)
# TODO: Count public methods of django.contrib.admin.options.ModelAdmin and increase
# MisdesignChecker.config.max_public_methods to this value to count only user' methods.
# nb_public_methods = 0
# for method in node.methods():
# if not method.name.startswith('_'):
# nb_public_methods += 1
suppress_message(linter, MisdesignChecker.leave_classdef, "R0904", is_model_admin_subclass)
# Tests
suppress_message(linter, MisdesignChecker.leave_classdef, "R0904", is_model_test_case_subclass)
# View
# Method could be a function (get, post)
suppress_message(
linter,
ClassChecker.leave_functiondef,
"no-self-use",
is_model_view_subclass_method_shouldnt_be_function,
)
# django-mptt
suppress_message(
linter,
DocStringChecker.visit_classdef,
"missing-docstring",
is_model_mpttmeta_subclass,
)
pylint_newstyle_classdef_compat(linter, "old-style-class", is_model_mpttmeta_subclass)
suppress_message(linter, ClassChecker.visit_classdef, "W0232", is_model_mpttmeta_subclass)
suppress_message(
linter,
MisdesignChecker.leave_classdef,
"too-few-public-methods",
is_model_mpttmeta_subclass,
)
# factory_boy's DjangoModelFactory
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_model_factory)
suppress_message(
linter,
ClassChecker.visit_functiondef,
"no-self-argument",
is_factory_post_generation_method,
)
# wsgi.py