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

Add typing to text reporter #5041

Merged
merged 9 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion pylint/reporters/base_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BaseReporter:

extension = ""

def __init__(self, output: TextIO = sys.stdout):
def __init__(self, output: Optional[TextIO] = sys.stdout):
cdce8p marked this conversation as resolved.
Show resolved Hide resolved
self.linter: "PyLinter"
self.section = 0
self.out: TextIO = output or sys.stdout
Expand Down
38 changes: 20 additions & 18 deletions pylint/reporters/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import os
import sys
import warnings
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING, Dict, Optional, Set, TextIO, Tuple

from pylint import utils
from pylint.interfaces import IReporter
Expand All @@ -35,8 +35,11 @@
from pylint.reporters.ureports.text_writer import TextWriter

if TYPE_CHECKING:
from pylint.lint import PyLinter
from pylint.reporters.ureports.nodes import Section

ColorMappingDict = Dict[str, Tuple[Optional[str], Optional[str]]]
cdce8p marked this conversation as resolved.
Show resolved Hide resolved
cdce8p marked this conversation as resolved.
Show resolved Hide resolved

TITLE_UNDERLINES = ["", "=", "-", "."]

ANSI_PREFIX = "\033["
Expand Down Expand Up @@ -64,22 +67,19 @@
}


def _get_ansi_code(color=None, style=None):
def _get_ansi_code(color: Optional[str] = None, style: Optional[str] = None) -> str:
"""return ansi escape code corresponding to color and style

:type color: str or None
:param color:
the color name (see `ANSI_COLORS` for available values)
or the color number when 256 colors are available

:type style: str or None
:param style:
style string (see `ANSI_COLORS` for available values). To get
several style effects at the same time, use a coma as separator.

:raise KeyError: if an unexistent color or style identifier is given

:rtype: str
:return: the built escape code
"""
ansi_code = []
cdce8p marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -98,24 +98,22 @@ def _get_ansi_code(color=None, style=None):
return ""


def colorize_ansi(msg, color=None, style=None):
def colorize_ansi(
msg: str, color: Optional[str] = None, style: Optional[str] = None
) -> str:
"""colorize message by wrapping it with ansi escape codes

:type msg: str or unicode
:param msg: the message string to colorize

:type color: str or None
:param color:
the color identifier (see `ANSI_COLORS` for available values)

:type style: str or None
:param style:
style string (see `ANSI_COLORS` for available values). To get
several style effects at the same time, use a coma as separator.

:raise KeyError: if an unexistent color or style identifier is given

:rtype: str or unicode
:return: the ansi escaped string
"""
# If both color and style are not defined, then leave the text as is
Expand All @@ -136,15 +134,15 @@ class TextReporter(BaseReporter):
extension = "txt"
line_format = "{path}:{line}:{column}: {msg_id}: {msg} ({symbol})"

def __init__(self, output=None):
def __init__(self, output: Optional[TextIO] = None) -> None:
BaseReporter.__init__(self, output)
self._modules = set()
self._modules: Set[str] = set()
self._template = self.line_format

def on_set_current_module(self, module: str, filepath: Optional[str]) -> None:
self._template = str(self.linter.config.msg_template or self._template)

def write_message(self, msg):
def write_message(self, msg: Message) -> None:
"""Convenience method to write a formatted message with class default template"""
self.writeln(msg.format(self._template))

Expand Down Expand Up @@ -174,7 +172,7 @@ class ParseableTextReporter(TextReporter):
name = "parseable"
line_format = "{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}"

def __init__(self, output=None):
def __init__(self, output: Optional[TextIO] = None) -> None:
warnings.warn(
f"{self.name} output format is deprecated. This is equivalent to --msg-template={self.line_format}",
DeprecationWarning,
Expand All @@ -193,7 +191,7 @@ class ColorizedTextReporter(TextReporter):
"""Simple TextReporter that colorizes text output"""

name = "colorized"
COLOR_MAPPING = {
COLOR_MAPPING: ColorMappingDict = {
"I": ("green", None),
"C": (None, "bold"),
"R": ("magenta", "bold, italic"),
Expand All @@ -203,7 +201,11 @@ class ColorizedTextReporter(TextReporter):
"S": ("yellow", "inverse"), # S stands for module Separator
}

def __init__(self, output=None, color_mapping=None):
def __init__(
self,
output: Optional[TextIO] = None,
color_mapping: Optional[ColorMappingDict] = None,
cdce8p marked this conversation as resolved.
Show resolved Hide resolved
) -> None:
TextReporter.__init__(self, output)
self.color_mapping = color_mapping or dict(ColorizedTextReporter.COLOR_MAPPING)
cdce8p marked this conversation as resolved.
Show resolved Hide resolved
ansi_terms = ["xterm-16color", "xterm-256color"]
Expand All @@ -214,7 +216,7 @@ def __init__(self, output=None, color_mapping=None):

self.out = colorama.AnsiToWin32(self.out)

def _get_decoration(self, msg_id):
def _get_decoration(self, msg_id: str) -> Tuple[Optional[str], Optional[str]]:
"""Returns the tuple color, style associated with msg_id as defined
in self.color_mapping
"""
Expand Down Expand Up @@ -248,7 +250,7 @@ def handle_message(self, msg: Message) -> None:
self.write_message(msg)


def register(linter):
def register(linter: "PyLinter") -> None:
"""Register the reporter classes with the linter."""
linter.register_reporter(TextReporter)
linter.register_reporter(ParseableTextReporter)
Expand Down