From 0e2059d99cd6498b839817f6db7a0c81248b3a07 Mon Sep 17 00:00:00 2001 From: Matej Spiller Muys Date: Fri, 6 Oct 2023 10:50:41 +0200 Subject: [PATCH] Support for pylint 3.x --- pylint_django/checkers/auth_user.py | 10 ++++------ pylint_django/checkers/django_installed.py | 2 +- pylint_django/checkers/foreign_key_strings.py | 14 ++++++++------ pylint_django/checkers/forms.py | 5 +---- pylint_django/checkers/json_response.py | 8 +++----- pylint_django/checkers/migrations.py | 6 +++--- pylint_django/checkers/models.py | 5 +---- pylint_django/compat.py | 5 +++++ pylint_django/plugin.py | 6 +----- pyproject.toml | 2 +- 10 files changed, 28 insertions(+), 35 deletions(-) diff --git a/pylint_django/checkers/auth_user.py b/pylint_django/checkers/auth_user.py index af6a6600..a54d5c35 100644 --- a/pylint_django/checkers/auth_user.py +++ b/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 = { @@ -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: diff --git a/pylint_django/checkers/django_installed.py b/pylint_django/checkers/django_installed.py index 5a0becfd..0caf275b 100644 --- a/pylint_django/checkers/django_installed.py +++ b/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): diff --git a/pylint_django/checkers/foreign_key_strings.py b/pylint_django/checkers/foreign_key_strings.py index fac33148..7282b79e 100644 --- a/pylint_django/checkers/foreign_key_strings.py +++ b/pylint_django/checkers/foreign_key_strings.py @@ -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 @@ -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 = ( @@ -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... @@ -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... diff --git a/pylint_django/checkers/forms.py b/pylint_django/checkers/forms.py index 8614b739..f668684c 100644 --- a/pylint_django/checkers/forms.py +++ b/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 @@ -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": ( diff --git a/pylint_django/checkers/json_response.py b/pylint_django/checkers/json_response.py index 392e6092..4d15f547 100644 --- a/pylint_django/checkers/json_response.py +++ b/pylint_django/checkers/json_response.py @@ -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): @@ -19,8 +19,6 @@ class JsonResponseChecker(checkers.BaseChecker): JSON data! """ - __implements__ = (interfaces.IAstroidChecker,) - # configuration section name name = "json-response-checker" msgs = { @@ -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", diff --git a/pylint_django/checkers/migrations.py b/pylint_django/checkers/migrations.py index c8b9d075..2b3bad71 100644 --- a/pylint_django/checkers/migrations.py +++ b/pylint_django/checkers/migrations.py @@ -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 @@ -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 @@ -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 diff --git a/pylint_django/checkers/models.py b/pylint_django/checkers/models.py index 032b6f48..5bc25c67 100644 --- a/pylint_django/checkers/models.py +++ b/pylint_django/checkers/models.py @@ -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 = { @@ -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 diff --git a/pylint_django/compat.py b/pylint_django/compat.py index fa06cc1d..9b496bc4 100644 --- a/pylint_django/compat.py +++ b/pylint_django/compat.py @@ -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. diff --git a/pylint_django/plugin.py b/pylint_django/plugin.py index c7cc1313..1db620a5 100644 --- a/pylint_django/plugin.py +++ b/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 @@ -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", diff --git a/pyproject.toml b/pyproject.toml index 95f29234..da939054 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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]