Skip to content

Commit afe6e5e

Browse files
committed
Seems like the detection fqname of classes has changed so that with newer versions of pylint/astroid, the class detected is the "replacement" created in the transforms package
1 parent aa79e85 commit afe6e5e

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

pylint_django/augmentations/__init__.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,10 @@ class ModelB(models.Model):
123123
pass
124124
else:
125125
for cls in inferred:
126-
if (node_is_subclass(
127-
cls, 'django.db.models.manager.Manager') or
128-
node_is_subclass(cls, 'django.db.models.base.Model')):
126+
if (node_is_subclass(cls,
127+
'django.db.models.manager.Manager',
128+
'django.db.models.base.Model',
129+
'.Model')):
129130
# This means that we are looking at a subclass of models.Model
130131
# and something is trying to access a <something>_set attribute.
131132
# Since this could exist, we will return so as not to raise an
@@ -156,25 +157,31 @@ def is_model_media_subclass(node):
156157
parents = ('django.contrib.admin.options.ModelAdmin',
157158
'django.forms.widgets.Media',
158159
'django.db.models.base.Model',
160+
'.Model', # for the transformed version used in this plugin
159161
'django.forms.forms.Form',
160-
'django.forms.models.ModelForm')
161-
return any([node_is_subclass(node.parent, parent) for parent in parents])
162+
'.Form',
163+
'django.forms.models.ModelForm',
164+
'.ModelForm')
165+
return node_is_subclass(node.parent, *parents)
162166

163167

164168
def is_model_meta_subclass(node):
165169
"""Checks that node is derivative of Meta class."""
166170
if node.name != 'Meta' or not isinstance(node.parent, Class):
167171
return False
168172

169-
parents = ('django.db.models.base.Model',
173+
parents = ('.Model', # for the transformed version used here
174+
'django.db.models.base.Model',
175+
'.Form',
170176
'django.forms.forms.Form',
177+
'.ModelForm',
171178
'django.forms.models.ModelForm',
172179
'rest_framework.serializers.ModelSerializer',
173180
'rest_framework.generics.GenericAPIView',
174181
'rest_framework.viewsets.ReadOnlyModelViewSet',
175182
'rest_framework.viewsets.ModelViewSet',
176183
'django_filters.filterset.FilterSet',)
177-
return any([node_is_subclass(node.parent, parent) for parent in parents])
184+
return node_is_subclass(node.parent, *parents)
178185

179186

180187
def is_model_mpttmeta_subclass(node):
@@ -183,9 +190,12 @@ def is_model_mpttmeta_subclass(node):
183190
return False
184191

185192
parents = ('django.db.models.base.Model',
193+
'.Model', # for the transformed version used in this plugin
186194
'django.forms.forms.Form',
187-
'django.forms.models.ModelForm')
188-
return any([node_is_subclass(node.parent, parent) for parent in parents])
195+
'.Form',
196+
'django.forms.models.ModelForm',
197+
'.ModelForm')
198+
return node_is_subclass(node.parent, *parents)
189199

190200

191201
def is_model_test_case_subclass(node):
@@ -233,7 +243,7 @@ def is_model_field_display_method(node):
233243
# blindly accepting get_*_display
234244
try:
235245
for cls in node.last_child().infered():
236-
if node_is_subclass(cls, 'django.db.models.base.Model'):
246+
if node_is_subclass(cls, 'django.db.models.base.Model', '.Model'):
237247
return True
238248
except InferenceError:
239249
return False
@@ -354,8 +364,8 @@ def apply_augmentations(linter):
354364
# For now, just suppress it on models and views
355365
suppress_message(linter, _leave_class(MisdesignChecker), 'too-few-public-methods',
356366
is_class('.Model'))
357-
# TODO: why does this not work with the fqn of 'View'? Must be something to do with the overriding and transforms
358-
suppress_message(linter, _leave_class(MisdesignChecker), 'too-few-public-methods', is_class('.View'))
367+
suppress_message(linter, _leave_class(MisdesignChecker), 'too-few-public-methods',
368+
is_class('.View'))
359369

360370
# Admin
361371
# Too many public methods (40+/20)

pylint_django/checkers/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def visit_classdef(self, node):
6666

6767
def _visit_classdef(self, node):
6868
"""Class visitor."""
69-
if not node_is_subclass(node, 'django.db.models.base.Model'):
69+
if not node_is_subclass(node, 'django.db.models.base.Model', '.Model'):
7070
# we only care about models
7171
return
7272

pylint_django/utils.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,31 @@
55
import sys
66

77
try:
8-
from astroid.util import YES as Unreferable
8+
from astroid.bases import YES as Uninferable
99
except ImportError:
10-
from astroid.util import Unreferable
10+
try:
11+
from astroid.util import YES as Uninferable
12+
except ImportError:
13+
from astroid.util import Uninferable
1114

1215

1316
PY3 = sys.version_info >= (3, 0)
1417

1518

16-
def node_is_subclass(cls, subclass_name):
19+
def node_is_subclass(cls, *subclass_names):
1720
"""Checks if cls node has parent with subclass_name."""
1821
if not isinstance(cls, (Class, Instance)):
1922
return False
2023

21-
if cls.bases == Unreferable:
24+
if cls.bases == Uninferable:
2225
return False
2326
for base_cls in cls.bases:
2427
try:
2528
for inf in base_cls.infered():
26-
if inf.qname() == subclass_name:
29+
if inf.qname() in subclass_names:
2730
return True
28-
if inf != cls and node_is_subclass(inf, subclass_name):
29-
# check up the hierachy in case we are a subclass of
31+
if inf != cls and node_is_subclass(inf, *subclass_names):
32+
# check up the hierarchy in case we are a subclass of
3033
# a subclass of a subclass ...
3134
return True
3235
except InferenceError:

test/test_func.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
linter.load_plugin_modules(['pylint_django'])
15-
linter.global_set_option('required-attributes', ()) # remove required __revision__
15+
1616

1717
def module_exists(module_name):
1818
try:
@@ -22,6 +22,7 @@ def module_exists(module_name):
2222
else:
2323
return True
2424

25+
2526
def tests(input_dir, messages_dir):
2627
callbacks = [cb_test_gen(LintTestUsingFile)]
2728

0 commit comments

Comments
 (0)