diff options
Diffstat (limited to 'pylint/reporters')
-rw-r--r-- | pylint/reporters/__init__.py | 4 | ||||
-rw-r--r-- | pylint/reporters/base_reporter.py | 23 | ||||
-rw-r--r-- | pylint/reporters/collecting_reporter.py | 4 | ||||
-rw-r--r-- | pylint/reporters/json_reporter.py | 6 | ||||
-rw-r--r-- | pylint/reporters/multi_reporter.py | 4 | ||||
-rw-r--r-- | pylint/reporters/reports_handler_mix_in.py | 4 | ||||
-rw-r--r-- | pylint/reporters/text.py | 143 | ||||
-rw-r--r-- | pylint/reporters/ureports/__init__.py | 4 | ||||
-rw-r--r-- | pylint/reporters/ureports/base_writer.py | 4 | ||||
-rw-r--r-- | pylint/reporters/ureports/nodes.py | 6 | ||||
-rw-r--r-- | pylint/reporters/ureports/text_writer.py | 4 |
11 files changed, 59 insertions, 147 deletions
diff --git a/pylint/reporters/__init__.py b/pylint/reporters/__init__.py index f22530de1..cf7b57576 100644 --- a/pylint/reporters/__init__.py +++ b/pylint/reporters/__init__.py @@ -1,6 +1,6 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt """Utilities methods and classes for reporters.""" diff --git a/pylint/reporters/base_reporter.py b/pylint/reporters/base_reporter.py index 3df970d80..d370b1910 100644 --- a/pylint/reporters/base_reporter.py +++ b/pylint/reporters/base_reporter.py @@ -1,14 +1,12 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt from __future__ import annotations import os import sys -import warnings from typing import TYPE_CHECKING, TextIO -from warnings import warn from pylint.message import Message from pylint.reporters.ureports.nodes import Text @@ -31,13 +29,6 @@ class BaseReporter: """Name of the reporter.""" def __init__(self, output: TextIO | None = None) -> None: - if getattr(self, "__implements__", None): - warnings.warn( - "Using the __implements__ inheritance pattern for BaseReporter is no " - "longer supported. Child classes should only inherit BaseReporter", - DeprecationWarning, - stacklevel=2, - ) self.linter: PyLinter self.section = 0 self.out: TextIO = output or sys.stdout @@ -49,16 +40,6 @@ class BaseReporter: """Handle a new message triggered on the current file.""" self.messages.append(msg) - def set_output(self, output: TextIO | None = None) -> None: - """Set output stream.""" - # TODO: 3.0: Remove deprecated method - warn( - "'set_output' will be removed in 3.0, please use 'reporter.out = stream' instead", - DeprecationWarning, - stacklevel=2, - ) - self.out = output or sys.stdout - def writeln(self, string: str = "") -> None: """Write a line in the output buffer.""" print(string, file=self.out) diff --git a/pylint/reporters/collecting_reporter.py b/pylint/reporters/collecting_reporter.py index ca9170253..943a74d55 100644 --- a/pylint/reporters/collecting_reporter.py +++ b/pylint/reporters/collecting_reporter.py @@ -1,6 +1,6 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt from __future__ import annotations diff --git a/pylint/reporters/json_reporter.py b/pylint/reporters/json_reporter.py index 29df6ba07..74fa6672b 100644 --- a/pylint/reporters/json_reporter.py +++ b/pylint/reporters/json_reporter.py @@ -1,6 +1,6 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt """JSON reporter.""" @@ -75,7 +75,7 @@ class JSONReporter(BaseJSONReporter): TODO: 3.0: Remove this JSONReporter in favor of the new one handling abs-path and confidence. - TODO: 2.16: Add a new JSONReporter handling abs-path, confidence and scores. + TODO: 3.0: Add a new JSONReporter handling abs-path, confidence and scores. (Ultimately all other breaking change related to json for 3.0). """ diff --git a/pylint/reporters/multi_reporter.py b/pylint/reporters/multi_reporter.py index 8bf0828c4..0c27293b7 100644 --- a/pylint/reporters/multi_reporter.py +++ b/pylint/reporters/multi_reporter.py @@ -1,6 +1,6 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt from __future__ import annotations diff --git a/pylint/reporters/reports_handler_mix_in.py b/pylint/reporters/reports_handler_mix_in.py index 32a7190e7..95d45ba91 100644 --- a/pylint/reporters/reports_handler_mix_in.py +++ b/pylint/reporters/reports_handler_mix_in.py @@ -1,6 +1,6 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt from __future__ import annotations diff --git a/pylint/reporters/text.py b/pylint/reporters/text.py index c25b31f7e..462ea48fe 100644 --- a/pylint/reporters/text.py +++ b/pylint/reporters/text.py @@ -1,6 +1,6 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt """Plain text reporters:. @@ -15,12 +15,11 @@ import re import sys import warnings from dataclasses import asdict, fields -from typing import TYPE_CHECKING, Dict, NamedTuple, Optional, TextIO, cast, overload +from typing import TYPE_CHECKING, Dict, NamedTuple, TextIO from pylint.message import Message from pylint.reporters import BaseReporter from pylint.reporters.ureports.text_writer import TextWriter -from pylint.utils import _splitstrip if TYPE_CHECKING: from pylint.lint import PyLinter @@ -37,6 +36,34 @@ class MessageStyle(NamedTuple): style: tuple[str, ...] = () """Tuple of style strings (see `ANSI_COLORS` for available values).""" + def __get_ansi_code(self) -> str: + """Return ANSI escape code corresponding to color and style. + + :raise KeyError: if a nonexistent color or style identifier is given + + :return: the built escape code + """ + ansi_code = [ANSI_STYLES[effect] for effect in self.style] + if self.color: + if self.color.isdigit(): + ansi_code.extend(["38", "5"]) + ansi_code.append(self.color) + else: + ansi_code.append(ANSI_COLORS[self.color]) + if ansi_code: + return ANSI_PREFIX + ";".join(ansi_code) + ANSI_END + return "" + + def _colorize_ansi(self, msg: str) -> str: + if self.color is None and len(self.style) == 0: + # If both color and style are not defined, then leave the text as is. + return msg + escape_code = self.__get_ansi_code() + # If invalid (or unknown) color, don't wrap msg with ANSI codes + if escape_code: + return f"{escape_code}{msg}{ANSI_RESET}" + return msg + ColorMappingDict = Dict[str, MessageStyle] @@ -70,85 +97,9 @@ MESSAGE_FIELDS = {i.name for i in fields(Message)} """All fields of the Message class.""" -def _get_ansi_code(msg_style: MessageStyle) -> str: - """Return ANSI escape code corresponding to color and style. - - :param msg_style: the message style - - :raise KeyError: if a nonexistent color or style identifier is given - - :return: the built escape code - """ - ansi_code = [ANSI_STYLES[effect] for effect in msg_style.style] - if msg_style.color: - if msg_style.color.isdigit(): - ansi_code.extend(["38", "5"]) - ansi_code.append(msg_style.color) - else: - ansi_code.append(ANSI_COLORS[msg_style.color]) - if ansi_code: - return ANSI_PREFIX + ";".join(ansi_code) + ANSI_END - return "" - - -@overload -def colorize_ansi( - msg: str, - msg_style: MessageStyle | None = ..., -) -> str: - ... - - -@overload -def colorize_ansi( - msg: str, - msg_style: str | None = ..., - style: str = ..., - *, - color: str | None = ..., -) -> str: - # Remove for pylint 3.0 - ... - - -def colorize_ansi( - msg: str, - msg_style: MessageStyle | str | None = None, - style: str = "", - **kwargs: str | None, -) -> str: - r"""colorize message by wrapping it with ANSI escape codes - - :param msg: the message string to colorize - - :param msg_style: the message style - or color (for backwards compatibility): the color of the message style - - :param style: the message's style elements, this will be deprecated - - :param \**kwargs: used to accept `color` parameter while it is being deprecated - - :return: the ANSI escaped string - """ - # TODO: 3.0: Remove deprecated typing and only accept MessageStyle as parameter - if not isinstance(msg_style, MessageStyle): - warnings.warn( - "In pylint 3.0, the colorize_ansi function of Text reporters will only accept a " - "MessageStyle parameter", - DeprecationWarning, - stacklevel=2, - ) - color = kwargs.get("color") - style_attrs = tuple(_splitstrip(style)) - msg_style = MessageStyle(color or msg_style, style_attrs) - # If both color and style are not defined, then leave the text as is - if msg_style.color is None and len(msg_style.style) == 0: - return msg - escape_code = _get_ansi_code(msg_style) - # If invalid (or unknown) color, don't wrap msg with ANSI codes - if escape_code: - return f"{escape_code}{msg}{ANSI_RESET}" - return msg +def colorize_ansi(msg: str, msg_style: MessageStyle) -> str: + """Colorize message by wrapping it with ANSI escape codes.""" + return msg_style._colorize_ansi(msg) def make_header(msg: Message) -> str: @@ -186,7 +137,8 @@ class TextReporter(BaseReporter): if argument[0] not in MESSAGE_FIELDS: warnings.warn( f"Don't recognize the argument '{argument[0]}' in the --msg-template. " - "Are you sure it is supported on the current version of pylint?" + "Are you sure it is supported on the current version of pylint?", + stacklevel=2, ) template = re.sub(r"\{" + argument[0] + r"(:.*?)?\}", "", template) self._fixed_template = template @@ -269,30 +221,9 @@ class ColorizedTextReporter(TextReporter): def __init__( self, output: TextIO | None = None, - color_mapping: ( - ColorMappingDict | dict[str, tuple[str | None, str]] | None - ) = None, + color_mapping: ColorMappingDict | None = None, ) -> None: super().__init__(output) - # TODO: 3.0: Remove deprecated typing and only accept ColorMappingDict as - # color_mapping parameter - if color_mapping and not isinstance( - list(color_mapping.values())[0], MessageStyle - ): - warnings.warn( - "In pylint 3.0, the ColorizedTextReporter will only accept ColorMappingDict as " - "color_mapping parameter", - DeprecationWarning, - stacklevel=2, - ) - temp_color_mapping: ColorMappingDict = {} - for key, value in color_mapping.items(): - color = value[0] - style_attrs = tuple(_splitstrip(value[1])) # type: ignore[arg-type] - temp_color_mapping[key] = MessageStyle(color, style_attrs) - color_mapping = temp_color_mapping - else: - color_mapping = cast(Optional[ColorMappingDict], color_mapping) self.color_mapping = color_mapping or ColorizedTextReporter.COLOR_MAPPING ansi_terms = ["xterm-16color", "xterm-256color"] if os.environ.get("TERM") not in ansi_terms: diff --git a/pylint/reporters/ureports/__init__.py b/pylint/reporters/ureports/__init__.py index a6a0946af..b87c3c319 100644 --- a/pylint/reporters/ureports/__init__.py +++ b/pylint/reporters/ureports/__init__.py @@ -1,6 +1,6 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt __all__ = ("BaseWriter",) diff --git a/pylint/reporters/ureports/base_writer.py b/pylint/reporters/ureports/base_writer.py index e4bd7e710..9a12123cb 100644 --- a/pylint/reporters/ureports/base_writer.py +++ b/pylint/reporters/ureports/base_writer.py @@ -1,6 +1,6 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt """Universal report objects and some formatting drivers. diff --git a/pylint/reporters/ureports/nodes.py b/pylint/reporters/ureports/nodes.py index 8b6bf32c6..59443996d 100644 --- a/pylint/reporters/ureports/nodes.py +++ b/pylint/reporters/ureports/nodes.py @@ -1,6 +1,6 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt """Micro reports objects. @@ -72,7 +72,7 @@ class BaseLayout(VNode): assert self.parent is not self if self.parent is None: return [] - return [self.parent] + self.parent.parents() + return [self.parent, *self.parent.parents()] def add_text(self, text: str) -> None: """Shortcut to add text data.""" diff --git a/pylint/reporters/ureports/text_writer.py b/pylint/reporters/ureports/text_writer.py index 13c675aab..5dd6a5d08 100644 --- a/pylint/reporters/ureports/text_writer.py +++ b/pylint/reporters/ureports/text_writer.py @@ -1,6 +1,6 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt """Text formatting drivers for ureports.""" |