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 python-typing-update on pylint/config directory #6307

Merged
merged 2 commits into from
Apr 14, 2022
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
80 changes: 35 additions & 45 deletions pylint/config/argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,13 @@
An Argument instance represents a pylint option to be handled by an argparse.ArgumentParser
"""

from __future__ import annotations

import argparse
import pathlib
import re
import sys
from typing import (
Any,
Callable,
Dict,
List,
Optional,
Pattern,
Sequence,
Tuple,
Type,
Union,
)
from typing import Any, Callable, Pattern, Sequence, Tuple, Union

from pylint import interfaces
from pylint import utils as pylint_utils
Expand Down Expand Up @@ -89,7 +79,7 @@ def _non_empty_string_transformer(value: str) -> str:
return pylint_utils._unquote(value)


def _py_version_transformer(value: str) -> Tuple[int, ...]:
def _py_version_transformer(value: str) -> tuple[int, ...]:
"""Transforms a version string into a version tuple."""
try:
version = tuple(int(val) for val in value.replace(",", ".").split("."))
Expand All @@ -102,15 +92,15 @@ def _py_version_transformer(value: str) -> Tuple[int, ...]:

def _regexp_csv_transfomer(value: str) -> Sequence[Pattern[str]]:
"""Transforms a comma separated list of regular expressions."""
patterns: List[Pattern[str]] = []
patterns: list[Pattern[str]] = []
for pattern in _csv_transformer(value):
patterns.append(re.compile(pattern))
return patterns


def _regexp_paths_csv_transfomer(value: str) -> Sequence[Pattern[str]]:
"""Transforms a comma separated list of regular expressions paths."""
patterns: List[Pattern[str]] = []
patterns: list[Pattern[str]] = []
for pattern in _csv_transformer(value):
patterns.append(
re.compile(
Expand All @@ -122,7 +112,7 @@ def _regexp_paths_csv_transfomer(value: str) -> Sequence[Pattern[str]]:
return patterns


_TYPE_TRANSFORMERS: Dict[str, Callable[[str], _ArgumentTypes]] = {
_TYPE_TRANSFORMERS: dict[str, Callable[[str], _ArgumentTypes]] = {
"choice": str,
"csv": _csv_transformer,
"float": float,
Expand Down Expand Up @@ -156,10 +146,10 @@ class _Argument:
def __init__(
self,
*,
flags: List[str],
flags: list[str],
arg_help: str,
hide_help: bool,
section: Optional[str],
section: str | None,
) -> None:
self.flags = flags
"""The name of the argument."""
Expand Down Expand Up @@ -189,12 +179,12 @@ class _BaseStoreArgument(_Argument):
def __init__(
self,
*,
flags: List[str],
flags: list[str],
action: str,
default: _ArgumentTypes,
arg_help: str,
hide_help: bool,
section: Optional[str],
section: str | None,
) -> None:
super().__init__(
flags=flags, arg_help=arg_help, hide_help=hide_help, section=section
Expand All @@ -218,15 +208,15 @@ class _StoreArgument(_BaseStoreArgument):
def __init__(
self,
*,
flags: List[str],
flags: list[str],
action: str,
default: _ArgumentTypes,
arg_type: str,
choices: Optional[List[str]],
choices: list[str] | None,
arg_help: str,
metavar: str,
hide_help: bool,
section: Optional[str],
section: str | None,
) -> None:
super().__init__(
flags=flags,
Expand Down Expand Up @@ -266,12 +256,12 @@ class _StoreTrueArgument(_BaseStoreArgument):
def __init__(
self,
*,
flags: List[str],
flags: list[str],
action: Literal["store_true"],
default: _ArgumentTypes,
arg_help: str,
hide_help: bool,
section: Optional[str],
section: str | None,
) -> None:
super().__init__(
flags=flags,
Expand All @@ -294,15 +284,15 @@ class _DeprecationArgument(_Argument):
def __init__(
self,
*,
flags: List[str],
action: Type[argparse.Action],
flags: list[str],
action: type[argparse.Action],
default: _ArgumentTypes,
arg_type: str,
choices: Optional[List[str]],
choices: list[str] | None,
arg_help: str,
metavar: str,
hide_help: bool,
section: Optional[str],
section: str | None,
) -> None:
super().__init__(
flags=flags, arg_help=arg_help, hide_help=hide_help, section=section
Expand Down Expand Up @@ -342,16 +332,16 @@ class _ExtendArgument(_DeprecationArgument):
def __init__(
self,
*,
flags: List[str],
flags: list[str],
action: Literal["extend"],
default: _ArgumentTypes,
arg_type: str,
metavar: str,
arg_help: str,
hide_help: bool,
section: Optional[str],
choices: Optional[List[str]],
dest: Optional[str],
section: str | None,
choices: list[str] | None,
dest: str | None,
) -> None:
# The extend action is included in the stdlib from 3.8+
if PY38_PLUS:
Expand Down Expand Up @@ -386,15 +376,15 @@ class _StoreOldNamesArgument(_DeprecationArgument):
def __init__(
self,
*,
flags: List[str],
flags: list[str],
default: _ArgumentTypes,
arg_type: str,
choices: Optional[List[str]],
choices: list[str] | None,
arg_help: str,
metavar: str,
hide_help: bool,
kwargs: Dict[str, Any],
section: Optional[str],
kwargs: dict[str, Any],
section: str | None,
) -> None:
super().__init__(
flags=flags,
Expand Down Expand Up @@ -423,15 +413,15 @@ class _StoreNewNamesArgument(_DeprecationArgument):
def __init__(
self,
*,
flags: List[str],
flags: list[str],
default: _ArgumentTypes,
arg_type: str,
choices: Optional[List[str]],
choices: list[str] | None,
arg_help: str,
metavar: str,
hide_help: bool,
kwargs: Dict[str, Any],
section: Optional[str],
kwargs: dict[str, Any],
section: str | None,
) -> None:
super().__init__(
flags=flags,
Expand Down Expand Up @@ -460,12 +450,12 @@ class _CallableArgument(_Argument):
def __init__(
self,
*,
flags: List[str],
action: Type[_CallbackAction],
flags: list[str],
action: type[_CallbackAction],
arg_help: str,
kwargs: Dict[str, Any],
kwargs: dict[str, Any],
hide_help: bool,
section: Optional[str],
section: str | None,
) -> None:
super().__init__(
flags=flags, arg_help=arg_help, hide_help=hide_help, section=section
Expand Down
48 changes: 25 additions & 23 deletions pylint/config/arguments_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

"""Arguments manager class used to handle command-line arguments and options."""

from __future__ import annotations

import argparse
import collections
import configparser
Expand All @@ -15,7 +17,7 @@
import textwrap
import warnings
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, List, Optional, TextIO, Tuple, Union
from typing import TYPE_CHECKING, Any, TextIO, Union

import tomlkit

Expand Down Expand Up @@ -58,7 +60,7 @@
class _ArgumentsManager:
"""Arguments manager class used to handle command-line arguments and options."""

def __init__(self, prog: str, usage: Optional[str] = None) -> None:
def __init__(self, prog: str, usage: str | None = None) -> None:
self.namespace = argparse.Namespace()
"""Namespace for all options."""

Expand All @@ -69,10 +71,10 @@ def __init__(self, prog: str, usage: Optional[str] = None) -> None:
)
"""The command line argument parser."""

self._argument_groups_dict: Dict[str, argparse._ArgumentGroup] = {}
self._argument_groups_dict: dict[str, argparse._ArgumentGroup] = {}
"""Dictionary of all the argument groups."""

self._option_dicts: Dict[str, OptionDict] = {}
self._option_dicts: dict[str, OptionDict] = {}
"""All option dictionaries that have been registered."""

# pylint: disable=fixme
Expand All @@ -82,20 +84,20 @@ def __init__(self, prog: str, usage: Optional[str] = None) -> None:
warnings.filterwarnings("ignore", category=DeprecationWarning)
self.reset_parsers(usage or "")
# list of registered options providers
self.options_providers: List[ConfigProvider] = []
self.options_providers: list[ConfigProvider] = []
# dictionary associating option name to checker
self._all_options: OrderedDict[str, ConfigProvider] = collections.OrderedDict()
self._short_options: Dict[str, str] = {}
self._nocallback_options: Dict[ConfigProvider, str] = {}
self._mygroups: Dict[str, optparse.OptionGroup] = {}
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:
return self.namespace

def _register_options_provider(self, provider: "_ArgumentsProvider") -> None:
def _register_options_provider(self, provider: _ArgumentsProvider) -> None:
"""Register an options provider and load its defaults."""
for opt, optdict in provider.options:
self._option_dicts[opt] = optdict
Expand All @@ -113,7 +115,7 @@ def _register_options_provider(self, provider: "_ArgumentsProvider") -> None:
self._load_default_argument_values()

def _add_arguments_to_parser(
self, section: str, section_desc: Optional[str], argument: _Argument
self, section: str, section_desc: str | None, argument: _Argument
) -> None:
"""Add an argument to the correct argument section/group."""
try:
Expand Down Expand Up @@ -210,15 +212,15 @@ def _load_default_argument_values(self) -> None:
"""Loads the default values of all registered options."""
self.namespace = self._arg_parser.parse_args([], self.namespace)

def _parse_configuration_file(self, arguments: List[str]) -> None:
def _parse_configuration_file(self, arguments: list[str]) -> None:
"""Parse the arguments found in a configuration file into the namespace."""
# pylint: disable-next=fixme
# TODO: This should parse_args instead of parse_known_args
self.namespace = self._arg_parser.parse_known_args(arguments, self.namespace)[0]

def _parse_command_line_configuration(
self, arguments: Optional[List[str]] = None
) -> List[str]:
self, arguments: list[str] | None = None
) -> list[str]:
"""Parse the arguments found on the command line into the namespace."""
arguments = sys.argv[1:] if arguments is None else arguments

Expand Down Expand Up @@ -287,8 +289,8 @@ def register_options_provider(
def add_option_group(
self,
group_name: str,
_: Optional[str],
options: List[Tuple[str, OptionDict]],
_: str | None,
options: list[tuple[str, OptionDict]],
provider: ConfigProvider,
) -> None:
"""DEPRECATED."""
Expand Down Expand Up @@ -325,7 +327,7 @@ def add_option_group(
def add_optik_option(
self,
provider: ConfigProvider,
optikcontainer: Union[optparse.OptionParser, optparse.OptionGroup],
optikcontainer: optparse.OptionParser | optparse.OptionGroup,
opt: str,
optdict: OptionDict,
) -> None:
Expand All @@ -344,7 +346,7 @@ def add_optik_option(

def optik_option(
self, provider: ConfigProvider, opt: str, optdict: OptionDict
) -> Tuple[List[str], OptionDict]:
) -> tuple[list[str], OptionDict]:
"""DEPRECATED: Get our personal option definition and return a suitable form for
use with optik/optparse
"""
Expand Down Expand Up @@ -381,7 +383,7 @@ def optik_option(
return args, optdict

def generate_config(
self, stream: Optional[TextIO] = None, skipsections: Tuple[str, ...] = ()
self, stream: TextIO | None = None, skipsections: tuple[str, ...] = ()
) -> None:
"""DEPRECATED: Write a configuration file according to the current configuration
into the given stream or stdout
Expand Down Expand Up @@ -450,7 +452,7 @@ def load_provider_defaults(self) -> None:
provider.load_defaults()

def read_config_file(
self, config_file: Optional[Path] = None, verbose: bool = False
self, config_file: Path | None = None, verbose: bool = False
) -> None:
"""DEPRECATED: Read the configuration file but do not load it (i.e. dispatching
values to each option's provider)
Expand Down Expand Up @@ -548,7 +550,7 @@ def load_configuration(self, **kwargs: Any) -> None:
warnings.filterwarnings("ignore", category=DeprecationWarning)
return self.load_configuration_from_config(kwargs)

def load_configuration_from_config(self, config: Dict[str, Any]) -> None:
def load_configuration_from_config(self, config: dict[str, Any]) -> None:
warnings.warn(
"DEPRECATED: load_configuration_from_config has been deprecated. It will be removed in pylint 3.0.",
DeprecationWarning,
Expand All @@ -559,8 +561,8 @@ def load_configuration_from_config(self, config: Dict[str, Any]) -> None:
provider.set_option(opt, opt_value)

def load_command_line_configuration(
self, args: Optional[List[str]] = None
) -> List[str]:
self, args: list[str] | None = None
) -> list[str]:
"""DEPRECATED: Override configuration according to command line parameters.

return additional arguments
Expand All @@ -580,7 +582,7 @@ def load_command_line_configuration(
setattr(config, attr, value) # pragma: no cover # Handled by argparse.
return args

def help(self, level: Optional[int] = None) -> str:
def help(self, level: int | None = None) -> str:
"""Return the usage string based on the available options."""
if level is not None:
warnings.warn(
Expand Down
Loading