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/reporters directory #6305

Merged
merged 1 commit 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
5 changes: 4 additions & 1 deletion pylint/reporters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

"""Utilities methods and classes for reporters."""

from __future__ import annotations

from typing import TYPE_CHECKING

from pylint import utils
Expand All @@ -16,7 +19,7 @@
from pylint.lint.pylinter import PyLinter


def initialize(linter: "PyLinter") -> None:
def initialize(linter: PyLinter) -> None:
"""Initialize linter with reporters in this package."""
utils.register_plugins(linter, __path__[0])

Expand Down
20 changes: 11 additions & 9 deletions pylint/reporters/base_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

from __future__ import annotations

import os
import sys
from typing import TYPE_CHECKING, List, Optional, TextIO
from typing import TYPE_CHECKING, TextIO
from warnings import warn

from pylint.message import Message
Expand All @@ -27,19 +29,19 @@ class BaseReporter:
name = "base"
"""Name of the reporter."""

def __init__(self, output: Optional[TextIO] = None) -> None:
self.linter: "PyLinter"
def __init__(self, output: TextIO | None = None) -> None:
self.linter: PyLinter
self.section = 0
self.out: TextIO = output or sys.stdout
self.messages: List[Message] = []
self.messages: list[Message] = []
# Build the path prefix to strip to get relative paths
self.path_strip_prefix = os.getcwd() + os.sep

def handle_message(self, msg: Message) -> None:
"""Handle a new message triggered on the current file."""
self.messages.append(msg)

def set_output(self, output: Optional[TextIO] = None) -> None:
def set_output(self, output: TextIO | None = None) -> None:
"""Set output stream."""
# pylint: disable-next=fixme
# TODO: Remove this method after depreciation
Expand All @@ -53,7 +55,7 @@ def writeln(self, string: str = "") -> None:
"""Write a line in the output buffer."""
print(string, file=self.out)

def display_reports(self, layout: "Section") -> None:
def display_reports(self, layout: Section) -> None:
"""Display results encapsulated in the layout tree."""
self.section = 0
if layout.report_id:
Expand All @@ -63,11 +65,11 @@ def display_reports(self, layout: "Section") -> None:
raise ValueError(f"Incorrect child for {layout.children[0].children}")
self._display(layout)

def _display(self, layout: "Section") -> None:
def _display(self, layout: Section) -> None:
"""Display the layout."""
raise NotImplementedError()

def display_messages(self, layout: Optional["Section"]) -> None:
def display_messages(self, layout: Section | None) -> None:
"""Hook for displaying the messages of the reporter.

This will be called whenever the underlying messages
Expand All @@ -80,7 +82,7 @@ def display_messages(self, layout: Optional["Section"]) -> None:

# Event callbacks

def on_set_current_module(self, module: str, filepath: Optional[str]) -> None:
def on_set_current_module(self, module: str, filepath: str | None) -> None:
"""Hook called when a module starts to be analysed."""

def on_close(
Expand Down
4 changes: 3 additions & 1 deletion pylint/reporters/collecting_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

from __future__ import annotations

from typing import TYPE_CHECKING

from pylint.reporters.base_reporter import BaseReporter
Expand All @@ -22,5 +24,5 @@ def __init__(self) -> None:
def reset(self) -> None:
self.messages = []

def _display(self, layout: "Section") -> None:
def _display(self, layout: Section) -> None:
pass
13 changes: 8 additions & 5 deletions pylint/reporters/json_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

"""JSON reporter."""

from __future__ import annotations

import json
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING

from pylint.interfaces import IReporter
from pylint.reporters.base_reporter import BaseReporter
Expand All @@ -21,7 +24,7 @@ class JSONReporter(BaseReporter):
name = "json"
extension = "json"

def display_messages(self, layout: Optional["Section"]) -> None:
def display_messages(self, layout: Section | None) -> None:
"""Launch layouts display."""
json_dumpable = [
{
Expand All @@ -41,12 +44,12 @@ def display_messages(self, layout: Optional["Section"]) -> None:
]
print(json.dumps(json_dumpable, indent=4), file=self.out)

def display_reports(self, layout: "Section") -> None:
def display_reports(self, layout: Section) -> None:
"""Don't do anything in this reporter."""

def _display(self, layout: "Section") -> None:
def _display(self, layout: Section) -> None:
"""Do nothing."""


def register(linter: "PyLinter") -> None:
def register(linter: PyLinter) -> None:
linter.register_reporter(JSONReporter)
26 changes: 14 additions & 12 deletions pylint/reporters/multi_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

from __future__ import annotations

import os
from typing import TYPE_CHECKING, Callable, List, Optional, TextIO
from typing import TYPE_CHECKING, Callable, TextIO

from pylint.interfaces import IReporter
from pylint.message import Message
Expand All @@ -30,23 +32,23 @@ class MultiReporter:

def __init__(
self,
sub_reporters: List[BaseReporter],
sub_reporters: list[BaseReporter],
close_output_files: Callable[[], None],
output: Optional[TextIO] = None,
output: TextIO | None = None,
):
self._sub_reporters = sub_reporters
self.close_output_files = close_output_files
self._path_strip_prefix = os.getcwd() + os.sep
self._linter: Optional["PyLinter"] = None
self._linter: PyLinter | None = None
self.out = output
self.messages: List[Message] = []
self.messages: list[Message] = []

@property
def out(self) -> Optional[TextIO]:
def out(self) -> TextIO | None:
return self.__out

@out.setter
def out(self, output: Optional[TextIO] = None) -> None:
def out(self, output: TextIO | None = None) -> None:
"""MultiReporter doesn't have its own output.

This method is only provided for API parity with BaseReporter
Expand All @@ -64,11 +66,11 @@ def path_strip_prefix(self) -> str:
return self._path_strip_prefix

@property
def linter(self) -> Optional["PyLinter"]:
def linter(self) -> PyLinter | None:
return self._linter

@linter.setter
def linter(self, value: "PyLinter") -> None:
def linter(self, value: PyLinter) -> None:
self._linter = value
for rep in self._sub_reporters:
rep.linter = value
Expand All @@ -83,17 +85,17 @@ def writeln(self, string: str = "") -> None:
for rep in self._sub_reporters:
rep.writeln(string)

def display_reports(self, layout: "Section") -> None:
def display_reports(self, layout: Section) -> None:
"""Display results encapsulated in the layout tree."""
for rep in self._sub_reporters:
rep.display_reports(layout)

def display_messages(self, layout: Optional["Section"]) -> None:
def display_messages(self, layout: Section | None) -> None:
"""Hook for displaying the messages of the reporter."""
for rep in self._sub_reporters:
rep.display_messages(layout)

def on_set_current_module(self, module: str, filepath: Optional[str]) -> None:
def on_set_current_module(self, module: str, filepath: str | None) -> None:
"""Hook called when a module starts to be analysed."""
for rep in self._sub_reporters:
rep.on_set_current_module(module, filepath)
Expand Down
13 changes: 7 additions & 6 deletions pylint/reporters/reports_handler_mix_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

from __future__ import annotations

import collections
from typing import (
TYPE_CHECKING,
Callable,
DefaultDict,
Dict,
List,
MutableSequence,
Optional,
Expand All @@ -33,14 +34,14 @@ class ReportsHandlerMixIn:

def __init__(self) -> None:
self._reports: ReportsDict = collections.defaultdict(list)
self._reports_state: Dict[str, bool] = {}
self._reports_state: dict[str, bool] = {}

def report_order(self) -> MutableSequence["BaseChecker"]:
def report_order(self) -> MutableSequence[BaseChecker]:
"""Return a list of reporters."""
return list(self._reports)

def register_report(
self, reportid: str, r_title: str, r_cb: ReportsCallable, checker: "BaseChecker"
self, reportid: str, r_title: str, r_cb: ReportsCallable, checker: BaseChecker
) -> None:
"""Register a report.

Expand All @@ -67,9 +68,9 @@ def report_is_enabled(self, reportid: str) -> bool:
return self._reports_state.get(reportid, True)

def make_reports( # type: ignore[misc] # ReportsHandlerMixIn is always mixed with PyLinter
self: "PyLinter",
self: PyLinter,
stats: LinterStats,
old_stats: Optional[LinterStats],
old_stats: LinterStats | None,
) -> Section:
"""Render registered reports."""
sect = Section("Report", f"{self.stats.statement} statements analysed.")
Expand Down
Loading