Skip to content

Commit 833762f

Browse files
Simplify testutils
1 parent 83244c0 commit 833762f

File tree

3 files changed

+12
-98
lines changed

3 files changed

+12
-98
lines changed

pylint/testutils/lint_module_test.py

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

1716
import pytest
1817
from _pytest.config import Config
1918

2019
from pylint import checkers
2120
from pylint.config.config_initialization import _config_initialization
22-
from pylint.constants import IS_PYPY
2321
from pylint.lint import PyLinter
2422
from pylint.message.message import Message
2523
from pylint.testutils.constants import _EXPECTED_RE, _OPERATORS, UPDATE_OPTION
@@ -33,7 +31,7 @@
3331
from pylint.testutils.output_line import OutputLine
3432
from pylint.testutils.reporter_for_tests import FunctionalTestReporter
3533

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

3836
PYLINTRC = Path(__file__).parent / "testing_pylintrc"
3937

@@ -105,13 +103,6 @@ def __init__(
105103
self._linter, args_list=args, config_file=rc_file, reporter=_test_reporter
106104
)
107105

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-
115106
self._config = config
116107

117108
def setUp(self) -> None:
@@ -227,8 +218,7 @@ def _get_expected(self) -> tuple[MessageCounter, list[OutputLine]]:
227218
expected_msgs = Counter()
228219
with self._open_expected_file() as f:
229220
expected_output_lines = [
230-
OutputLine.from_csv(row, self._check_end_position)
231-
for row in csv.reader(f, "test")
221+
OutputLine.from_csv(row) for row in csv.reader(f, "test")
232222
]
233223
return expected_msgs, expected_output_lines
234224

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

250238
def _runTest(self) -> None:

pylint/testutils/output_line.py

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

77
from collections.abc import Sequence
8-
from typing import Any, NamedTuple, TypeVar
8+
from typing import Any, NamedTuple
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-
1715

1816
class MessageTest(NamedTuple):
1917
msg_id: str
@@ -41,17 +39,15 @@ class OutputLine(NamedTuple):
4139
confidence: str
4240

4341
@classmethod
44-
def from_msg(cls, msg: Message, check_endline: bool = True) -> OutputLine:
42+
def from_msg(cls, msg: Message) -> OutputLine:
4543
"""Create an OutputLine from a Pylint Message."""
4644
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)
4945
return cls(
5046
msg.symbol,
5147
msg.line,
5248
column,
53-
end_line,
54-
end_column,
49+
msg.end_line,
50+
msg.end_column,
5551
msg.obj or "",
5652
msg.msg.replace("\r\n", "\n"),
5753
msg.confidence.name,
@@ -62,19 +58,8 @@ def _get_column(column: str | int) -> int:
6258
"""Handle column numbers."""
6359
return int(column)
6460

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-
7461
@classmethod
75-
def from_csv(
76-
cls, row: Sequence[str] | str, check_endline: bool = True
77-
) -> OutputLine:
62+
def from_csv(cls, row: Sequence[str] | str) -> OutputLine:
7863
"""Create an OutputLine from a comma separated list (the functional tests
7964
expected output .txt files).
8065
"""
@@ -83,12 +68,8 @@ def from_csv(
8368
try:
8469
line = int(row[1])
8570
column = cls._get_column(row[2])
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-
)
71+
end_line = cls._value_to_optional_int(row[3])
72+
end_column = cls._value_to_optional_int(row[4])
9273
# symbol, line, column, end_line, end_column, node, msg, confidences
9374
assert len(row) == 8
9475
return cls(

tests/testutils/test_output_line.py

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -70,33 +70,13 @@ 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-
9373

9474
@pytest.mark.parametrize("confidence", [HIGH, INFERENCE])
9575
def test_output_line_to_csv(confidence: Confidence, message: _MessageCallable) -> None:
9676
"""Test that the OutputLine NamedTuple is instantiated correctly with from_msg
9777
and then converted to csv.
9878
"""
99-
output_line = OutputLine.from_msg(message(confidence), True)
79+
output_line = OutputLine.from_msg(message(confidence))
10080
csv = output_line.to_csv()
10181
assert csv == (
10282
"missing-docstring",
@@ -109,19 +89,6 @@ def test_output_line_to_csv(confidence: Confidence, message: _MessageCallable) -
10989
confidence.name,
11090
)
11191

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-
12592

12693
def test_output_line_from_csv() -> None:
12794
"""Test that the OutputLine NamedTuple is instantiated correctly with from_csv.
@@ -140,25 +107,3 @@ def test_output_line_from_csv() -> None:
140107
msg="msg",
141108
confidence="HIGH",
142109
)
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)