Skip to content

Commit 3092b42

Browse files
Revert "Simplify testutils"
This reverts commit 833762f.
1 parent 38e5481 commit 3092b42

File tree

3 files changed

+98
-12
lines changed

3 files changed

+98
-12
lines changed

pylint/testutils/lint_module_test.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
from collections import Counter
1212
from io import StringIO
1313
from pathlib import Path
14-
from typing import TextIO
14+
from typing import Counter as CounterType
15+
from typing import TextIO, Tuple
1516

1617
import pytest
1718
from _pytest.config import Config
1819

1920
from pylint import checkers
2021
from pylint.config.config_initialization import _config_initialization
22+
from pylint.constants import IS_PYPY
2123
from pylint.lint import PyLinter
2224
from pylint.message.message import Message
2325
from pylint.testutils.constants import _EXPECTED_RE, _OPERATORS, UPDATE_OPTION
@@ -31,7 +33,7 @@
3133
from pylint.testutils.output_line import OutputLine
3234
from pylint.testutils.reporter_for_tests import FunctionalTestReporter
3335

34-
MessageCounter = Counter[tuple[int, str]]
36+
MessageCounter = CounterType[Tuple[int, str]]
3537

3638
PYLINTRC = Path(__file__).parent / "testing_pylintrc"
3739

@@ -103,6 +105,13 @@ def __init__(
103105
self._linter, args_list=args, config_file=rc_file, reporter=_test_reporter
104106
)
105107

108+
self._check_end_position = (
109+
sys.version_info >= self._linter.config.min_pyver_end_position
110+
)
111+
# TODO: PY3.9: PyPy supports end_lineno from 3.9 and above
112+
if self._check_end_position and IS_PYPY:
113+
self._check_end_position = sys.version_info >= (3, 9) # pragma: no cover
114+
106115
self._config = config
107116

108117
def setUp(self) -> None:
@@ -218,7 +227,8 @@ def _get_expected(self) -> tuple[MessageCounter, list[OutputLine]]:
218227
expected_msgs = Counter()
219228
with self._open_expected_file() as f:
220229
expected_output_lines = [
221-
OutputLine.from_csv(row) for row in csv.reader(f, "test")
230+
OutputLine.from_csv(row, self._check_end_position)
231+
for row in csv.reader(f, "test")
222232
]
223233
return expected_msgs, expected_output_lines
224234

@@ -232,7 +242,9 @@ def _get_actual(self) -> tuple[MessageCounter, list[OutputLine]]:
232242
msg.symbol != "fatal"
233243
), f"Pylint analysis failed because of '{msg.msg}'"
234244
received_msgs[msg.line, msg.symbol] += 1
235-
received_output_lines.append(OutputLine.from_msg(msg))
245+
received_output_lines.append(
246+
OutputLine.from_msg(msg, self._check_end_position)
247+
)
236248
return received_msgs, received_output_lines
237249

238250
def _runTest(self) -> None:

pylint/testutils/output_line.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
from __future__ import annotations
66

77
from collections.abc import Sequence
8-
from typing import Any, NamedTuple
8+
from typing import Any, NamedTuple, TypeVar
99

1010
from astroid import nodes
1111

1212
from pylint.interfaces import UNDEFINED, Confidence
1313
from pylint.message.message import Message
1414

15+
_T = TypeVar("_T")
16+
1517

1618
class MessageTest(NamedTuple):
1719
msg_id: str
@@ -39,15 +41,17 @@ class OutputLine(NamedTuple):
3941
confidence: str
4042

4143
@classmethod
42-
def from_msg(cls, msg: Message) -> OutputLine:
44+
def from_msg(cls, msg: Message, check_endline: bool = True) -> OutputLine:
4345
"""Create an OutputLine from a Pylint Message."""
4446
column = cls._get_column(msg.column)
47+
end_line = cls._get_py38_none_value(msg.end_line, check_endline)
48+
end_column = cls._get_py38_none_value(msg.end_column, check_endline)
4549
return cls(
4650
msg.symbol,
4751
msg.line,
4852
column,
49-
msg.end_line,
50-
msg.end_column,
53+
end_line,
54+
end_column,
5155
msg.obj or "",
5256
msg.msg.replace("\r\n", "\n"),
5357
msg.confidence.name,
@@ -58,8 +62,19 @@ def _get_column(column: str | int) -> int:
5862
"""Handle column numbers."""
5963
return int(column)
6064

65+
@staticmethod
66+
def _get_py38_none_value(value: _T, check_endline: bool) -> _T | None:
67+
"""Used to make end_line and end_column None as indicated by our version
68+
compared to `min_pyver_end_position`.
69+
"""
70+
if not check_endline:
71+
return None # pragma: no cover
72+
return value
73+
6174
@classmethod
62-
def from_csv(cls, row: Sequence[str] | str) -> OutputLine:
75+
def from_csv(
76+
cls, row: Sequence[str] | str, check_endline: bool = True
77+
) -> OutputLine:
6378
"""Create an OutputLine from a comma separated list (the functional tests
6479
expected output .txt files).
6580
"""
@@ -68,8 +83,12 @@ def from_csv(cls, row: Sequence[str] | str) -> OutputLine:
6883
try:
6984
line = int(row[1])
7085
column = cls._get_column(row[2])
71-
end_line = cls._value_to_optional_int(row[3])
72-
end_column = cls._value_to_optional_int(row[4])
86+
end_line = cls._value_to_optional_int(
87+
cls._get_py38_none_value(row[3], check_endline)
88+
)
89+
end_column = cls._value_to_optional_int(
90+
cls._get_py38_none_value(row[4], check_endline)
91+
)
7392
# symbol, line, column, end_line, end_column, node, msg, confidences
7493
assert len(row) == 8
7594
return cls(

tests/testutils/test_output_line.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,33 @@ def test_output_line_from_message(message: _MessageCallable) -> None:
7070
assert output_line.msg == "msg"
7171
assert output_line.confidence == "HIGH"
7272

73+
output_line_with_end = OutputLine.from_msg(message(), True)
74+
assert output_line_with_end.symbol == "missing-docstring"
75+
assert output_line_with_end.lineno == 1
76+
assert output_line_with_end.column == 2
77+
assert output_line_with_end.end_lineno == 1
78+
assert output_line_with_end.end_column == 3
79+
assert output_line_with_end.object == "obj"
80+
assert output_line_with_end.msg == "msg"
81+
assert output_line_with_end.confidence == "HIGH"
82+
83+
output_line_without_end = OutputLine.from_msg(message(), False)
84+
assert output_line_without_end.symbol == "missing-docstring"
85+
assert output_line_without_end.lineno == 1
86+
assert output_line_without_end.column == 2
87+
assert output_line_without_end.end_lineno is None
88+
assert output_line_without_end.end_column is None
89+
assert output_line_without_end.object == "obj"
90+
assert output_line_without_end.msg == "msg"
91+
assert output_line_without_end.confidence == "HIGH"
92+
7393

7494
@pytest.mark.parametrize("confidence", [HIGH, INFERENCE])
7595
def test_output_line_to_csv(confidence: Confidence, message: _MessageCallable) -> None:
7696
"""Test that the OutputLine NamedTuple is instantiated correctly with from_msg
7797
and then converted to csv.
7898
"""
79-
output_line = OutputLine.from_msg(message(confidence))
99+
output_line = OutputLine.from_msg(message(confidence), True)
80100
csv = output_line.to_csv()
81101
assert csv == (
82102
"missing-docstring",
@@ -89,6 +109,19 @@ def test_output_line_to_csv(confidence: Confidence, message: _MessageCallable) -
89109
confidence.name,
90110
)
91111

112+
output_line_without_end = OutputLine.from_msg(message(confidence), False)
113+
csv = output_line_without_end.to_csv()
114+
assert csv == (
115+
"missing-docstring",
116+
"1",
117+
"2",
118+
"None",
119+
"None",
120+
"obj",
121+
"msg",
122+
confidence.name,
123+
)
124+
92125

93126
def test_output_line_from_csv() -> None:
94127
"""Test that the OutputLine NamedTuple is instantiated correctly with from_csv.
@@ -107,3 +140,25 @@ def test_output_line_from_csv() -> None:
107140
msg="msg",
108141
confidence="HIGH",
109142
)
143+
output_line_with_end = OutputLine.from_csv(proper_csv, True)
144+
assert output_line_with_end == OutputLine(
145+
symbol="missing-docstring",
146+
lineno=1,
147+
column=2,
148+
end_lineno=1,
149+
end_column=None,
150+
object="obj",
151+
msg="msg",
152+
confidence="HIGH",
153+
)
154+
output_line_without_end = OutputLine.from_csv(proper_csv, False)
155+
assert output_line_without_end == OutputLine(
156+
symbol="missing-docstring",
157+
lineno=1,
158+
column=2,
159+
end_lineno=None,
160+
end_column=None,
161+
object="obj",
162+
msg="msg",
163+
confidence="HIGH",
164+
)

0 commit comments

Comments
 (0)