Skip to content

Commit 492454d

Browse files
author
Tommy Wu
committed
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 f4f609e commit 492454d

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
@@ -16,6 +16,15 @@ def is_foreignkey_in_class(node):
1616
if not isinstance(node.parent.parent, ClassDef):
1717
return False
1818

19+
# Make sure the outfit class is the subclass of django.db.models.Model
20+
is_in_django_model_class = node_is_subclass(
21+
node.parent.parent,
22+
'django.db.models.base.Model',
23+
'.Model'
24+
)
25+
if not is_in_django_model_class:
26+
return False
27+
1928
if isinstance(node.func, Attribute):
2029
attr = node.func.attrname
2130
elif isinstance(node.func, nodes.Name):

setup.py

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

0 commit comments

Comments
 (0)