Skip to content

Commit 6d07d6f

Browse files
DanielNoordPierre-Sassoulas
authored andcommitted
Add typing for json_reporter and sub-classes
1 parent f45ab49 commit 6d07d6f

11 files changed

+76
-33
lines changed

pylint/interfaces.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818

1919
"""Interfaces for Pylint objects"""
2020
from collections import namedtuple
21-
from typing import Tuple
21+
from typing import TYPE_CHECKING, Tuple
2222

2323
from astroid import nodes
2424

25+
if TYPE_CHECKING:
26+
from pylint.reporters.ureports.nodes import Section
27+
2528
Confidence = namedtuple("Confidence", ["name", "description"])
2629
# Warning Certainties
2730
HIGH = Confidence("HIGH", "No false positive possible.")
@@ -99,7 +102,7 @@ class IReporter(Interface):
99102
def handle_message(self, msg) -> None:
100103
"""Handle the given message object."""
101104

102-
def display_reports(self, layout):
105+
def display_reports(self, layout: "Section") -> None:
103106
"""display results encapsulated in the layout tree"""
104107

105108

pylint/reporters/base_reporter.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33

44
import os
55
import sys
6-
from typing import List, Optional
6+
from typing import TYPE_CHECKING, List, Optional
77

88
from pylint.message import Message
9+
from pylint.reporters.ureports.nodes import Text
910
from pylint.typing import CheckerStats
1011

12+
if TYPE_CHECKING:
13+
from pylint.reporters.ureports.nodes import Section
14+
1115

1216
class BaseReporter:
1317
"""base class for reporters
@@ -39,18 +43,21 @@ def writeln(self, string=""):
3943
"""write a line in the output buffer"""
4044
print(string, file=self.out)
4145

42-
def display_reports(self, layout):
46+
def display_reports(self, layout: "Section") -> None:
4347
"""display results encapsulated in the layout tree"""
4448
self.section = 0
4549
if layout.report_id:
46-
layout.children[0].children[0].data += f" ({layout.report_id})"
50+
if isinstance(layout.children[0].children[0], Text):
51+
layout.children[0].children[0].data += f" ({layout.report_id})"
52+
else:
53+
raise ValueError(f"Incorrect child for {layout.children[0].children}")
4754
self._display(layout)
4855

49-
def _display(self, layout):
56+
def _display(self, layout: "Section") -> None:
5057
"""display the layout"""
5158
raise NotImplementedError()
5259

53-
def display_messages(self, layout):
60+
def display_messages(self, layout: Optional["Section"]) -> None:
5461
"""Hook for displaying the messages of the reporter
5562
5663
This will be called whenever the underlying messages

pylint/reporters/collecting_reporter.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
3+
from typing import TYPE_CHECKING
4+
35
from pylint.reporters.base_reporter import BaseReporter
46

7+
if TYPE_CHECKING:
8+
from pylint.reporters.ureports.nodes import Section
9+
510

611
class CollectingReporter(BaseReporter):
712
"""collects messages"""
@@ -15,4 +20,5 @@ def __init__(self) -> None:
1520
def reset(self) -> None:
1621
self.messages = []
1722

18-
_display = None
23+
def _display(self, layout: "Section") -> None:
24+
pass

pylint/reporters/json_reporter.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@
1313

1414
"""JSON reporter"""
1515
import json
16+
from typing import TYPE_CHECKING, Optional
1617

1718
from pylint.interfaces import IReporter
1819
from pylint.reporters.base_reporter import BaseReporter
1920

21+
if TYPE_CHECKING:
22+
from pylint.lint.pylinter import PyLinter
23+
from pylint.reporters.ureports.nodes import Section
24+
2025

2126
class JSONReporter(BaseReporter):
2227
"""Report messages and layouts in JSON."""
@@ -25,7 +30,7 @@ class JSONReporter(BaseReporter):
2530
name = "json"
2631
extension = "json"
2732

28-
def display_messages(self, layout):
33+
def display_messages(self, layout: Optional["Section"]) -> None:
2934
"""Launch layouts display"""
3035
json_dumpable = [
3136
{
@@ -43,13 +48,13 @@ def display_messages(self, layout):
4348
]
4449
print(json.dumps(json_dumpable, indent=4), file=self.out)
4550

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

49-
def _display(self, layout):
54+
def _display(self, layout: "Section") -> None:
5055
"""Do nothing."""
5156

5257

53-
def register(linter):
58+
def register(linter: "PyLinter") -> None:
5459
"""Register the reporter classes with the linter."""
5560
linter.register_reporter(JSONReporter)

pylint/reporters/multi_reporter.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33

44

55
import os
6-
from typing import IO, Any, AnyStr, Callable, List, Optional
6+
from typing import IO, TYPE_CHECKING, Any, AnyStr, Callable, List, Optional
77

88
from pylint.interfaces import IReporter
99
from pylint.message import Message
1010
from pylint.reporters.base_reporter import BaseReporter
11-
from pylint.reporters.ureports.nodes import BaseLayout
1211
from pylint.typing import CheckerStats
1312

13+
if TYPE_CHECKING:
14+
from pylint.reporters.ureports.nodes import Section
15+
1416
AnyFile = IO[AnyStr]
1517
PyLinter = Any
1618

@@ -78,12 +80,12 @@ def writeln(self, string: str = "") -> None:
7880
for rep in self._sub_reporters:
7981
rep.writeln(string)
8082

81-
def display_reports(self, layout: BaseLayout) -> None:
83+
def display_reports(self, layout: "Section") -> None:
8284
"""display results encapsulated in the layout tree"""
8385
for rep in self._sub_reporters:
8486
rep.display_reports(layout)
8587

86-
def display_messages(self, layout: BaseLayout) -> None:
88+
def display_messages(self, layout: Optional["Section"]) -> None:
8789
"""hook for displaying the messages of the reporter"""
8890
for rep in self._sub_reporters:
8991
rep.display_messages(layout)

pylint/reporters/text.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@
2626
import os
2727
import sys
2828
import warnings
29-
from typing import Optional
29+
from typing import TYPE_CHECKING, Optional
3030

3131
from pylint import utils
3232
from pylint.interfaces import IReporter
3333
from pylint.message import Message
3434
from pylint.reporters import BaseReporter
3535
from pylint.reporters.ureports.text_writer import TextWriter
3636

37+
if TYPE_CHECKING:
38+
from pylint.reporters.ureports.nodes import Section
39+
3740
TITLE_UNDERLINES = ["", "=", "-", "."]
3841

3942
ANSI_PREFIX = "\033["
@@ -155,7 +158,7 @@ def handle_message(self, msg: Message) -> None:
155158
self.writeln("************* ")
156159
self.write_message(msg)
157160

158-
def _display(self, layout):
161+
def _display(self, layout: "Section") -> None:
159162
"""launch layouts display"""
160163
print(file=self.out)
161164
TextWriter().format(layout, self.out)

pylint/testutils/reporter_for_tests.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33

44
from io import StringIO
55
from os import getcwd, linesep, sep
6-
from typing import Dict, List, Optional
6+
from typing import TYPE_CHECKING, Dict, List, Optional
77

88
from pylint import interfaces
99
from pylint.message import Message
1010
from pylint.reporters import BaseReporter
1111

12+
if TYPE_CHECKING:
13+
from pylint.reporters.ureports.nodes import Section
14+
1215

1316
class GenericTestReporter(BaseReporter):
1417
"""reporter storing plain text messages"""
@@ -56,25 +59,27 @@ def on_set_current_module(self, module: str, filepath: Optional[str]) -> None:
5659

5760
# pylint: enable=unused-argument
5861

59-
def display_reports(self, layout):
62+
def display_reports(self, layout: "Section") -> None:
6063
"""ignore layouts"""
6164

62-
_display = None
65+
def _display(self, layout: "Section") -> None:
66+
pass
6367

6468

6569
class MinimalTestReporter(BaseReporter):
6670
def on_set_current_module(self, module: str, filepath: Optional[str]) -> None:
6771
self.messages = []
6872

69-
_display = None
73+
def _display(self, layout: "Section") -> None:
74+
pass
7075

7176

7277
class FunctionalTestReporter(BaseReporter):
7378
def on_set_current_module(self, module: str, filepath: Optional[str]) -> None:
7479
self.messages = []
7580

76-
def display_reports(self, layout):
81+
def display_reports(self, layout: "Section") -> None:
7782
"""Ignore layouts and don't call self._display()."""
7883

79-
def _display(self, layout):
84+
def _display(self, layout: "Section") -> None:
8085
pass

tests/extensions/test_broad_try_clause.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@
1414
"""Tests for the pylint checker in :mod:`pylint.extensions.broad_try_clause`"""
1515
import unittest
1616
from os import path as osp
17-
from typing import Optional
17+
from typing import TYPE_CHECKING, Optional
1818

1919
from pylint import checkers
2020
from pylint.extensions.broad_try_clause import BroadTryClauseChecker
2121
from pylint.lint import PyLinter
2222
from pylint.reporters import BaseReporter
2323

24+
if TYPE_CHECKING:
25+
from pylint.reporters.ureports.nodes import Section
26+
2427

2528
class BroadTryClauseTestReporter(BaseReporter):
2629
def on_set_current_module(self, module: str, filepath: Optional[str]) -> None:
2730
self.messages = []
2831

29-
def _display(self, layout):
32+
def _display(self, layout: "Section") -> None:
3033
pass
3134

3235

tests/extensions/test_comparetozero.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,22 @@
1515

1616
import os
1717
import unittest
18-
from typing import Optional
18+
from typing import TYPE_CHECKING, Optional
1919

2020
from pylint import checkers
2121
from pylint.extensions.comparetozero import CompareToZeroChecker
2222
from pylint.lint import PyLinter
2323
from pylint.reporters import BaseReporter
2424

25+
if TYPE_CHECKING:
26+
from pylint.reporters.ureports.nodes import Section
27+
2528

2629
class CompareToZeroTestReporter(BaseReporter):
2730
def on_set_current_module(self, module: str, filepath: Optional[str]) -> None:
2831
self.messages = []
2932

30-
def _display(self, layout):
33+
def _display(self, layout: "Section") -> None:
3134
pass
3235

3336

tests/test_self.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
from io import StringIO
5252
from os.path import abspath, dirname, join
5353
from pathlib import Path
54-
from typing import Any, Generator, Iterator, List, Optional, Union
54+
from typing import TYPE_CHECKING, Any, Generator, Iterator, List, Optional, Union
5555
from unittest import mock
5656
from unittest.mock import patch
5757

@@ -66,9 +66,11 @@
6666
from pylint.message import Message
6767
from pylint.reporters import JSONReporter
6868
from pylint.reporters.text import BaseReporter, ColorizedTextReporter, TextReporter
69-
from pylint.reporters.ureports.nodes import EvaluationSection
7069
from pylint.utils import utils
7170

71+
if TYPE_CHECKING:
72+
from pylint.reporters.ureports.nodes import Section
73+
7274
HERE = abspath(dirname(__file__))
7375
CLEAN_PATH = re.escape(dirname(dirname(__file__)) + os.path.sep)
7476
UNNECESSARY_LAMBDA = join(
@@ -116,7 +118,7 @@ def handle_message(self, msg: Message) -> None:
116118
for rep in self._reporters:
117119
rep.handle_message(msg)
118120

119-
def _display(self, layout: EvaluationSection) -> None:
121+
def _display(self, layout: "Section") -> None:
120122
pass
121123

122124
@property

tests/unittest_reporting.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from contextlib import redirect_stdout
2222
from io import StringIO
2323
from json import dumps
24+
from typing import TYPE_CHECKING
2425

2526
import pytest
2627

@@ -31,6 +32,9 @@
3132
from pylint.reporters.text import ParseableTextReporter, TextReporter
3233
from pylint.typing import FileItem
3334

35+
if TYPE_CHECKING:
36+
from pylint.reporters.ureports.nodes import Section
37+
3438

3539
@pytest.fixture(scope="module")
3640
def reporter():
@@ -93,7 +97,7 @@ def __init__(self, output=None):
9397
def writeln(self, string=""):
9498
pass
9599

96-
def _display(self, layout):
100+
def _display(self, layout: "Section") -> None:
97101
pass
98102

99103

@@ -257,7 +261,7 @@ def test_multi_format_output(tmp_path):
257261

258262
def test_display_results_is_renamed():
259263
class CustomReporter(TextReporter):
260-
def _display(self, layout):
264+
def _display(self, layout: "Section") -> None:
261265
return None
262266

263267
reporter = CustomReporter()

0 commit comments

Comments
 (0)