2020-11-29 09:31:01 +00:00
|
|
|
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING
|
|
|
|
|
|
|
|
import collections
|
|
|
|
|
|
|
|
from pylint import interfaces
|
2021-01-23 13:13:33 +00:00
|
|
|
from pylint.constants import PY38_PLUS
|
2021-01-17 15:41:54 +00:00
|
|
|
from pylint.testutils.constants import UPDATE_OPTION
|
2020-11-29 09:31:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Message(
|
|
|
|
collections.namedtuple("Message", ["msg_id", "line", "node", "args", "confidence"])
|
|
|
|
):
|
|
|
|
def __new__(cls, msg_id, line=None, node=None, args=None, confidence=None):
|
|
|
|
return tuple.__new__(cls, (msg_id, line, node, args, confidence))
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if isinstance(other, Message):
|
|
|
|
if self.confidence and other.confidence:
|
|
|
|
return super().__eq__(other)
|
|
|
|
return self[:-1] == other[:-1]
|
|
|
|
return NotImplemented # pragma: no cover
|
|
|
|
|
2021-02-06 06:55:55 +00:00
|
|
|
def __hash__(self):
|
|
|
|
return None
|
2020-11-29 09:31:01 +00:00
|
|
|
|
|
|
|
|
2020-11-29 14:20:20 +00:00
|
|
|
class MalformedOutputLineException(Exception):
|
|
|
|
def __init__(self, row, exception):
|
2021-01-03 22:01:08 +00:00
|
|
|
example = "msg-symbolic-name:42:27:MyClass.my_function:The message"
|
|
|
|
other_example = "msg-symbolic-name:7:42::The message"
|
2021-01-17 15:41:54 +00:00
|
|
|
expected = [
|
|
|
|
"symbol",
|
|
|
|
"line",
|
|
|
|
"column",
|
|
|
|
"MyClass.myFunction, (or '')",
|
|
|
|
"Message",
|
|
|
|
"confidence",
|
|
|
|
]
|
|
|
|
reconstructed_row = ""
|
|
|
|
i = 0
|
|
|
|
for i, column in enumerate(row):
|
|
|
|
reconstructed_row += "\t{}='{}' ?\n".format(expected[i], column)
|
|
|
|
for missing in expected[i + 1 :]:
|
|
|
|
reconstructed_row += "\t{}= Nothing provided !\n".format(missing)
|
|
|
|
msg = """\
|
|
|
|
{exception}
|
|
|
|
|
|
|
|
Expected '{example}' or '{other_example}' but we got '{raw}':
|
|
|
|
{reconstructed_row}
|
|
|
|
|
|
|
|
Try updating it with: 'python tests/test_functional.py {update_option}'""".format(
|
|
|
|
exception=exception,
|
2020-11-29 14:20:20 +00:00
|
|
|
example=example,
|
|
|
|
other_example=other_example,
|
2021-01-17 15:41:54 +00:00
|
|
|
raw=":".join(row),
|
2020-11-29 14:20:20 +00:00
|
|
|
reconstructed_row=reconstructed_row,
|
2021-01-17 15:41:54 +00:00
|
|
|
update_option=UPDATE_OPTION,
|
2020-11-29 14:20:20 +00:00
|
|
|
)
|
2021-01-17 15:41:54 +00:00
|
|
|
Exception.__init__(self, msg)
|
2020-11-29 14:20:20 +00:00
|
|
|
|
|
|
|
|
2020-11-29 09:31:01 +00:00
|
|
|
class OutputLine(
|
|
|
|
collections.namedtuple(
|
2021-01-03 22:01:08 +00:00
|
|
|
"OutputLine", ["symbol", "lineno", "column", "object", "msg", "confidence"]
|
2020-11-29 09:31:01 +00:00
|
|
|
)
|
|
|
|
):
|
|
|
|
@classmethod
|
|
|
|
def from_msg(cls, msg):
|
2021-01-04 17:32:42 +00:00
|
|
|
column = cls.get_column(msg.column)
|
2020-11-29 09:31:01 +00:00
|
|
|
return cls(
|
|
|
|
msg.symbol,
|
|
|
|
msg.line,
|
2021-01-04 17:32:42 +00:00
|
|
|
column,
|
2020-11-29 09:31:01 +00:00
|
|
|
msg.obj or "",
|
|
|
|
msg.msg.replace("\r\n", "\n"),
|
|
|
|
msg.confidence.name
|
|
|
|
if msg.confidence != interfaces.UNDEFINED
|
|
|
|
else interfaces.HIGH.name,
|
|
|
|
)
|
|
|
|
|
2021-01-04 17:32:42 +00:00
|
|
|
@classmethod
|
|
|
|
def get_column(cls, column):
|
2021-01-23 13:13:33 +00:00
|
|
|
if not PY38_PLUS:
|
2021-01-04 17:32:42 +00:00
|
|
|
return ""
|
|
|
|
return str(column)
|
|
|
|
|
2020-11-29 09:31:01 +00:00
|
|
|
@classmethod
|
|
|
|
def from_csv(cls, row):
|
2020-11-29 14:20:20 +00:00
|
|
|
try:
|
2021-01-03 22:01:08 +00:00
|
|
|
confidence = row[5] if len(row) == 6 else interfaces.HIGH.name
|
2021-01-04 17:32:42 +00:00
|
|
|
column = cls.get_column(row[2])
|
|
|
|
return cls(row[0], int(row[1]), column, row[3], row[4], confidence)
|
2020-11-29 14:20:20 +00:00
|
|
|
except Exception as e:
|
|
|
|
raise MalformedOutputLineException(row, e) from e
|
2020-11-29 09:31:01 +00:00
|
|
|
|
|
|
|
def to_csv(self):
|
|
|
|
if self.confidence == interfaces.HIGH.name:
|
|
|
|
return self[:-1]
|
|
|
|
return self
|