summaryrefslogtreecommitdiff
path: root/pylint
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2023-03-08 20:53:50 +0100
committerGitHub <noreply@github.com>2023-03-08 20:53:50 +0100
commit3318aa0c5877abd9e9d2361f8a21b8880b7a052d (patch)
treee3c219f0040b635378391803319f1de9f147847d /pylint
parentac5f0a4176532a459f0c4c0b6289c43f94994489 (diff)
downloadpylint-git-3318aa0c5877abd9e9d2361f8a21b8880b7a052d.tar.gz
Remove all old code related to ``optparse`` config parsing. (#8405)
* Remove all old config parsing code * Temporarily disable a test
Diffstat (limited to 'pylint')
-rw-r--r--pylint/checkers/base_checker.py8
-rw-r--r--pylint/config/__init__.py62
-rw-r--r--pylint/config/_pylint_config/generate_command.py7
-rw-r--r--pylint/config/arguments_manager.py435
-rw-r--r--pylint/config/arguments_provider.py173
-rw-r--r--pylint/config/callback_actions.py7
-rw-r--r--pylint/config/configuration_mixin.py41
-rw-r--r--pylint/config/environment_variable.py11
-rw-r--r--pylint/config/find_default_config_files.py19
-rw-r--r--pylint/config/option.py239
-rw-r--r--pylint/config/option_manager_mixin.py372
-rw-r--r--pylint/config/option_parser.py56
-rw-r--r--pylint/config/options_provider_mixin.py123
-rw-r--r--pylint/config/utils.py23
-rw-r--r--pylint/lint/pylinter.py19
-rw-r--r--pylint/lint/utils.py2
-rw-r--r--pylint/utils/docs.py37
17 files changed, 45 insertions, 1589 deletions
diff --git a/pylint/checkers/base_checker.py b/pylint/checkers/base_checker.py
index fb921db1e..d6172fb42 100644
--- a/pylint/checkers/base_checker.py
+++ b/pylint/checkers/base_checker.py
@@ -96,11 +96,9 @@ class BaseChecker(_ArgumentsProvider):
See: MessageHandlerMixIn.get_full_documentation()
"""
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- return self.get_full_documentation(
- msgs=self.msgs, options=self.options_and_values(), reports=self.reports
- )
+ return self.get_full_documentation(
+ msgs=self.msgs, options=self._options_and_values(), reports=self.reports
+ )
def get_full_documentation(
self,
diff --git a/pylint/config/__init__.py b/pylint/config/__init__.py
index 5f90bbae0..c26df891e 100644
--- a/pylint/config/__init__.py
+++ b/pylint/config/__init__.py
@@ -4,64 +4,6 @@
from __future__ import annotations
-__all__ = [
- "ConfigurationMixIn", # Deprecated
- "find_default_config_files",
- "find_pylintrc", # Deprecated
- "Option", # Deprecated
- "OptionsManagerMixIn", # Deprecated
- "OptionParser", # Deprecated
- "OptionsProviderMixIn", # Deprecated
- "UnsupportedAction", # Deprecated
- "PYLINTRC",
- "USER_HOME", # Compatibility with the old API
- "PYLINT_HOME", # Compatibility with the old API
- "save_results", # Compatibility with the old API # Deprecated
- "load_results", # Compatibility with the old API # Deprecated
-]
+__all__ = ["find_default_config_files"]
-import warnings
-
-from pylint.config.arguments_provider import UnsupportedAction
-from pylint.config.configuration_mixin import ConfigurationMixIn
-from pylint.config.environment_variable import PYLINTRC
-from pylint.config.find_default_config_files import (
- find_default_config_files,
- find_pylintrc,
-)
-from pylint.config.option import Option
-from pylint.config.option_manager_mixin import OptionsManagerMixIn
-from pylint.config.option_parser import OptionParser # type: ignore[attr-defined]
-from pylint.config.options_provider_mixin import ( # type: ignore[attr-defined]
- OptionsProviderMixIn,
-)
-from pylint.constants import PYLINT_HOME, USER_HOME
-from pylint.utils import LinterStats
-
-
-def load_results(base: str) -> LinterStats | None:
- # TODO: 3.0: Remove deprecated function
- # pylint: disable=import-outside-toplevel
- from pylint.lint.caching import load_results as _real_load_results
-
- warnings.warn(
- "'pylint.config.load_results' is deprecated, please use "
- "'pylint.lint.load_results' instead. This will be removed in 3.0.",
- DeprecationWarning,
- stacklevel=2,
- )
- return _real_load_results(base, PYLINT_HOME)
-
-
-def save_results(results: LinterStats, base: str) -> None:
- # TODO: 3.0: Remove deprecated function
- # pylint: disable=import-outside-toplevel
- from pylint.lint.caching import save_results as _real_save_results
-
- warnings.warn(
- "'pylint.config.save_results' is deprecated, please use "
- "'pylint.lint.save_results' instead. This will be removed in 3.0.",
- DeprecationWarning,
- stacklevel=2,
- )
- return _real_save_results(results, base, PYLINT_HOME)
+from pylint.config.find_default_config_files import find_default_config_files
diff --git a/pylint/config/_pylint_config/generate_command.py b/pylint/config/_pylint_config/generate_command.py
index 110069b90..9d0f9d649 100644
--- a/pylint/config/_pylint_config/generate_command.py
+++ b/pylint/config/_pylint_config/generate_command.py
@@ -7,7 +7,6 @@
from __future__ import annotations
-import warnings
from io import StringIO
from typing import TYPE_CHECKING
@@ -29,10 +28,8 @@ def generate_interactive_config(linter: PyLinter) -> None:
config_string = linter._generate_config_file(minimal=minimal)
else:
output_stream = StringIO()
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- linter.generate_config(stream=output_stream, skipsections=("Commands",))
- config_string = output_stream.getvalue()
+ linter._generate_config(stream=output_stream, skipsections=("Commands",))
+ config_string = output_stream.getvalue()
if to_file:
with open(output_file_name, "w", encoding="utf-8") as f:
diff --git a/pylint/config/arguments_manager.py b/pylint/config/arguments_manager.py
index 2151aefde..0c94dbf54 100644
--- a/pylint/config/arguments_manager.py
+++ b/pylint/config/arguments_manager.py
@@ -7,18 +7,12 @@
from __future__ import annotations
import argparse
-import configparser
-import copy
-import optparse # pylint: disable=deprecated-module
-import os
import re
import sys
import textwrap
import warnings
-from collections import OrderedDict
from collections.abc import Sequence
-from pathlib import Path
-from typing import TYPE_CHECKING, Any, TextIO, Union
+from typing import TYPE_CHECKING, Any, TextIO
import tomlkit
@@ -37,11 +31,6 @@ from pylint.config.exceptions import (
_UnrecognizedOptionError,
)
from pylint.config.help_formatter import _HelpFormatter
-from pylint.config.option import Option
-from pylint.config.option_parser import OptionParser # type: ignore[attr-defined]
-from pylint.config.options_provider_mixin import ( # type: ignore[attr-defined]
- OptionsProviderMixIn,
-)
from pylint.config.utils import _convert_option_to_argument, _parse_rich_type_value
from pylint.constants import MAIN_CHECKER_NAME
from pylint.typing import DirectoryNamespaceDict, OptionDict
@@ -55,10 +44,7 @@ else:
if TYPE_CHECKING:
from pylint.config.arguments_provider import _ArgumentsProvider
-ConfigProvider = Union["_ArgumentsProvider", OptionsProviderMixIn]
-
-# pylint: disable-next=too-many-instance-attributes
class _ArgumentsManager:
"""Arguments manager class used to handle command-line arguments and options."""
@@ -95,21 +81,6 @@ class _ArgumentsManager:
self._directory_namespaces: DirectoryNamespaceDict = {}
"""Mapping of directories and their respective namespace objects."""
- # TODO: 3.0: Remove deprecated attributes introduced to keep API
- # parity with optparse. Until '_maxlevel'
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- self.reset_parsers(usage or "")
- # list of registered options providers
- self._options_providers: list[ConfigProvider] = []
- # dictionary associating option name to checker
- self._all_options: OrderedDict[str, ConfigProvider] = OrderedDict()
- self._short_options: dict[str, str] = {}
- self._nocallback_options: dict[ConfigProvider, str] = {}
- self._mygroups: dict[str, optparse.OptionGroup] = {}
- # verbosity
- self._maxlevel: int = 0
-
@property
def config(self) -> argparse.Namespace:
"""Namespace for all options."""
@@ -119,25 +90,6 @@ class _ArgumentsManager:
def config(self, value: argparse.Namespace) -> None:
self._config = value
- @property
- def options_providers(self) -> list[ConfigProvider]:
- # TODO: 3.0: Remove deprecated attribute.
- warnings.warn(
- "options_providers has been deprecated. It will be removed in pylint 3.0.",
- DeprecationWarning,
- stacklevel=2,
- )
- return self._options_providers
-
- @options_providers.setter
- def options_providers(self, value: list[ConfigProvider]) -> None:
- warnings.warn(
- "Setting options_providers has been deprecated. It will be removed in pylint 3.0.",
- DeprecationWarning,
- stacklevel=2,
- )
- self._options_providers = value
-
def _register_options_provider(self, provider: _ArgumentsProvider) -> None:
"""Register an options provider and load its defaults."""
for opt, optdict in provider.options:
@@ -281,173 +233,12 @@ class _ArgumentsManager:
return parsed_args
- def reset_parsers(self, usage: str = "") -> None: # pragma: no cover
- """DEPRECATED."""
- warnings.warn(
- "reset_parsers has been deprecated. Parsers should be instantiated "
- "once during initialization and do not need to be reset.",
- DeprecationWarning,
- stacklevel=2,
- )
- # configuration file parser
- self.cfgfile_parser = configparser.ConfigParser(
- inline_comment_prefixes=("#", ";")
- )
- # command line parser
- self.cmdline_parser = OptionParser(Option, usage=usage)
- self.cmdline_parser.options_manager = self
- self._optik_option_attrs = set(self.cmdline_parser.option_class.ATTRS)
-
- def register_options_provider(
- self, provider: ConfigProvider, own_group: bool = True
- ) -> None: # pragma: no cover
- """DEPRECATED: Register an options provider."""
- warnings.warn(
- "register_options_provider has been deprecated. Options providers and "
- "arguments providers should be registered by initializing ArgumentsProvider. "
- "This automatically registers the provider on the ArgumentsManager.",
- DeprecationWarning,
- stacklevel=2,
- )
- self.options_providers.append(provider)
- non_group_spec_options = [
- option for option in provider.options if "group" not in option[1]
- ]
- groups = getattr(provider, "option_groups", ())
- if own_group and non_group_spec_options:
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- self.add_option_group(
- provider.name.upper(),
- provider.__doc__,
- non_group_spec_options,
- provider,
- )
- else:
- for opt, optdict in non_group_spec_options:
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- self.add_optik_option(provider, self.cmdline_parser, opt, optdict)
- for gname, gdoc in groups:
- gname = gname.upper()
- goptions = [
- option
- for option in provider.options
- if option[1].get("group", "").upper() == gname # type: ignore[union-attr]
- ]
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- self.add_option_group(gname, gdoc, goptions, provider)
-
- def add_option_group(
- self,
- group_name: str,
- _: str | None,
- options: list[tuple[str, OptionDict]],
- provider: ConfigProvider,
- ) -> None: # pragma: no cover
- """DEPRECATED."""
- warnings.warn(
- "add_option_group has been deprecated. Option groups should be "
- "registered by initializing ArgumentsProvider. "
- "This automatically registers the group on the ArgumentsManager.",
- DeprecationWarning,
- stacklevel=2,
- )
- # add option group to the command line parser
- if group_name in self._mygroups:
- group = self._mygroups[group_name]
- else:
- group = optparse.OptionGroup(
- self.cmdline_parser, title=group_name.capitalize()
- )
- self.cmdline_parser.add_option_group(group)
- self._mygroups[group_name] = group
- # add section to the config file
- if (
- group_name != "DEFAULT"
- and group_name not in self.cfgfile_parser._sections # type: ignore[attr-defined]
- ):
- self.cfgfile_parser.add_section(group_name)
- # add provider's specific options
- for opt, optdict in options:
- if not isinstance(optdict.get("action", "store"), str):
- optdict["action"] = "callback"
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- self.add_optik_option(provider, group, opt, optdict)
-
- def add_optik_option(
- self,
- provider: ConfigProvider,
- optikcontainer: optparse.OptionParser | optparse.OptionGroup,
- opt: str,
- optdict: OptionDict,
- ) -> None: # pragma: no cover
- """DEPRECATED."""
- warnings.warn(
- "add_optik_option has been deprecated. Options should be automatically "
- "added by initializing an ArgumentsProvider.",
- DeprecationWarning,
- stacklevel=2,
- )
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- args, optdict = self.optik_option(provider, opt, optdict)
- option = optikcontainer.add_option(*args, **optdict)
- self._all_options[opt] = provider
- self._maxlevel = max(self._maxlevel, option.level or 0)
-
- def optik_option(
- self, provider: ConfigProvider, opt: str, optdict: OptionDict
- ) -> tuple[list[str], OptionDict]: # pragma: no cover
- """DEPRECATED: Get our personal option definition and return a suitable form for
- use with optik/optparse.
- """
- warnings.warn(
- "optik_option has been deprecated. Parsing of option dictionaries should be done "
- "automatically by initializing an ArgumentsProvider.",
- DeprecationWarning,
- stacklevel=2,
- )
- optdict = copy.copy(optdict)
- if "action" in optdict:
- self._nocallback_options[provider] = opt
- else:
- optdict["action"] = "callback"
- optdict["callback"] = self.cb_set_provider_option
- # default is handled here and *must not* be given to optik if you
- # want the whole machinery to work
- if "default" in optdict:
- if (
- "help" in optdict
- and optdict.get("default") is not None
- and optdict["action"] not in ("store_true", "store_false")
- ):
- optdict["help"] += " [current: %default]" # type: ignore[operator]
- del optdict["default"]
- args = ["--" + str(opt)]
- if "short" in optdict:
- self._short_options[optdict["short"]] = opt # type: ignore[index]
- args.append("-" + optdict["short"]) # type: ignore[operator]
- del optdict["short"]
- # cleanup option definition dict before giving it to optik
- for key in list(optdict.keys()):
- if key not in self._optik_option_attrs:
- optdict.pop(key)
- return args, optdict
-
- def generate_config(
+ def _generate_config(
self, stream: TextIO | None = None, skipsections: tuple[str, ...] = ()
- ) -> None: # pragma: no cover
- """DEPRECATED: Write a configuration file according to the current configuration
+ ) -> None:
+ """Write a configuration file according to the current configuration
into the given stream or stdout.
"""
- warnings.warn(
- "generate_config has been deprecated. It will be removed in pylint 3.0.",
- DeprecationWarning,
- stacklevel=2,
- )
options_by_section = {}
sections = []
for group in sorted(
@@ -503,202 +294,10 @@ class _ArgumentsManager:
)
printed = True
- def load_provider_defaults(self) -> None: # pragma: no cover
- """DEPRECATED: Initialize configuration using default values."""
- warnings.warn(
- "load_provider_defaults has been deprecated. Parsing of option defaults should be done "
- "automatically by initializing an ArgumentsProvider.",
- DeprecationWarning,
- stacklevel=2,
- )
- for provider in self.options_providers:
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- provider.load_defaults()
-
- def read_config_file(
- self, config_file: Path | None = None, verbose: bool = False
- ) -> None: # pragma: no cover
- """DEPRECATED: Read the configuration file but do not load it (i.e. dispatching
- values to each option's provider).
-
- :raises OSError: When the specified config file doesn't exist
- """
- warnings.warn(
- "read_config_file has been deprecated. It will be removed in pylint 3.0.",
- DeprecationWarning,
- stacklevel=2,
- )
- if not config_file:
- if verbose:
- print(
- "No config file found, using default configuration", file=sys.stderr
- )
- return
- config_file = Path(os.path.expandvars(config_file)).expanduser()
- if not config_file.exists():
- raise OSError(f"The config file {str(config_file)} doesn't exist!")
- parser = self.cfgfile_parser
- if config_file.suffix == ".toml":
- try:
- self._parse_toml(config_file, parser)
- except tomllib.TOMLDecodeError:
- pass
- else:
- # Use this encoding in order to strip the BOM marker, if any.
- with open(config_file, encoding="utf_8_sig") as fp:
- parser.read_file(fp)
- # normalize each section's title
- for sect, values in list(parser._sections.items()): # type: ignore[attr-defined]
- if sect.startswith("pylint."):
- sect = sect[len("pylint.") :]
- if not sect.isupper() and values:
- parser._sections[sect.upper()] = values # type: ignore[attr-defined]
-
- if verbose:
- print(f"Using config file '{config_file}'", file=sys.stderr)
-
- @staticmethod
- def _parse_toml(
- config_file: Path, parser: configparser.ConfigParser
- ) -> None: # pragma: no cover
- """DEPRECATED: Parse and handle errors of a toml configuration file.
-
- TODO: 3.0: Remove deprecated method.
- """
- with open(config_file, mode="rb") as fp:
- content = tomllib.load(fp)
- try:
- sections_values = content["tool"]["pylint"]
- except KeyError:
- return
- for section, values in sections_values.items():
- section_name = section.upper()
- # TOML has rich types, convert values to
- # strings as ConfigParser expects.
- if not isinstance(values, dict):
- continue
- for option, value in values.items():
- if isinstance(value, bool):
- values[option] = "yes" if value else "no"
- elif isinstance(value, list):
- values[option] = ",".join(value)
- else:
- values[option] = str(value)
- for option, value in values.items():
- try:
- parser.set(section_name, option, value=value)
- except configparser.NoSectionError:
- parser.add_section(section_name)
- parser.set(section_name, option, value=value)
-
- def load_config_file(self) -> None: # pragma: no cover
- """DEPRECATED: Dispatch values previously read from a configuration file to each
- option's provider.
- """
- warnings.warn(
- "load_config_file has been deprecated. It will be removed in pylint 3.0.",
- DeprecationWarning,
- stacklevel=2,
- )
- parser = self.cfgfile_parser
- for section in parser.sections():
- for option, value in parser.items(section):
- try:
- self.global_set_option(option, value)
- except (KeyError, optparse.OptionError):
- continue
-
- def load_configuration(self, **kwargs: Any) -> None: # pragma: no cover
- """DEPRECATED: Override configuration according to given parameters."""
- warnings.warn(
- "load_configuration has been deprecated. It will be removed in pylint 3.0.",
- DeprecationWarning,
- stacklevel=2,
- )
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- return self.load_configuration_from_config(kwargs)
-
- def load_configuration_from_config(
- self, config: dict[str, Any]
- ) -> None: # pragma: no cover
- warnings.warn(
- "DEPRECATED: load_configuration_from_config has been deprecated. It will be removed in pylint 3.0.",
- DeprecationWarning,
- stacklevel=2,
- )
- for opt, opt_value in config.items():
- opt = opt.replace("_", "-")
- provider = self._all_options[opt]
- provider.set_option(opt, opt_value)
-
- def load_command_line_configuration(
- self, args: list[str] | None = None
- ) -> list[str]: # pragma: no cover
- """DEPRECATED: Override configuration according to command line parameters.
-
- return additional arguments
- """
- warnings.warn(
- "load_command_line_configuration has been deprecated. It will be removed in pylint 3.0.",
- DeprecationWarning,
- stacklevel=2,
- )
- args = sys.argv[1:] if args is None else list(args)
- (options, args) = self.cmdline_parser.parse_args(args=args)
- for provider in self._nocallback_options:
- config = provider.config
- for attr in config.__dict__.keys():
- value = getattr(options, attr, None)
- if value is None:
- continue
- setattr(config, attr, value)
- return args # type: ignore[return-value]
-
- def help(self, level: int | None = None) -> str:
+ def help(self) -> str:
"""Return the usage string based on the available options."""
- if level is not None:
- warnings.warn(
- "Supplying a 'level' argument to help() has been deprecated."
- "You can call help() without any arguments.",
- DeprecationWarning,
- stacklevel=2,
- )
return self._arg_parser.format_help()
- def cb_set_provider_option( # pragma: no cover
- self, option: Any, opt: Any, value: Any, parser: Any
- ) -> None:
- """DEPRECATED: Optik callback for option setting."""
- # TODO: 3.0: Remove deprecated method.
- warnings.warn(
- "cb_set_provider_option has been deprecated. It will be removed in pylint 3.0.",
- DeprecationWarning,
- stacklevel=2,
- )
- if opt.startswith("--"):
- # remove -- on long option
- opt = opt[2:]
- else:
- # short option, get its long equivalent
- opt = self._short_options[opt[1:]]
- # trick since we can't set action='store_true' on options
- if value is None:
- value = 1
- self.set_option(opt, value)
-
- def global_set_option(self, opt: str, value: Any) -> None: # pragma: no cover
- """DEPRECATED: Set option on the correct option provider."""
- # TODO: 3.0: Remove deprecated method.
- warnings.warn(
- "global_set_option has been deprecated. You can use _arguments_manager.set_option "
- "or linter.set_option to set options on the global configuration object.",
- DeprecationWarning,
- stacklevel=2,
- )
- self.set_option(opt, value)
-
def _generate_config_file(self, *, minimal: bool = False) -> str:
"""Write a configuration file according to the current configuration into
stdout.
@@ -792,30 +391,8 @@ class _ArgumentsManager:
return str(toml_string)
- def set_option(
- self,
- optname: str,
- value: Any,
- action: str | None = "default_value",
- optdict: None | str | OptionDict = "default_value",
- ) -> None:
+ def set_option(self, optname: str, value: Any) -> None:
"""Set an option on the namespace object."""
- # TODO: 3.0: Remove deprecated arguments.
- if action != "default_value":
- warnings.warn(
- "The 'action' argument has been deprecated. You can use set_option "
- "without the 'action' or 'optdict' arguments.",
- DeprecationWarning,
- stacklevel=2,
- )
- if optdict != "default_value":
- warnings.warn(
- "The 'optdict' argument has been deprecated. You can use set_option "
- "without the 'action' or 'optdict' arguments.",
- DeprecationWarning,
- stacklevel=2,
- )
-
self.config = self._arg_parser.parse_known_args(
[f"--{optname.replace('_', '-')}", _parse_rich_type_value(value)],
self.config,
diff --git a/pylint/config/arguments_provider.py b/pylint/config/arguments_provider.py
index f5aac2f1d..170c9a156 100644
--- a/pylint/config/arguments_provider.py
+++ b/pylint/config/arguments_provider.py
@@ -6,9 +6,6 @@
from __future__ import annotations
-import argparse
-import optparse # pylint: disable=deprecated-module
-import warnings
from collections.abc import Iterator
from typing import Any
@@ -16,19 +13,6 @@ from pylint.config.arguments_manager import _ArgumentsManager
from pylint.typing import OptionDict, Options
-class UnsupportedAction(Exception):
- """Raised by set_option when it doesn't know what to do for an action."""
-
- def __init__(self, *args: object) -> None:
- # TODO: 3.0: Remove deprecated exception
- warnings.warn(
- "UnsupportedAction has been deprecated and will be removed in pylint 3.0",
- DeprecationWarning,
- stacklevel=2,
- )
- super().__init__(*args)
-
-
class _ArgumentsProvider:
"""Base class for classes that provide arguments."""
@@ -47,170 +31,35 @@ class _ArgumentsProvider:
self._arguments_manager._register_options_provider(self)
- self._level = 0
-
- @property
- def level(self) -> int:
- # TODO: 3.0: Remove deprecated attribute
- warnings.warn(
- "The level attribute has been deprecated. It was used to display the checker in the help or not,"
- " and everything is displayed in the help now. It will be removed in pylint 3.0.",
- DeprecationWarning,
- stacklevel=2,
- )
- return self._level
-
- @level.setter
- def level(self, value: int) -> None:
- # TODO: 3.0: Remove deprecated attribute
- warnings.warn(
- "Setting the level attribute has been deprecated. It was used to display the checker "
- "in the help or not, and everything is displayed in the help now. It will be removed "
- "in pylint 3.0.",
- DeprecationWarning,
- stacklevel=2,
- )
- self._level = value
-
- @property
- def config(self) -> argparse.Namespace:
- # TODO: 3.0: Remove deprecated attribute
- warnings.warn(
- "The checker-specific config attribute has been deprecated. Please use "
- "'linter.config' to access the global configuration object.",
- DeprecationWarning,
- stacklevel=2,
- )
- return self._arguments_manager.config
-
- def load_defaults(self) -> None: # pragma: no cover
- """DEPRECATED: Initialize the provider using default values."""
- warnings.warn(
- "load_defaults has been deprecated. Option groups should be "
- "registered by initializing an ArgumentsProvider. "
- "This automatically registers the group on the ArgumentsManager.",
- DeprecationWarning,
- stacklevel=2,
- )
- for opt, optdict in self.options:
- action = optdict.get("action")
- if action != "callback":
- # callback action have no default
- if optdict is None:
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- optdict = self.get_option_def(opt)
- default = optdict.get("default")
- self.set_option(opt, default, action, optdict)
-
- def option_attrname(
- self, opt: str, optdict: OptionDict | None = None
- ) -> str: # pragma: no cover
- """DEPRECATED: Get the config attribute corresponding to opt."""
- warnings.warn(
- "option_attrname has been deprecated. It will be removed "
- "in a future release.",
- DeprecationWarning,
- stacklevel=2,
- )
- if optdict is None:
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- optdict = self.get_option_def(opt)
- return optdict.get("dest", opt.replace("-", "_")) # type: ignore[return-value]
-
- def option_value(self, opt: str) -> Any: # pragma: no cover
- """DEPRECATED: Get the current value for the given option."""
- warnings.warn(
- "option_value has been deprecated. It will be removed "
- "in a future release.",
- DeprecationWarning,
- stacklevel=2,
- )
+ def _option_value(self, opt: str) -> Any:
+ """Get the current value for the given option."""
return getattr(self._arguments_manager.config, opt.replace("-", "_"), None)
- def set_option( # pragma: no cover
- self,
- optname: Any,
- value: Any,
- action: Any = None, # pylint: disable=unused-argument
- optdict: Any = None, # pylint: disable=unused-argument
- ) -> None:
- """DEPRECATED: Method called to set an option (registered in the options
- list).
- """
- # TODO: 3.0: Remove deprecated method.
- warnings.warn(
- "set_option has been deprecated. You can use _arguments_manager.set_option "
- "or linter.set_option to set options on the global configuration object.",
- DeprecationWarning,
- stacklevel=2,
- )
- self._arguments_manager.set_option(optname, value)
-
- def get_option_def(self, opt: str) -> OptionDict: # pragma: no cover
- """DEPRECATED: Return the dictionary defining an option given its name.
-
- :raises OptionError: If the option isn't found.
- """
- warnings.warn(
- "get_option_def has been deprecated. It will be removed "
- "in a future release.",
- DeprecationWarning,
- stacklevel=2,
- )
- assert self.options
- for option in self.options:
- if option[0] == opt:
- return option[1]
- raise optparse.OptionError(
- f"no such option {opt} in section {self.name!r}", opt # type: ignore[arg-type]
- )
-
- def options_by_section(
+ def _options_by_section(
self,
) -> Iterator[
tuple[str, list[tuple[str, OptionDict, Any]]]
| tuple[None, dict[str, list[tuple[str, OptionDict, Any]]]]
- ]: # pragma: no cover
- """DEPRECATED: Return an iterator on options grouped by section.
+ ]:
+ """Return an iterator on options grouped by section.
(section, [list of (optname, optdict, optvalue)])
"""
- # TODO 3.0: Make this function private see
- # https://github.com/PyCQA/pylint/pull/6665#discussion_r880143229
- # It's only used in '_get_global_options_documentation'
- warnings.warn(
- "options_by_section has been deprecated. It will be removed "
- "in a future release.",
- DeprecationWarning,
- stacklevel=2,
- )
sections: dict[str, list[tuple[str, OptionDict, Any]]] = {}
for optname, optdict in self.options:
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- sections.setdefault(optdict.get("group"), []).append( # type: ignore[arg-type]
- (optname, optdict, self.option_value(optname))
- )
+ sections.setdefault(optdict.get("group"), []).append( # type: ignore[arg-type]
+ (optname, optdict, self._option_value(optname))
+ )
if None in sections:
yield None, sections.pop(None) # type: ignore[call-overload]
for section, options in sorted(sections.items()):
yield section.upper(), options
- def options_and_values(
+ def _options_and_values(
self, options: Options | None = None
- ) -> Iterator[tuple[str, OptionDict, Any]]: # pragma: no cover
+ ) -> Iterator[tuple[str, OptionDict, Any]]:
"""DEPRECATED."""
- warnings.warn(
- "options_and_values has been deprecated. It will be removed "
- "in a future release.",
- DeprecationWarning,
- stacklevel=2,
- )
if options is None:
options = self.options
for optname, optdict in options:
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- yield optname, optdict, self.option_value(optname)
+ yield optname, optdict, self._option_value(optname)
diff --git a/pylint/config/callback_actions.py b/pylint/config/callback_actions.py
index a4c633464..91485e095 100644
--- a/pylint/config/callback_actions.py
+++ b/pylint/config/callback_actions.py
@@ -11,7 +11,6 @@ from __future__ import annotations
import abc
import argparse
import sys
-import warnings
from collections.abc import Callable, Sequence
from pathlib import Path
from typing import TYPE_CHECKING, Any
@@ -265,11 +264,9 @@ class _GenerateRCFileAction(_AccessRunObjectAction):
values: str | Sequence[Any] | None,
option_string: str | None = "--generate-rcfile",
) -> None:
- # TODO: 2.x: Deprecate this after the auto-upgrade functionality of
+ # TODO: 3.x: Deprecate this after the auto-upgrade functionality of
# pylint-config is sufficient.
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- self.run.linter.generate_config(skipsections=("Commands",))
+ self.run.linter._generate_config(skipsections=("Commands",))
sys.exit(0)
diff --git a/pylint/config/configuration_mixin.py b/pylint/config/configuration_mixin.py
deleted file mode 100644
index 55857224a..000000000
--- a/pylint/config/configuration_mixin.py
+++ /dev/null
@@ -1,41 +0,0 @@
-# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
-# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
-# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
-
-from __future__ import annotations
-
-import warnings
-from typing import Any
-
-from pylint.config.option_manager_mixin import OptionsManagerMixIn
-from pylint.config.options_provider_mixin import ( # type: ignore[attr-defined]
- OptionsProviderMixIn,
-)
-
-
-class ConfigurationMixIn(OptionsManagerMixIn, OptionsProviderMixIn): # type: ignore[misc]
- """Basic mixin for simple configurations which don't need the
- manager / providers model.
- """
-
- def __init__(self, *args: Any, **kwargs: Any) -> None:
- # TODO: 3.0: Remove deprecated class
- warnings.warn(
- "ConfigurationMixIn has been deprecated and will be removed in pylint 3.0",
- DeprecationWarning,
- stacklevel=2,
- )
- if not args:
- kwargs.setdefault("usage", "")
- OptionsManagerMixIn.__init__(self, *args, **kwargs)
- OptionsProviderMixIn.__init__(self)
- if not getattr(self, "option_groups", None):
- self.option_groups: list[tuple[str, str]] = []
- for _, optdict in self.options:
- try:
- gdef = (optdict["group"].upper(), "")
- except KeyError:
- continue
- if gdef not in self.option_groups:
- self.option_groups.append(gdef)
- self.register_options_provider(self, own_group=False)
diff --git a/pylint/config/environment_variable.py b/pylint/config/environment_variable.py
deleted file mode 100644
index 291b1651c..000000000
--- a/pylint/config/environment_variable.py
+++ /dev/null
@@ -1,11 +0,0 @@
-# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
-# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
-# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
-
-import warnings
-
-from pylint.config.find_default_config_files import find_pylintrc
-
-with warnings.catch_warnings():
- warnings.simplefilter("ignore")
- PYLINTRC = find_pylintrc()
diff --git a/pylint/config/find_default_config_files.py b/pylint/config/find_default_config_files.py
index 43e682a58..15c0d35bc 100644
--- a/pylint/config/find_default_config_files.py
+++ b/pylint/config/find_default_config_files.py
@@ -7,7 +7,6 @@ from __future__ import annotations
import configparser
import os
import sys
-import warnings
from collections.abc import Iterator
from pathlib import Path
@@ -110,21 +109,3 @@ def find_default_config_files() -> Iterator[Path]:
yield Path("/etc/pylintrc").resolve()
except OSError:
pass
-
-
-def find_pylintrc() -> str | None:
- """Search the pylint rc file and return its path if it finds it, else return
- None.
- """
- # TODO: 3.0: Remove deprecated function
- warnings.warn(
- "find_pylintrc and the PYLINTRC constant have been deprecated. "
- "Use find_default_config_files if you want access to pylint's configuration file "
- "finding logic.",
- DeprecationWarning,
- stacklevel=2,
- )
- for config_file in find_default_config_files():
- if str(config_file).endswith("pylintrc"):
- return str(config_file)
- return None
diff --git a/pylint/config/option.py b/pylint/config/option.py
deleted file mode 100644
index 74e4d45d8..000000000
--- a/pylint/config/option.py
+++ /dev/null
@@ -1,239 +0,0 @@
-# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
-# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
-# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
-
-from __future__ import annotations
-
-import copy
-import optparse # pylint: disable=deprecated-module
-import pathlib
-import re
-import warnings
-from collections.abc import Callable, Sequence
-from re import Pattern
-from typing import Any
-
-from pylint import utils
-
-
-# pylint: disable=unused-argument
-def _csv_validator(
- _: Any, name: str, value: str | list[str] | tuple[str]
-) -> Sequence[str]:
- return utils._check_csv(value)
-
-
-# pylint: disable=unused-argument
-def _regexp_validator(
- _: Any, name: str, value: str | re.Pattern[str]
-) -> re.Pattern[str]:
- if hasattr(value, "pattern"):
- return value # type: ignore[return-value]
- return re.compile(value)
-
-
-# pylint: disable=unused-argument
-def _regexp_csv_validator(
- _: Any, name: str, value: str | list[str]
-) -> list[re.Pattern[str]]:
- return [_regexp_validator(_, name, val) for val in _csv_validator(_, name, value)]
-
-
-def _regexp_paths_csv_validator(
- _: Any, name: str, value: str | list[Pattern[str]]
-) -> list[Pattern[str]]:
- if isinstance(value, list):
- return value
- patterns = []
- for val in _csv_validator(_, name, value):
- patterns.append(
- re.compile(
- str(pathlib.PureWindowsPath(val)).replace("\\", "\\\\")
- + "|"
- + pathlib.PureWindowsPath(val).as_posix()
- )
- )
- return patterns
-
-
-def _choice_validator(choices: list[Any], name: str, value: Any) -> Any:
- if value not in choices:
- msg = "option %s: invalid value: %r, should be in %s"
- raise optparse.OptionValueError(msg % (name, value, choices))
- return value
-
-
-def _yn_validator(opt: str, _: str, value: Any) -> bool:
- if isinstance(value, int):
- return bool(value)
- if isinstance(value, str):
- value = value.lower()
- if value in {"y", "yes", "true"}:
- return True
- if value in {"n", "no", "false"}:
- return False
- msg = "option %s: invalid yn value %r, should be in (y, yes, true, n, no, false)"
- raise optparse.OptionValueError(msg % (opt, value))
-
-
-def _multiple_choice_validator(choices: list[Any], name: str, value: Any) -> Any:
- values = utils._check_csv(value)
- for csv_value in values:
- if csv_value not in choices:
- msg = "option %s: invalid value: %r, should be in %s"
- raise optparse.OptionValueError(msg % (name, csv_value, choices))
- return values
-
-
-def _non_empty_string_validator( # pragma: no cover # Unused
- opt: Any, _: str, value: str
-) -> str:
- if not value:
- msg = "indent string can't be empty."
- raise optparse.OptionValueError(msg)
- return utils._unquote(value)
-
-
-def _multiple_choices_validating_option( # pragma: no cover # Unused
- opt: optparse.Option, name: str, value: Any
-) -> Any:
- return _multiple_choice_validator(
- opt.choices, name, value # type: ignore[attr-defined]
- )
-
-
-def _py_version_validator(_: Any, name: str, value: Any) -> tuple[int, int, int]:
- if not isinstance(value, tuple):
- try:
- value = tuple(int(val) for val in value.split("."))
- except (ValueError, AttributeError):
- raise optparse.OptionValueError(
- f"Invalid format for {name}, should be version string. E.g., '3.8'"
- ) from None
- return value # type: ignore[no-any-return]
-
-
-VALIDATORS: dict[str, Callable[[Any, str, Any], Any] | Callable[[Any], Any]] = {
- "string": utils._unquote,
- "int": int,
- "float": float,
- "glob_paths_csv": _csv_validator,
- "regexp": lambda pattern: re.compile(pattern or ""),
- "regexp_csv": _regexp_csv_validator,
- "regexp_paths_csv": _regexp_paths_csv_validator,
- "csv": _csv_validator,
- "yn": _yn_validator,
- "choice": lambda opt, name, value: _choice_validator(opt["choices"], name, value),
- "confidence": lambda opt, name, value: _multiple_choice_validator(
- opt["choices"], name, value
- ),
- "multiple_choice": lambda opt, name, value: _multiple_choice_validator(
- opt["choices"], name, value
- ),
- "non_empty_string": _non_empty_string_validator,
- "py_version": _py_version_validator,
-}
-
-
-def _call_validator(opttype: str, optdict: Any, option: str, value: Any) -> Any:
- if opttype not in VALIDATORS:
- raise TypeError(f'Unsupported type "{opttype}"')
- try:
- return VALIDATORS[opttype](optdict, option, value) # type: ignore[call-arg]
- except TypeError:
- try:
- return VALIDATORS[opttype](value) # type: ignore[call-arg]
- except Exception as e:
- raise optparse.OptionValueError(
- f"{option} value ({value!r}) should be of type {opttype}"
- ) from e
-
-
-def _validate(value: Any, optdict: Any, name: str = "") -> Any:
- """Return a validated value for an option according to its type.
-
- optional argument name is only used for error message formatting
- """
- try:
- _type = optdict["type"]
- except KeyError:
- return value
- return _call_validator(_type, optdict, name, value)
-
-
-# pylint: disable=no-member
-class Option(optparse.Option):
- TYPES = optparse.Option.TYPES + (
- "glob_paths_csv",
- "regexp",
- "regexp_csv",
- "regexp_paths_csv",
- "csv",
- "yn",
- "confidence",
- "multiple_choice",
- "non_empty_string",
- "py_version",
- )
- ATTRS = optparse.Option.ATTRS + ["hide", "level"]
- TYPE_CHECKER = copy.copy(optparse.Option.TYPE_CHECKER)
- TYPE_CHECKER["glob_paths_csv"] = _csv_validator
- TYPE_CHECKER["regexp"] = _regexp_validator
- TYPE_CHECKER["regexp_csv"] = _regexp_csv_validator
- TYPE_CHECKER["regexp_paths_csv"] = _regexp_paths_csv_validator
- TYPE_CHECKER["csv"] = _csv_validator
- TYPE_CHECKER["yn"] = _yn_validator
- TYPE_CHECKER["confidence"] = _multiple_choices_validating_option
- TYPE_CHECKER["multiple_choice"] = _multiple_choices_validating_option
- TYPE_CHECKER["non_empty_string"] = _non_empty_string_validator
- TYPE_CHECKER["py_version"] = _py_version_validator
-
- def __init__(self, *opts: Any, **attrs: Any) -> None:
- # TODO: 3.0: Remove deprecated class
- warnings.warn(
- "Option has been deprecated and will be removed in pylint 3.0",
- DeprecationWarning,
- stacklevel=2,
- )
- super().__init__(*opts, **attrs)
- if hasattr(self, "hide") and self.hide:
- self.help = optparse.SUPPRESS_HELP
-
- def _check_choice(self) -> None:
- if self.type in {"choice", "multiple_choice", "confidence"}:
- if self.choices is None: # type: ignore[attr-defined]
- raise optparse.OptionError(
- "must supply a list of choices for type 'choice'", self
- )
- if not isinstance(self.choices, (tuple, list)): # type: ignore[attr-defined]
- raise optparse.OptionError(
- # pylint: disable-next=consider-using-f-string
- "choices must be a list of strings ('%s' supplied)"
- % str(type(self.choices)).split("'")[1], # type: ignore[attr-defined]
- self,
- )
- elif self.choices is not None: # type: ignore[attr-defined]
- raise optparse.OptionError(
- f"must not supply choices for type {self.type!r}", self
- )
-
- optparse.Option.CHECK_METHODS[2] = _check_choice # type: ignore[index]
-
- def process( # pragma: no cover # Argparse
- self, opt: Any, value: Any, values: Any, parser: Any
- ) -> int:
- assert isinstance(self.dest, str)
- if self.callback and self.callback.__module__ == "pylint.lint.run":
- return 1
- # First, convert the value(s) to the right type. Howl if any
- # value(s) are bogus.
- value = self.convert_value(opt, value)
- if self.type == "named":
- existent = getattr(values, self.dest)
- if existent:
- existent.update(value)
- value = existent
- # And then take whatever action is expected of us.
- # This is a separate method to make life easier for
- # subclasses to add new actions.
- return self.take_action(self.action, self.dest, opt, value, values, parser)
diff --git a/pylint/config/option_manager_mixin.py b/pylint/config/option_manager_mixin.py
deleted file mode 100644
index c468f494f..000000000
--- a/pylint/config/option_manager_mixin.py
+++ /dev/null
@@ -1,372 +0,0 @@
-# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
-# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
-# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
-
-
-# pylint: disable=duplicate-code
-
-from __future__ import annotations
-
-import collections
-import configparser
-import contextlib
-import copy
-import optparse # pylint: disable=deprecated-module
-import os
-import sys
-import warnings
-from collections.abc import Iterator
-from pathlib import Path
-from typing import TYPE_CHECKING, Any, TextIO
-
-from pylint import utils
-from pylint.config.option import Option
-from pylint.config.option_parser import OptionParser # type: ignore[attr-defined]
-from pylint.typing import OptionDict
-
-if TYPE_CHECKING:
- from pylint.config.options_provider_mixin import ( # type: ignore[attr-defined]
- OptionsProviderMixin,
- )
-
-if sys.version_info >= (3, 11):
- import tomllib
-else:
- import tomli as tomllib
-
-
-def _expand_default(self: optparse.HelpFormatter, option: Option) -> str:
- """Patch OptionParser.expand_default with custom behaviour.
-
- This will handle defaults to avoid overriding values in the
- configuration file.
- """
- if self.parser is None or not self.default_tag:
- return str(option.help)
- optname = option._long_opts[0][2:]
- try:
- provider = self.parser.options_manager._all_options[optname] # type: ignore[attr-defined]
- except KeyError:
- value = None
- else:
- optdict = provider.get_option_def(optname)
- optname = provider.option_attrname(optname, optdict)
- value = getattr(provider.config, optname, optdict)
- value = utils._format_option_value(optdict, value)
- if value is optparse.NO_DEFAULT or not value:
- value = self.NO_DEFAULT_VALUE
- return option.help.replace(self.default_tag, str(value)) # type: ignore[union-attr]
-
-
-@contextlib.contextmanager
-def _patch_optparse() -> Iterator[None]:
- # pylint: disable = redefined-variable-type
- orig_default = optparse.HelpFormatter
- try:
- optparse.HelpFormatter.expand_default = _expand_default # type: ignore[assignment]
- yield
- finally:
- optparse.HelpFormatter.expand_default = orig_default # type: ignore[assignment]
-
-
-class OptionsManagerMixIn:
- """Handle configuration from both a configuration file and command line options."""
-
- def __init__(self, usage: str) -> None:
- # TODO: 3.0: Remove deprecated class
- warnings.warn(
- "OptionsManagerMixIn has been deprecated and will be removed in pylint 3.0",
- DeprecationWarning,
- stacklevel=2,
- )
- self.reset_parsers(usage)
- # list of registered options providers
- self.options_providers: list[OptionsProviderMixin] = []
- # dictionary associating option name to checker
- self._all_options: collections.OrderedDict[Any, Any] = collections.OrderedDict()
- self._short_options: dict[Any, Any] = {}
- self._nocallback_options: dict[Any, Any] = {}
- self._mygroups: dict[Any, Any] = {}
- # verbosity
- self._maxlevel = 0
-
- def reset_parsers(self, usage: str = "") -> None:
- # configuration file parser
- self.cfgfile_parser = configparser.ConfigParser(
- inline_comment_prefixes=("#", ";")
- )
- # command line parser
- self.cmdline_parser = OptionParser(Option, usage=usage)
- self.cmdline_parser.options_manager = self
- self._optik_option_attrs = set(self.cmdline_parser.option_class.ATTRS)
-
- def register_options_provider(
- self, provider: OptionsProviderMixin, own_group: bool = True
- ) -> None:
- """Register an options provider."""
- self.options_providers.append(provider)
- non_group_spec_options = [
- option for option in provider.options if "group" not in option[1]
- ]
- groups = getattr(provider, "option_groups", ())
- if own_group and non_group_spec_options:
- self.add_option_group(
- provider.name.upper(),
- provider.__doc__,
- non_group_spec_options,
- provider,
- )
- else:
- for opt, optdict in non_group_spec_options:
- self.add_optik_option(provider, self.cmdline_parser, opt, optdict)
- for gname, gdoc in groups:
- gname = gname.upper()
- goptions = [
- option
- for option in provider.options
- if option[1].get("group", "").upper() == gname
- ]
- self.add_option_group(gname, gdoc, goptions, provider)
-
- def add_option_group(
- self, group_name: str, _: Any, options: Any, provider: OptionsProviderMixin
- ) -> None:
- # add option group to the command line parser
- if group_name in self._mygroups:
- group = self._mygroups[group_name]
- else:
- group = optparse.OptionGroup(
- self.cmdline_parser, title=group_name.capitalize()
- )
- self.cmdline_parser.add_option_group(group)
- self._mygroups[group_name] = group
- # add section to the config file
- if (
- group_name != "DEFAULT"
- and group_name not in self.cfgfile_parser._sections # type: ignore[attr-defined]
- ):
- self.cfgfile_parser.add_section(group_name)
- # add provider's specific options
- for opt, optdict in options:
- if not isinstance(optdict.get("action", "store"), str):
- optdict["action"] = "callback"
- self.add_optik_option(provider, group, opt, optdict)
-
- def add_optik_option(
- self,
- provider: OptionsProviderMixin,
- optikcontainer: Any,
- opt: str,
- optdict: OptionDict,
- ) -> None:
- args, optdict = self.optik_option(provider, opt, optdict)
- option = optikcontainer.add_option(*args, **optdict)
- self._all_options[opt] = provider
- self._maxlevel = max(self._maxlevel, option.level or 0)
-
- def optik_option(
- self, provider: OptionsProviderMixin, opt: str, optdict: OptionDict
- ) -> tuple[list[str], OptionDict]:
- """Get our personal option definition and return a suitable form for
- use with optik/optparse.
- """
- optdict = copy.copy(optdict)
- if "action" in optdict:
- self._nocallback_options[provider] = opt
- else:
- optdict["action"] = "callback"
- optdict["callback"] = self.cb_set_provider_option
- # default is handled here and *must not* be given to optik if you
- # want the whole machinery to work
- if "default" in optdict:
- if (
- "help" in optdict
- and optdict.get("default") is not None
- and optdict["action"] not in ("store_true", "store_false")
- ):
- optdict["help"] += " [current: %default]" # type: ignore[operator]
- del optdict["default"]
- args = ["--" + str(opt)]
- if "short" in optdict:
- self._short_options[optdict["short"]] = opt
- args.append("-" + optdict["short"]) # type: ignore[operator]
- del optdict["short"]
- # cleanup option definition dict before giving it to optik
- for key in list(optdict.keys()):
- if key not in self._optik_option_attrs:
- optdict.pop(key)
- return args, optdict
-
- def cb_set_provider_option(
- self, option: Option, opt: str, value: Any, parser: Any
- ) -> None:
- """Optik callback for option setting."""
- if opt.startswith("--"):
- # remove -- on long option
- opt = opt[2:]
- else:
- # short option, get its long equivalent
- opt = self._short_options[opt[1:]]
- # trick since we can't set action='store_true' on options
- if value is None:
- value = 1
- self.global_set_option(opt, value)
-
- def global_set_option(self, opt: str, value: Any) -> None:
- """Set option on the correct option provider."""
- self._all_options[opt].set_option(opt, value)
-
- def generate_config(
- self, stream: TextIO | None = None, skipsections: tuple[str, ...] = ()
- ) -> None:
- """Write a configuration file according to the current configuration
- into the given stream or stdout.
- """
- options_by_section: dict[str, list[tuple[str, OptionDict, Any]]] = {}
- sections = []
- for provider in self.options_providers:
- for section, options in provider.options_by_section():
- if section is None:
- section = provider.name
- if section in skipsections:
- continue
- options = [
- (n, d, v)
- for (n, d, v) in options
- if d.get("type") is not None and not d.get("deprecated")
- ]
- if not options:
- continue
- if section not in sections:
- sections.append(section)
- all_options = options_by_section.setdefault(section, [])
- all_options += options
- stream = stream or sys.stdout
- printed = False
- for section in sections:
- if printed:
- print("\n", file=stream)
- utils.format_section(
- stream, section.upper(), sorted(options_by_section[section])
- )
- printed = True
-
- def load_provider_defaults(self) -> None:
- """Initialize configuration using default values."""
- for provider in self.options_providers:
- provider.load_defaults()
-
- def read_config_file(
- self, config_file: Path | None = None, verbose: bool = False
- ) -> None:
- """Read the configuration file but do not load it (i.e. dispatching
- values to each option's provider).
- """
- if config_file:
- config_file = Path(os.path.expandvars(config_file)).expanduser()
- if not config_file.exists():
- raise OSError(f"The config file {str(config_file)} doesn't exist!")
-
- parser = self.cfgfile_parser
- if config_file.suffix == ".toml":
- try:
- self._parse_toml(config_file, parser)
- except tomllib.TOMLDecodeError:
- pass
- else:
- # Use this encoding in order to strip the BOM marker, if any.
- with open(config_file, encoding="utf_8_sig") as fp:
- parser.read_file(fp)
- # normalize each section's title
- for sect, values in list(parser._sections.items()): # type: ignore[attr-defined]
- if sect.startswith("pylint."):
- sect = sect[len("pylint.") :]
- if not sect.isupper() and values:
- parser._sections[sect.upper()] = values # type: ignore[attr-defined]
-
- if not verbose:
- return
- if config_file and config_file.exists():
- msg = f"Using config file '{config_file}'"
- else:
- msg = "No config file found, using default configuration"
- print(msg, file=sys.stderr)
-
- def _parse_toml(self, config_file: Path, parser: configparser.ConfigParser) -> None:
- """Parse and handle errors of a toml configuration file."""
- with open(config_file, mode="rb") as fp:
- content = tomllib.load(fp)
- try:
- sections_values = content["tool"]["pylint"]
- except KeyError:
- return
- for section, values in sections_values.items():
- section_name = section.upper()
- # TOML has rich types, convert values to
- # strings as ConfigParser expects.
- if not isinstance(values, dict):
- # This class is a mixin: add_message comes from the `PyLinter` class
- self.add_message( # type: ignore[attr-defined]
- "bad-configuration-section", line=0, args=(section, values)
- )
- continue
- for option, value in values.items():
- if isinstance(value, bool):
- values[option] = "yes" if value else "no"
- elif isinstance(value, list):
- values[option] = ",".join(value)
- else:
- values[option] = str(value)
- for option, value in values.items():
- try:
- parser.set(section_name, option, value=value)
- except configparser.NoSectionError:
- parser.add_section(section_name)
- parser.set(section_name, option, value=value)
-
- def load_config_file(self) -> None:
- """Dispatch values previously read from a configuration file to each
- option's provider.
- """
- parser = self.cfgfile_parser
- for section in parser.sections():
- for option, value in parser.items(section):
- try:
- self.global_set_option(option, value)
- except (KeyError, optparse.OptionError):
- continue
-
- def load_configuration(self, **kwargs: Any) -> None:
- """Override configuration according to given parameters."""
- return self.load_configuration_from_config(kwargs)
-
- def load_configuration_from_config(self, config: dict[str, Any]) -> None:
- for opt, opt_value in config.items():
- opt = opt.replace("_", "-")
- provider = self._all_options[opt]
- provider.set_option(opt, opt_value)
-
- def load_command_line_configuration(
- self, args: list[str] | None = None
- ) -> list[str]:
- """Override configuration according to command line parameters.
-
- return additional arguments
- """
- with _patch_optparse():
- args = sys.argv[1:] if args is None else list(args)
- (options, args) = self.cmdline_parser.parse_args(args=args)
- for provider in self._nocallback_options:
- config = provider.config
- for attr in config.__dict__.keys():
- value = getattr(options, attr, None)
- if value is None:
- continue
- setattr(config, attr, value)
- return args # type: ignore[return-value]
-
- def help(self, level: int = 0) -> str:
- """Return the usage string for available options."""
- self.cmdline_parser.formatter.output_level = level
- with _patch_optparse():
- return str(self.cmdline_parser.format_help())
diff --git a/pylint/config/option_parser.py b/pylint/config/option_parser.py
deleted file mode 100644
index 9ffb80116..000000000
--- a/pylint/config/option_parser.py
+++ /dev/null
@@ -1,56 +0,0 @@
-# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
-# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
-# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
-
-# type: ignore # Deprecated module.
-
-import optparse # pylint: disable=deprecated-module
-import warnings
-
-from pylint.config.option import Option
-
-
-def _level_options(group, outputlevel):
- return [
- option
- for option in group.option_list
- if (getattr(option, "level", 0) or 0) <= outputlevel
- and option.help is not optparse.SUPPRESS_HELP
- ]
-
-
-class OptionParser(optparse.OptionParser):
- def __init__(self, option_class, *args, **kwargs):
- # TODO: 3.0: Remove deprecated class
- warnings.warn(
- "OptionParser has been deprecated and will be removed in pylint 3.0",
- DeprecationWarning,
- stacklevel=2,
- )
- super().__init__(option_class=Option, *args, **kwargs) # noqa: B026
-
- def format_option_help(self, formatter=None):
- if formatter is None:
- formatter = self.formatter
- outputlevel = getattr(formatter, "output_level", 0)
- formatter.store_option_strings(self)
- result = [formatter.format_heading("Options")]
- formatter.indent()
- if self.option_list:
- result.append(optparse.OptionContainer.format_option_help(self, formatter))
- result.append("\n")
- for group in self.option_groups:
- if group.level <= outputlevel and (
- group.description or _level_options(group, outputlevel)
- ):
- result.append(group.format_help(formatter))
- result.append("\n")
- formatter.dedent()
- # Drop the last "\n", or the header if no options or option groups:
- return "".join(result[:-1])
-
- def _match_long_opt(self, opt): # pragma: no cover # Unused
- """Disable abbreviations."""
- if opt not in self._long_opt:
- raise optparse.BadOptionError(opt)
- return opt
diff --git a/pylint/config/options_provider_mixin.py b/pylint/config/options_provider_mixin.py
deleted file mode 100644
index 67f64ee0a..000000000
--- a/pylint/config/options_provider_mixin.py
+++ /dev/null
@@ -1,123 +0,0 @@
-# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
-# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
-# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
-
-# type: ignore # Deprecated module.
-
-import optparse # pylint: disable=deprecated-module
-import warnings
-
-from pylint.config.callback_actions import _CallbackAction
-from pylint.config.option import _validate
-from pylint.typing import Options
-
-
-class UnsupportedAction(Exception):
- """Raised by set_option when it doesn't know what to do for an action."""
-
-
-class OptionsProviderMixIn:
- """Mixin to provide options to an OptionsManager."""
-
- # those attributes should be overridden
- name = "default"
- options: Options = ()
- level = 0
-
- def __init__(self):
- # TODO: 3.0: Remove deprecated class
- warnings.warn(
- "OptionsProviderMixIn has been deprecated and will be removed in pylint 3.0",
- DeprecationWarning,
- stacklevel=2,
- )
- self.config = optparse.Values()
- self.load_defaults()
-
- def load_defaults(self):
- """Initialize the provider using default values."""
- for opt, optdict in self.options:
- action = optdict.get("action")
- if action != "callback":
- # callback action have no default
- if optdict is None:
- optdict = self.get_option_def(opt)
- default = optdict.get("default")
- self.set_option(opt, default, action, optdict)
-
- def option_attrname(self, opt, optdict=None):
- """Get the config attribute corresponding to opt."""
- if optdict is None:
- optdict = self.get_option_def(opt)
- return optdict.get("dest", opt.replace("-", "_"))
-
- def option_value(self, opt):
- """Get the current value for the given option."""
- return getattr(self.config, self.option_attrname(opt), None)
-
- def set_option(self, optname, value, action=None, optdict=None):
- """Method called to set an option (registered in the options list)."""
- if optdict is None:
- optdict = self.get_option_def(optname)
- if value is not None:
- value = _validate(value, optdict, optname)
- if action is None:
- action = optdict.get("action", "store")
- if action == "store":
- setattr(self.config, self.option_attrname(optname, optdict), value)
- elif action in {"store_true", "count"}:
- setattr(self.config, self.option_attrname(optname, optdict), value)
- elif action == "store_false":
- setattr(self.config, self.option_attrname(optname, optdict), value)
- elif action == "append":
- optname = self.option_attrname(optname, optdict)
- _list = getattr(self.config, optname, None)
- if _list is None:
- if isinstance(value, (list, tuple)):
- _list = value
- elif value is not None:
- _list = [value]
- setattr(self.config, optname, _list)
- elif isinstance(_list, tuple):
- setattr(self.config, optname, _list + (value,))
- else:
- _list.append(value)
- elif (
- action == "callback"
- or (not isinstance(action, str))
- and issubclass(action, _CallbackAction)
- ):
- return
- else:
- raise UnsupportedAction(action)
-
- def get_option_def(self, opt):
- """Return the dictionary defining an option given its name."""
- assert self.options
- for option in self.options:
- if option[0] == opt:
- return option[1]
- raise optparse.OptionError(
- f"no such option {opt} in section {self.name!r}", opt
- )
-
- def options_by_section(self):
- """Return an iterator on options grouped by section.
-
- (section, [list of (optname, optdict, optvalue)])
- """
- sections = {}
- for optname, optdict in self.options:
- sections.setdefault(optdict.get("group"), []).append(
- (optname, optdict, self.option_value(optname))
- )
- if None in sections:
- yield None, sections.pop(None)
- for section, options in sorted(sections.items()):
- yield section.upper(), options
-
- def options_and_values(self, options=None):
- if options is None:
- options = self.options
- for optname, optdict in options:
- yield optname, optdict, self.option_value(optname)
diff --git a/pylint/config/utils.py b/pylint/config/utils.py
index d7cbd7c07..eaab2493c 100644
--- a/pylint/config/utils.py
+++ b/pylint/config/utils.py
@@ -7,7 +7,6 @@
from __future__ import annotations
import re
-import warnings
from collections.abc import Callable, Sequence
from pathlib import Path
from typing import TYPE_CHECKING, Any
@@ -39,14 +38,6 @@ def _convert_option_to_argument(
| _ExtendArgument
):
"""Convert an optdict to an Argument class instance."""
- if "level" in optdict and "hide" not in optdict:
- warnings.warn(
- "The 'level' key in optdicts has been deprecated. "
- "Use 'hide' with a boolean to hide an option from the help message. "
- f"optdict={optdict}",
- DeprecationWarning,
- )
-
# Get the long and short flags
flags = [f"--{opt}"]
if "short" in optdict:
@@ -74,17 +65,9 @@ def _convert_option_to_argument(
section=optdict.get("group", None),
metavar=optdict.get("metavar", None),
)
- try:
- default = optdict["default"]
- except KeyError:
- warnings.warn(
- "An option dictionary should have a 'default' key to specify "
- "the option's default value. This key will be required in pylint "
- "3.0. It is not required for 'store_true' and callable actions. "
- f"optdict={optdict}",
- DeprecationWarning,
- )
- default = None
+
+ default = optdict["default"]
+
if action == "extend":
return _ExtendArgument(
flags=flags,
diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py
index 863076f8f..b35867eae 100644
--- a/pylint/lint/pylinter.py
+++ b/pylint/lint/pylinter.py
@@ -346,25 +346,6 @@ class PyLinter(
self.register_checker(self)
- @property
- def option_groups(self) -> tuple[tuple[str, str], ...]:
- # TODO: 3.0: Remove deprecated attribute
- warnings.warn(
- "The option_groups attribute has been deprecated and will be removed in pylint 3.0",
- DeprecationWarning,
- stacklevel=2,
- )
- return self._option_groups
-
- @option_groups.setter
- def option_groups(self, value: tuple[tuple[str, str], ...]) -> None:
- warnings.warn(
- "The option_groups attribute has been deprecated and will be removed in pylint 3.0",
- DeprecationWarning,
- stacklevel=2,
- )
- self._option_groups = value
-
def load_default_plugins(self) -> None:
checkers.initialize(self)
reporters.initialize(self)
diff --git a/pylint/lint/utils.py b/pylint/lint/utils.py
index 98fb8087a..950c94b85 100644
--- a/pylint/lint/utils.py
+++ b/pylint/lint/utils.py
@@ -12,7 +12,7 @@ from collections.abc import Iterator, Sequence
from datetime import datetime
from pathlib import Path
-from pylint.config import PYLINT_HOME
+from pylint.constants import PYLINT_HOME
from pylint.lint.expand_modules import discover_package_path
diff --git a/pylint/utils/docs.py b/pylint/utils/docs.py
index ebd7cc8c3..b670c1bc7 100644
--- a/pylint/utils/docs.py
+++ b/pylint/utils/docs.py
@@ -7,7 +7,6 @@
from __future__ import annotations
import sys
-import warnings
from typing import TYPE_CHECKING, Any, TextIO
from pylint.constants import MAIN_CHECKER_NAME
@@ -25,20 +24,16 @@ def _get_checkers_infos(linter: PyLinter) -> dict[str, dict[str, Any]]:
if name != MAIN_CHECKER_NAME:
try:
by_checker[name]["checker"] = checker
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- by_checker[name]["options"] += checker.options_and_values()
+ by_checker[name]["options"] += checker._options_and_values()
by_checker[name]["msgs"].update(checker.msgs)
by_checker[name]["reports"] += checker.reports
except KeyError:
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- by_checker[name] = {
- "checker": checker,
- "options": list(checker.options_and_values()),
- "msgs": dict(checker.msgs),
- "reports": list(checker.reports),
- }
+ by_checker[name] = {
+ "checker": checker,
+ "options": list(checker._options_and_values()),
+ "msgs": dict(checker.msgs),
+ "reports": list(checker.reports),
+ }
return by_checker
@@ -51,16 +46,14 @@ Pylint provides global options and switches.
"""
for checker in linter.get_checkers():
if checker.name == MAIN_CHECKER_NAME and checker.options:
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- for section, options in checker.options_by_section():
- if section is None:
- title = "General options"
- else:
- title = f"{section.capitalize()} options"
- result += get_rst_title(title, "~")
- assert isinstance(options, list)
- result += f"{get_rst_section(None, options)}\n"
+ for section, options in checker._options_by_section():
+ if section is None:
+ title = "General options"
+ else:
+ title = f"{section.capitalize()} options"
+ result += get_rst_title(title, "~")
+ assert isinstance(options, list)
+ result += f"{get_rst_section(None, options)}\n"
return result