Skip to content

Commit

Permalink
[deprecation] Make 'OutputLine' require a fixed number of args
Browse files Browse the repository at this point in the history
Co-authored-by: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
  • Loading branch information
Pierre-Sassoulas and DanielNoord committed Mar 22, 2023
1 parent 25406f7 commit aa10d07
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 128 deletions.
5 changes: 5 additions & 0 deletions doc/whatsnew/fragments/8474.internal
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Following a deprecation period, the ``OutputLine`` class now requires
the right number of argument all the time. The functional output can be
regenerated automatically to achieve that easily.

Refs #8474
68 changes: 13 additions & 55 deletions pylint/testutils/output_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from __future__ import annotations

import warnings
from collections.abc import Sequence
from typing import Any, NamedTuple, TypeVar

Expand Down Expand Up @@ -89,63 +88,22 @@ def from_csv(
"""
if isinstance(row, str):
row = row.split(",")
# noinspection PyBroadException
# pylint: disable = too-many-try-statements
try:
line = int(row[1])
column = cls._get_column(row[2])
if len(row) == 5:
# TODO: 3.0
warnings.warn(
"In pylint 3.0 functional tests expected output should always include the "
"expected confidence level, expected end_line and expected end_column. "
"An OutputLine should thus have a length of 8.",
DeprecationWarning,
stacklevel=2,
)
return cls(
row[0],
int(row[1]),
column,
None,
None,
row[3],
row[4],
UNDEFINED.name,
)
if len(row) == 6:
# TODO: 3.0
warnings.warn(
"In pylint 3.0 functional tests expected output should always include the "
"expected end_line and expected end_column. An OutputLine should thus have "
"a length of 8.",
DeprecationWarning,
stacklevel=2,
)
return cls(
row[0], int(row[1]), column, None, None, row[3], row[4], row[5]
)
if len(row) == 8:
end_line = cls._get_py38_none_value(row[3], check_endline)
end_column = cls._get_py38_none_value(row[4], check_endline)
return cls(
row[0],
int(row[1]),
column,
cls._value_to_optional_int(end_line),
cls._value_to_optional_int(end_column),
row[5],
row[6],
row[7],
)
raise IndexError
except Exception: # pylint: disable=broad-except
warnings.warn(
"Expected 'msg-symbolic-name:42:27:MyClass.my_function:The message:"
f"CONFIDENCE' but we got '{':'.join(row)}'. Try updating the expected"
f" output with:\npython tests/test_functional.py {UPDATE_OPTION}",
UserWarning,
stacklevel=2,
end_line = cls._value_to_optional_int(
cls._get_py38_none_value(row[3], check_endline)
)
end_column = cls._value_to_optional_int(
cls._get_py38_none_value(row[4], check_endline)
)
# symbol, line, column, end_line, end_column, node, msg, confidences
assert len(row) == 8
return cls(
row[0], line, column, end_line, end_column, row[5], row[6], row[7]
)
except Exception: # pylint: disable=broad-except
# We need this to not fail for the update script to work.
return cls("", 0, 0, None, None, "", "", "")

def to_csv(self) -> tuple[str, str, str, str, str, str, str, str]:
Expand Down
74 changes: 1 addition & 73 deletions tests/testutils/test_output_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,83 +133,11 @@ def test_output_line_to_csv(confidence: Confidence, message: _MessageCallable) -
confidence.name,
)


def test_output_line_from_csv_error() -> None:
"""Test that errors are correctly raised for incorrect OutputLine's."""
# Test a csv-string which does not have a number for line and column
with pytest.warns(
UserWarning,
match="msg-symbolic-name:42:27:MyClass.my_function:The message",
):
OutputLine.from_csv("'missing-docstring', 'line', 'column', 'obj', 'msg'", True)
# Test a tuple which does not have a number for line and column
with pytest.warns(
UserWarning, match="we got 'missing-docstring:line:column:obj:msg'"
):
csv = ("missing-docstring", "line", "column", "obj", "msg")
OutputLine.from_csv(csv, True)
# Test a csv-string that is too long
with pytest.warns(
UserWarning,
match="msg-symbolic-name:42:27:MyClass.my_function:The message",
):
OutputLine.from_csv(
"'missing-docstring', 1, 2, 'obj', 'msg', 'func', 'message', 'conf', 'too_long'",
True,
)


@pytest.mark.parametrize(
"confidence,expected_confidence", [[None, "UNDEFINED"], ["INFERENCE", "INFERENCE"]]
)
def test_output_line_from_csv_deprecated(
confidence: str | None, expected_confidence: str
) -> None:
"""Test that the OutputLine NamedTuple is instantiated correctly with from_csv.
Test OutputLine's of length 5 or 6.
"""
if confidence:
proper_csv = [
"missing-docstring",
"1",
"2",
"obj",
"msg",
confidence,
]
else:
proper_csv = ["missing-docstring", "1", "2", "obj", "msg"]
with pytest.warns(DeprecationWarning) as records:
output_line = OutputLine.from_csv(proper_csv, True)
assert len(records) == 1

expected_column = 2 if PY38_PLUS else 0
assert output_line == OutputLine(
symbol="missing-docstring",
lineno=1,
column=expected_column,
end_lineno=None,
end_column=None,
object="obj",
msg="msg",
confidence=expected_confidence,
)


def test_output_line_from_csv() -> None:
"""Test that the OutputLine NamedTuple is instantiated correctly with from_csv.
Test OutputLine of length 8.
"""
proper_csv = [
"missing-docstring",
"1",
"2",
"1",
"None",
"obj",
"msg",
"HIGH",
]
proper_csv = ["missing-docstring", "1", "2", "1", "None", "obj", "msg", "HIGH"]
expected_column = 2 if PY38_PLUS else 0

output_line = OutputLine.from_csv(proper_csv)
Expand Down

0 comments on commit aa10d07

Please sign in to comment.