Skip to content

Commit

Permalink
Support for pylint 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Matej Spiller Muys authored and carlio committed Oct 22, 2023
1 parent 86bf375 commit 3b760ed
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 35 deletions.
10 changes: 4 additions & 6 deletions pylint_django/checkers/auth_user.py
@@ -1,12 +1,10 @@
from pylint import checkers, interfaces
from pylint.checkers import utils
from pylint import checkers

from pylint_django.__pkginfo__ import BASE_ID
from pylint_django.compat import check_messages


class AuthUserChecker(checkers.BaseChecker):
__implements__ = (interfaces.IAstroidChecker,)

name = "auth-user-checker"

msgs = {
Expand All @@ -22,14 +20,14 @@ class AuthUserChecker(checkers.BaseChecker):
),
}

@utils.check_messages("hard-coded-auth-user")
@check_messages("hard-coded-auth-user")
def visit_const(self, node):
# for now we don't check if the parent is a ForeignKey field
# because the user model should not be hard-coded anywhere
if node.value == "auth.User":
self.add_message("hard-coded-auth-user", node=node)

@utils.check_messages("imported-auth-user")
@check_messages("imported-auth-user")
def visit_importfrom(self, node):
if node.modname == "django.contrib.auth.models":
for imported_names in node.names:
Expand Down
2 changes: 1 addition & 1 deletion pylint_django/checkers/django_installed.py
@@ -1,9 +1,9 @@
from __future__ import absolute_import

from pylint.checkers import BaseChecker
from pylint.checkers.utils import check_messages

from pylint_django.__pkginfo__ import BASE_ID
from pylint_django.compat import check_messages


class DjangoInstalledChecker(BaseChecker):
Expand Down
14 changes: 8 additions & 6 deletions pylint_django/checkers/foreign_key_strings.py
Expand Up @@ -2,10 +2,9 @@

import astroid
from pylint.checkers import BaseChecker
from pylint.checkers.utils import check_messages
from pylint.interfaces import IAstroidChecker

from pylint_django.__pkginfo__ import BASE_ID
from pylint_django.compat import check_messages
from pylint_django.transforms import foreignkey


Expand All @@ -27,8 +26,6 @@ class ForeignKeyStringsChecker(BaseChecker):
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 = (
Expand Down Expand Up @@ -95,7 +92,12 @@ def open(self):
# 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:
if hasattr(self, "linter"):
django_settings_module = self.linter.config.django_settings_module
else:
django_settings_module = self.config.django_settings_module

if 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...
Expand All @@ -108,7 +110,7 @@ def open(self):
try:
from django.conf import Settings, settings # pylint: disable=import-outside-toplevel

settings.configure(Settings(self.config.django_settings_module))
settings.configure(Settings(django_settings_module))
django.setup()
except ImportError:
# we could not find the provided settings module...
Expand Down
5 changes: 1 addition & 4 deletions pylint_django/checkers/forms.py
@@ -1,10 +1,9 @@
"""Models."""
from astroid.nodes import Assign, AssignName, ClassDef
from pylint.checkers import BaseChecker
from pylint.checkers.utils import check_messages
from pylint.interfaces import IAstroidChecker

from pylint_django.__pkginfo__ import BASE_ID
from pylint_django.compat import check_messages
from pylint_django.utils import node_is_subclass


Expand All @@ -18,8 +17,6 @@ def _get_child_meta(node):
class FormChecker(BaseChecker):
"""Django model checker."""

__implements__ = IAstroidChecker

name = "django-form-checker"
msgs = {
f"W{BASE_ID}04": (
Expand Down
8 changes: 3 additions & 5 deletions pylint_django/checkers/json_response.py
Expand Up @@ -7,10 +7,10 @@
"""

import astroid
from pylint import checkers, interfaces
from pylint.checkers import utils
from pylint import checkers

from pylint_django.__pkginfo__ import BASE_ID
from pylint_django.compat import check_messages


class JsonResponseChecker(checkers.BaseChecker):
Expand All @@ -19,8 +19,6 @@ class JsonResponseChecker(checkers.BaseChecker):
JSON data!
"""

__implements__ = (interfaces.IAstroidChecker,)

# configuration section name
name = "json-response-checker"
msgs = {
Expand All @@ -43,7 +41,7 @@ class JsonResponseChecker(checkers.BaseChecker):
),
}

@utils.check_messages(
@check_messages(
"http-response-with-json-dumps",
"http-response-with-content-type-json",
"redundant-content-type-for-json-response",
Expand Down
6 changes: 3 additions & 3 deletions pylint_django/checkers/migrations.py
Expand Up @@ -10,11 +10,11 @@

import astroid
from pylint import checkers, interfaces
from pylint.checkers import utils
from pylint_plugin_utils import suppress_message

from pylint_django import compat
from pylint_django.__pkginfo__ import BASE_ID
from pylint_django.compat import check_messages
from pylint_django.utils import is_migrations_module


Expand Down Expand Up @@ -86,7 +86,7 @@ def visit_call(self, node):
if node not in self._possible_offences[module]:
self._possible_offences[module].append(node)

@utils.check_messages("new-db-field-with-default")
@check_messages("new-db-field-with-default")
def close(self):
def _path(node):
return node.path
Expand Down Expand Up @@ -125,7 +125,7 @@ class MissingBackwardsMigrationChecker(checkers.BaseChecker):
)
}

@utils.check_messages("missing-backwards-migration-callable")
@check_messages("missing-backwards-migration-callable")
def visit_call(self, node):
try:
module = node.frame().parent
Expand Down
5 changes: 1 addition & 4 deletions pylint_django/checkers/models.py
Expand Up @@ -2,10 +2,9 @@
from astroid import Const
from astroid.nodes import Assign, AssignName, ClassDef, FunctionDef
from pylint.checkers import BaseChecker
from pylint.checkers.utils import check_messages
from pylint.interfaces import IAstroidChecker

from pylint_django.__pkginfo__ import BASE_ID
from pylint_django.compat import check_messages
from pylint_django.utils import PY3, node_is_subclass

MESSAGES = {
Expand Down Expand Up @@ -75,8 +74,6 @@ def _is_unicode_or_str_in_python_2_compatibility(method):
class ModelChecker(BaseChecker):
"""Django model checker."""

__implements__ = IAstroidChecker

name = "django-model-checker"
msgs = MESSAGES

Expand Down
5 changes: 5 additions & 0 deletions pylint_django/compat.py
Expand Up @@ -20,6 +20,11 @@
except ImportError:
from astroid.util import Uninferable

try:
from pylint.checkers.utils import check_messages
except (ImportError, ModuleNotFoundError):
from pylint.checkers.utils import only_required_for_messages as check_messages

import pylint

# pylint before version 2.3 does not support load_configuration() hook.
Expand Down
6 changes: 1 addition & 5 deletions pylint_django/plugin.py
@@ -1,7 +1,4 @@
"""Common Django module."""
from pylint.checkers.base import NameChecker
from pylint_plugin_utils import get_checker

# we want to import the transforms to make sure they get added to the astroid manager,
# however we don't actually access them directly, so we'll disable the warning
from pylint_django import transforms # noqa, pylint: disable=unused-import
Expand All @@ -13,8 +10,7 @@ def load_configuration(linter):
"""
Amend existing checker config.
"""
name_checker = get_checker(linter, NameChecker)
name_checker.config.good_names += (
linter.config.good_names += (
"pk",
"qs",
"urlpatterns",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Expand Up @@ -42,7 +42,7 @@ exclude = ["**/tests/**", "**/testutils.py", "**/tests.py"]
[tool.poetry.dependencies]
python = ">=3.7,<4.0"
pylint-plugin-utils = ">=0.8"
pylint = ">=2.0,<3"
pylint = ">=2.0,<4"
Django = {version=">=2.2", optional = true}

[tool.poetry.group.dev.dependencies]
Expand Down

0 comments on commit 3b760ed

Please sign in to comment.