diff options
author | Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com> | 2022-04-11 21:50:07 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-11 21:50:07 +0200 |
commit | 2c8ec14c3d81d55c3f3b01529cc174e25e7ad250 (patch) | |
tree | 9352012d1ca2a0004b384a748f65ecf8a7d158fe /pylint | |
parent | 53deea2753cc0d15614e105e213edc8b9a9356c2 (diff) | |
download | pylint-git-2c8ec14c3d81d55c3f3b01529cc174e25e7ad250.tar.gz |
Create ``_OutputFormatAction`` (#6267)
Diffstat (limited to 'pylint')
-rw-r--r-- | pylint/config/callback_actions.py | 61 | ||||
-rw-r--r-- | pylint/lint/pylinter.py | 10 | ||||
-rw-r--r-- | pylint/testutils/decorator.py | 2 | ||||
-rw-r--r-- | pylint/typing.py | 7 |
4 files changed, 76 insertions, 4 deletions
diff --git a/pylint/config/callback_actions.py b/pylint/config/callback_actions.py index 5c5431778..7c3aa092a 100644 --- a/pylint/config/callback_actions.py +++ b/pylint/config/callback_actions.py @@ -17,6 +17,7 @@ from pylint import extensions, interfaces, utils if TYPE_CHECKING: from pylint.config.help_formatter import _HelpFormatter + from pylint.lint import PyLinter from pylint.lint.run import Run @@ -283,3 +284,63 @@ class _LongHelpAction(_AccessRunObjectAction): print(self.run.linter.help()) sys.exit(0) + + +class _AccessLinterObjectAction(_CallbackAction): + """Action that has access to the Linter object.""" + + def __init__( + self, + option_strings: Sequence[str], + dest: str, + nargs: None = None, + const: None = None, + default: None = None, + type: None = None, + choices: None = None, + required: bool = False, + help: str = "", + metavar: str = "", + **kwargs: "PyLinter", + ) -> None: + self.linter = kwargs["linter"] + + super().__init__( + option_strings, + dest, + 1, + const, + default, + type, + choices, + required, + help, + metavar, + ) + + @abc.abstractmethod + def __call__( + self, + parser: argparse.ArgumentParser, + namespace: argparse.Namespace, + values: Union[str, Sequence[Any], None], + option_string: Optional[str] = None, + ) -> None: + raise NotImplementedError + + +class _OutputFormatAction(_AccessLinterObjectAction): + """Callback action for setting the output format.""" + + def __call__( + self, + parser: argparse.ArgumentParser, + namespace: argparse.Namespace, + values: Union[str, Sequence[Any], None], + option_string: Optional[str] = "--enable", + ) -> None: + assert isinstance(values, (tuple, list)) + assert isinstance( + values[0], str + ), "'output-format' should be a comma separated string of reporters" + self.linter._load_reporters(values[0]) diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index 8ee021c87..c531a5322 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -219,8 +219,7 @@ class PyLinter( # Will be used like this : datetime.now().strftime(crash_file_path) crash_file_path: str = "pylint-crash-%Y-%m-%d-%H.txt" - @staticmethod - def make_options() -> Options: + def make_options(self) -> Options: return ( ( "ignore", @@ -282,6 +281,10 @@ class PyLinter( { "default": "text", "type": "string", + # pylint: disable-next=fixme + # TODO: Optparse: Uncomment these lines. + # "action": _OutputFormatAction, + # "callback": lambda x: x, "metavar": "<format>", "short": "f", "group": "Reports", @@ -289,6 +292,7 @@ class PyLinter( " parseable, colorized, json and msvs (visual studio)." " You can also give a reporter class, e.g. mypackage.mymodule." "MyReporterClass.", + "kwargs": {"linter": self}, }, ), ( @@ -592,7 +596,7 @@ class PyLinter( # Attributes related to (command-line) options and their parsing self._external_opts = options - self.options: Options = options + PyLinter.make_options() + self.options: Options = options + self.make_options() for opt_group in option_groups: self.option_groups_descs[opt_group[0]] = opt_group[1] # pylint: disable-next=fixme diff --git a/pylint/testutils/decorator.py b/pylint/testutils/decorator.py index 6527ae49c..b21bb8d16 100644 --- a/pylint/testutils/decorator.py +++ b/pylint/testutils/decorator.py @@ -30,7 +30,7 @@ def set_config(**kwargs): self.checker.set_option( key.replace("_", "-"), value, - optdict=dict(PyLinter.make_options())[ + optdict=dict(PyLinter().make_options())[ key.replace("_", "-") ], ) diff --git a/pylint/typing.py b/pylint/typing.py index e62ac4d80..a1e4e5c0f 100644 --- a/pylint/typing.py +++ b/pylint/typing.py @@ -5,6 +5,7 @@ """A collection of typing utilities.""" import sys from typing import ( + TYPE_CHECKING, Any, Callable, Dict, @@ -13,6 +14,7 @@ from typing import ( Optional, Pattern, Tuple, + Type, Union, ) @@ -21,6 +23,9 @@ if sys.version_info >= (3, 8): else: from typing_extensions import Literal, TypedDict +if TYPE_CHECKING: + from pylint.config.callback_actions import _CallbackAction + class FileItem(NamedTuple): """Represents data about a file handled by pylint. @@ -92,6 +97,8 @@ OptionDict = Dict[ int, Pattern[str], Iterable[Union[str, int, Pattern[str]]], + Type["_CallbackAction"], + Callable[[Any], Any], Callable[[Any, Any, Any, Any], Any], ], ] |