-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0fbbb7
commit c447d7b
Showing
11 changed files
with
229 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# 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 | ||
|
||
"""Definition of an Argument class and validators for various argument types. | ||
An Argument instance represents a pylint option to be handled by an ArgumentsParser, | ||
which should be a subclass or instance of argparse.ArgumentsParser. | ||
""" | ||
|
||
|
||
from typing import Any, Callable, Dict, List, Optional, Union | ||
|
||
from pylint import utils as pylint_utils | ||
|
||
ArgumentTypes = Union[str, List[str]] | ||
"""List of types possible argument types""" | ||
|
||
|
||
def _csv_validator(value: Union[str, List[str]]) -> List[str]: | ||
"""Validates a comma separated string""" | ||
return pylint_utils._check_csv(value) | ||
|
||
|
||
ASSIGNMENT_VALIDATORS: Dict[str, Callable[[Any], ArgumentTypes]] = { | ||
"choice": str, | ||
"csv": _csv_validator, | ||
} | ||
"""Validators for all assignment types""" | ||
|
||
|
||
class _Argument: | ||
"""Class representing an argument to be passed by an argparse.ArgumentsParser. | ||
This is based on the parameters passed to argparse.ArgumentsParser.add_message. | ||
See: | ||
https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument""" | ||
|
||
def __init__( | ||
self, | ||
name: str, | ||
default: ArgumentTypes, | ||
arg_type: str, | ||
arg_help: str, | ||
metavar: str, | ||
choices: Optional[List[str]] = None, | ||
) -> None: | ||
self.name = name | ||
"""The name of the argument""" | ||
|
||
self.type = ASSIGNMENT_VALIDATORS[arg_type] | ||
"""A validator function that returns and checks the type of the argument.""" | ||
|
||
self.default = self.type(default) | ||
"""The default value of the argument.""" | ||
|
||
self.choices = choices or [] | ||
"""A list of possible choices for the argument. The list is empty if there are no restrictions.""" | ||
|
||
self.help = arg_help | ||
"""The description of the argument""" | ||
|
||
self.metavar = metavar | ||
"""The metavar of the argument. | ||
See: | ||
https://docs.python.org/3/library/argparse.html#metavar | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# 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 | ||
|
||
"""Arguments manager class used to handle command-line arguments and options""" | ||
|
||
import argparse | ||
|
||
|
||
class _ArgumentsManager: | ||
"""Arguments manager class used to handle command-line arguments and options""" | ||
|
||
def __init__(self, namespace: argparse.Namespace) -> None: | ||
"""We don't set the parser as an instance attribute as the ArgumentParser class | ||
can't be pickled on Windows. This blocks running pylint in parallel jobs. | ||
See: https://github.com/PyCQA/pylint/pull/5584#issuecomment-999190647""" | ||
parser = argparse.ArgumentParser(prog="pylint") | ||
|
||
self.namespace = namespace | ||
self.help_message = self._get_help_message(parser) | ||
|
||
@staticmethod | ||
def _get_help_message(parser: argparse.ArgumentParser) -> str: | ||
return parser.format_help() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 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 | ||
|
||
"""Utils for arguments/options parsing and handling.""" | ||
|
||
from typing import Any, Dict | ||
|
||
from pylint.config.argument import _Argument | ||
|
||
ArgumentsDict = Dict[str, _Argument] | ||
|
||
IMPLEMENTED_OPTDICT_KEYS = {"default", "type", "choices", "help", "metavar"} | ||
"""This is used to track our progress on accepting all optdict keys""" | ||
|
||
|
||
def _convert_option_to_argument(opt: str, optdict: Dict[str, Any]) -> _Argument: | ||
"""Convert an optdict to an Argument class instance""" | ||
# See if the optdict contains any keys we don't yet implement | ||
for key, value in optdict.items(): | ||
if key not in IMPLEMENTED_OPTDICT_KEYS: | ||
print("Unhandled key found in Argument creation:", key) | ||
print("It's value is:", value) | ||
return _Argument( | ||
name=opt, | ||
default=optdict["default"], | ||
arg_type=optdict["type"], | ||
choices=optdict.get("choices", []), | ||
arg_help=optdict["help"], | ||
metavar=optdict["metavar"], | ||
) | ||
|
||
|
||
def _validate_argument(argument: _Argument) -> None: | ||
"""Method to check if all instance attributes have the correct typed.""" | ||
# Check default value | ||
assert argument.default | ||
assert argument.default == argument.type(argument.default) | ||
|
||
# Check type value | ||
assert argument.type | ||
assert callable(argument.type) | ||
|
||
# Check choices value | ||
assert isinstance(argument.choices, list) | ||
assert all(isinstance(i, str) for i in argument.choices) | ||
|
||
# Check help value | ||
assert argument.help | ||
assert isinstance(argument.help, str) | ||
|
||
# Check metavar value | ||
assert argument.metavar | ||
assert isinstance(argument.metavar, str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""Test for the (new) implementation of option parsing with argparse""" | ||
|
||
import argparse | ||
from os.path import abspath, dirname, join | ||
|
||
from pylint import config | ||
from pylint.lint import PyLinter, Run | ||
|
||
HERE = abspath(dirname(__file__)) | ||
REGRTEST_DATA_DIR = join(HERE, "..", "regrtest_data") | ||
|
||
|
||
class TestArgumentsManager: | ||
"""Tests for the ArgumentsManager class""" | ||
|
||
@staticmethod | ||
def test_initialize_arguments_manager_class(linter: PyLinter) -> None: | ||
"""Tests that ArgumentsManager is correctly initialized on a PyLinter class""" | ||
assert isinstance(linter, config._ArgumentsManager) | ||
assert isinstance(linter.namespace, argparse.Namespace) | ||
|
||
@staticmethod | ||
def test_help_message(linter: PyLinter) -> None: | ||
"""Test the help_message attribute of the ArgumentsManager class""" | ||
assert isinstance(linter.help_message, str) | ||
lines = linter.help_message.splitlines() | ||
assert lines[0] == "usage: pylint [-h]" | ||
|
||
|
||
class TestOptionsProviderMixin: | ||
"""Tests for the argparse implementation of OptionsProviderMixin""" | ||
|
||
empty_module = join(REGRTEST_DATA_DIR, "empty.py") | ||
|
||
def test_logger_checker(self) -> None: | ||
"""Tests that we create Argument instances for the settings of the logger module""" | ||
run = Run([self.empty_module], exit=False) | ||
assert run.linter.namespace_test | ||
assert len(run.linter.namespace_test) == 2 | ||
|
||
assert "logging-modules" in run.linter.namespace_test | ||
logging_modules = run.linter.namespace_test["logging-modules"] | ||
assert isinstance(logging_modules, config._Argument) | ||
config._validate_argument(logging_modules) | ||
|
||
assert "logging-format-style" in run.linter.namespace_test | ||
logging_format_style = run.linter.namespace_test["logging-format-style"] | ||
assert isinstance(logging_format_style, config._Argument) | ||
config._validate_argument(logging_format_style) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters