-
Notifications
You must be signed in to change notification settings - Fork 118
/
foreign_key_strings.py
117 lines (97 loc) · 5.55 KB
/
foreign_key_strings.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
from __future__ import absolute_import
from pylint.checkers import BaseChecker
from pylint.interfaces import IAstroidChecker
from pylint.checkers.utils import check_messages
from pylint_django.__pkginfo__ import BASE_ID
from pylint_django.transforms import foreignkey
import astroid
class ForeignKeyStringsChecker(BaseChecker):
"""
Adds transforms to be able to do type inference for model ForeignKeyField
properties which use a string to name the foreign relationship. This uses
Django's model name resolution and this checker wraps the setup to ensure
Django is able to configure itself before attempting to use the lookups.
"""
_LONG_MESSAGE = """Finding foreign-key relationships from strings in pylint-django requires configuring Django.
This can be done via the DJANGO_SETTINGS_MODULE environment variable or the pylint option django-settings-module, eg:
`pylint --load-plugins=pylint_django --django-settings-module=myproject.settings`
. This can also be set as an option in a .pylintrc configuration file.
Some basic default settings were used, however this will lead to less accurate linting.
Consider passing in an explicit Django configuration file to match your project to improve accuracy."""
__implements__ = (IAstroidChecker,)
name = "Django foreign keys referenced by strings"
options = (
(
"django-settings-module",
{
"default": None,
"type": "string",
"metavar": "<django settings module>",
"help": "A module containing Django settings to be used while linting.",
},
),
)
msgs = {"E%s10" % BASE_ID: ("Django was not configured. For more information run pylint --load-plugins=pylint_django --help-msg=django-not-configured", "django-not-configured", _LONG_MESSAGE),
"F%s10" % BASE_ID: (
'Provided Django settings %s could not be loaded',
'django-settings-module-not-found',
'The provided Django settings module %s was not found on the path'
)}
def open(self):
self._raise_warning = False
# This is a bit of a hacky workaround. pylint-django does not *require* that
# Django is configured explicitly, and will use some basic defaults in that
# case. However, as this is a WARNING not a FATAL, the error must be raised
# with an AST node - only F and R messages are scope exempt (see
# https://github.com/PyCQA/pylint/blob/master/pylint/constants.py#L24)
# However, testing to see if Django is configured happens in `open()`
# before any modules are inspected, as Django needs to be configured with
# defaults before the foreignkey checker can work properly. At this point,
# there are no nodes.
# Therefore, during the initialisation in `open()`, if django was configured
# using defaults by pylint-django, it cannot raise the warning yet and
# must wait until some module is inspected to be able to raise... so that
# state is stashed in this property.
from django.core.exceptions import (
ImproperlyConfigured,
) # pylint: disable=import-outside-toplevel
try:
import django # pylint: disable=import-outside-toplevel
django.setup()
from django.apps import apps # pylint: disable=import-outside-toplevel
except ImproperlyConfigured:
# this means that Django wasn't able to configure itself using some defaults
# provided (likely in a DJANGO_SETTINGS_MODULE environment variable)
# so see if the user has specified a pylint option
if self.config.django_settings_module is None:
# we will warn the user that they haven't actually configured Django themselves
self._raise_warning = True
# but use django defaults then...
from django.conf import settings # pylint: disable=import-outside-toplevel
settings.configure()
django.setup()
else:
# see if we can load the provided settings module
try:
from django.conf import settings, Settings # pylint: disable=import-outside-toplevel
settings.configure(Settings(self.config.django_settings_module))
django.setup()
except ImportError:
# we could not find the provided settings module...
# at least here it is a fatal error so we can just raise this immediately
self.add_message('django-settings-module-not-found', args=self.config.django_settings_module)
# however we'll trundle on with basic settings
from django.conf import settings # pylint: disable=import-outside-toplevel
settings.configure()
django.setup()
# now we can add the trasforms speciifc to this checker
foreignkey.add_transform(astroid.MANAGER)
# TODO: this is a bit messy having so many inline imports but in order to avoid
# duplicating the django_installed checker, it'll do for now. In the future, merging
# those two checkers together might make sense.
@check_messages("django-not-configured")
def visit_module(self, node):
if self._raise_warning:
# just add it to the first node we see... which isn't nice but not sure what else to do
self.add_message("django-not-configured", node=node)
self._raise_warning = False # only raise it once...