Skip to content

Support for pylint 3.x #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions pylint_django/checkers/auth_user.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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