Skip to content

Commit 2068335

Browse files
Tommy Wucarlio
Tommy Wu
authored andcommitted
Make the ForeiginKey detection more accurate
To handle this case: the project use tastypie and django. tastypie has a `ForeignKey` field which has the same name as django's `ForeignKey`. The issue is the lint trys resolving the `ForeignKey` for the tastypie `ForeignKey` which cause import error. In this commit, add a check to ensure the current class of the `ForeignKey` is a subclass of `Model` of django. Tested manually Test case added: func_noerror_foreign_key_in_non_django_class
1 parent 8bab55e commit 2068335

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
Checks that Pylint doesn't raise an error when a 'ForeignKey' appears in a
3+
non-django class
4+
5+
The real case is described as follow:
6+
The project use tastypie and django.
7+
tastypie has a `ForeignKey` field which has the same name
8+
as django's `ForeignKey`.
9+
The issue is the lint trys resolving the `ForeignKey` for the
10+
tastypie `ForeignKey` which cause import error.
11+
"""
12+
# pylint: disable=missing-docstring
13+
from tastypie.resources import ModelResource
14+
from tastypie import fields
15+
from tastypie.fields import ForeignKey
16+
17+
18+
class MyTestResource(ModelResource): # pylint: disable=too-few-public-methods
19+
field1 = ForeignKey('myapp.api.resource', 'xxx')
20+
field2 = fields.ForeignKey('myapp.api.resource', 'xxx')

pylint_django/transforms/foreignkey.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ def is_foreignkey_in_class(node):
1313
if not isinstance(node.parent.parent, ClassDef):
1414
return False
1515

16+
# Make sure the outfit class is the subclass of django.db.models.Model
17+
is_in_django_model_class = node_is_subclass(
18+
node.parent.parent,
19+
'django.db.models.base.Model',
20+
'.Model'
21+
)
22+
if not is_in_django_model_class:
23+
return False
24+
1625
if isinstance(node.func, Attribute):
1726
attr = node.func.attrname
1827
elif isinstance(node.func, nodes.Name):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
],
2424
extras_require={
2525
"with_django": ["Django"],
26-
"for_tests": ["django_tables2", "factory-boy", "coverage", "pytest", "wheel"],
26+
"for_tests": ["django_tables2", "factory-boy", "coverage", "pytest", "wheel", 'django-tastypie'],
2727
},
2828
license="GPLv2",
2929
classifiers=[

0 commit comments

Comments
 (0)