Skip to content
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

Use PyLint 3.0+ #672

Merged
merged 3 commits into from
Oct 7, 2024
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
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
Changelog
#########

Version 1.12.0
--------------

**New**:

* pylint version will now be above 3.0.0 again

Version 1.11.0
--------------

Expand Down
182 changes: 28 additions & 154 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion prospector/profiles/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def _load_content_package(name):
file_names = (
["prospector.yaml", "prospector.yml"]
if len(name_split) == 1
else [f"{name_split[1]}.yaml", f"{name_split[1]}.yaml"]
else [f"{name_split[1]}.yaml", f"{name_split[1]}.yml"]
)

data = None
Expand Down
4 changes: 2 additions & 2 deletions prospector/tools/pylint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _prospector_configure(self, prospector_config, linter: ProspectorLinter):
continue
for option in checker.options:
if option[0] in options:
checker.set_option(option[0], options[option[0]])
checker._arguments_manager.set_option(option[0], options[option[0]])

# The warnings about disabling warnings are useful for figuring out
# with other tools to suppress messages from. For example, an unused
Expand Down Expand Up @@ -167,7 +167,7 @@ def _get_pylint_check_paths(self, found_files: FileFinder) -> List[Path]:
def _get_pylint_configuration(
self, check_paths: List[Path], linter: ProspectorLinter, prospector_config, pylint_options
):
self._args = linter.load_command_line_configuration(str(path) for path in check_paths)
self._args = check_paths
linter.load_default_plugins()

config_messages = self._prospector_configure(prospector_config, linter)
Expand Down
48 changes: 43 additions & 5 deletions prospector/tools/pylint/linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

from packaging import version as packaging_version
from pylint import version as pylint_version
from pylint.config.config_file_parser import _ConfigurationFileParser
from pylint.config.config_initialization import _order_all_first
from pylint.lint import PyLinter
from pylint.utils import _splitstrip
from pylint.utils import _splitstrip, utils


class ProspectorLinter(PyLinter):
Expand All @@ -12,13 +14,49 @@ def __init__(self, found_files, *args, **kwargs):
# set up the standard PyLint linter
PyLinter.__init__(self, *args, **kwargs)

# Largely inspired by https://github.com/pylint-dev/pylint/blob/main/pylint/config/config_initialization.py#L26
def config_from_file(self, config_file=None):
"""Will return `True` if plugins have been loaded. For pylint>=1.5. Else `False`."""
self.read_config_file(config_file)
if self.cfgfile_parser.has_option("MASTER", "load-plugins"):
plugins = _splitstrip(self.cfgfile_parser.get("MASTER", "load-plugins"))
config_file_parser = _ConfigurationFileParser(False, self)
config_data, config_args = config_file_parser.parse_config_file(file_path=config_file)
if config_data.get("MASTER", {}).get("load-plugins"):
plugins = _splitstrip(config_data["MASTER"]["load-plugins"])
self.load_plugin_modules(plugins)
self.load_config_file()

config_args = _order_all_first(config_args, joined=False)

if "init-hook" in config_data:
exec(utils._unquote(config_data["init-hook"])) # pylint: disable=exec-used

# Load plugins if specified in the config file
if "load-plugins" in config_data:
self.load_plugin_modules(utils._splitstrip(config_data["load-plugins"]))

self._parse_configuration_file(config_args)

# Set the current module to the command line
# to allow raising messages on it
self.set_current_module(config_file)

self._emit_stashed_messages()

# Set the current module to configuration as we don't know where
# the --load-plugins key is coming from
self.set_current_module("Command line or configuration file")

# We have loaded configuration from config file and command line. Now, we can
# load plugin specific configuration.
self.load_plugin_configuration()

# Now that plugins are loaded, get list of all fail_on messages, and
# enable them
self.enable_fail_on_messages()

self._parse_error_mode()

# Link the base Namespace object on the current directory
self._directory_namespaces[Path().resolve()] = (self.config, {})

return True

def _expand_files(self, modules):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ prospector = 'prospector.run:main'

[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
pylint = ">=2.8.3,<3.0"
pylint = ">=3.0"
pylint-celery = "0.3"
pylint-django = "~2.5"
pylint-plugin-utils = "~0.7"
Expand Down
6 changes: 4 additions & 2 deletions tests/tools/pylint/test_pylint_tool.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from pathlib import Path
from pathlib import Path, PosixPath
from typing import Tuple
from unittest import TestCase
from unittest.mock import patch
Expand Down Expand Up @@ -75,7 +75,7 @@ def test_absolute_path_is_computed_correctly(self):
found_files = _get_test_files("testpath/testfile.py")
pylint_tool.configure(config, found_files)
self.assertNotEqual(pylint_tool._args, [os.path.join(*root_sep_split)])
self.assertEqual(pylint_tool._args, [os.path.join(*root_os_split)])
self.assertEqual(pylint_tool._args, [PosixPath(os.path.join(*root_os_split))])

def test_wont_throw_false_positive_relative_beyond_top_level(self):
with patch("os.getcwd", return_value=os.path.realpath("tests/tools/pylint/testpath/")):
Expand All @@ -91,6 +91,8 @@ def test_will_throw_useless_suppression(self):

found_files = _get_test_files("testpath", "testpath/test_useless_suppression.py")
pylint_tool.configure(config, found_files)
# useless-suppression is now disable by default in pylint
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pylint_tool._linter.enable("useless-suppression")
messages = pylint_tool.run(found_files)
assert any(
m.code == "useless-suppression" for m in messages
Expand Down
Loading