summaryrefslogtreecommitdiff
path: root/pylint/message
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2022-04-18 17:36:07 +0200
committerGitHub <noreply@github.com>2022-04-18 17:36:07 +0200
commite5dfec0127e342eac8bdb46b1ecd8483b39c6049 (patch)
treea06e91a10482a0b1842dba5bd2180e986ee0dae9 /pylint/message
parentaf4bcf86a24465690197f487f062596f3bf25eba (diff)
downloadpylint-git-e5dfec0127e342eac8bdb46b1ecd8483b39c6049.tar.gz
Make ``Message`` a ``dataclass`` (#6381)
Diffstat (limited to 'pylint/message')
-rw-r--r--pylint/message/message.py108
1 files changed, 47 insertions, 61 deletions
diff --git a/pylint/message/message.py b/pylint/message/message.py
index 67f787572..4efa3f124 100644
--- a/pylint/message/message.py
+++ b/pylint/message/message.py
@@ -4,85 +4,71 @@
from __future__ import annotations
-import collections
-from typing import overload
+from dataclasses import asdict, dataclass
from warnings import warn
from pylint.constants import MSG_TYPES
-from pylint.interfaces import Confidence
+from pylint.interfaces import UNDEFINED, Confidence
from pylint.typing import MessageLocationTuple
-_MsgBase = collections.namedtuple(
- "_MsgBase",
- [
- "msg_id",
- "symbol",
- "msg",
- "C",
- "category",
- "confidence",
- "abspath",
- "path",
- "module",
- "obj",
- "line",
- "column",
- "end_line",
- "end_column",
- ],
-)
-
-class Message(_MsgBase):
+@dataclass(unsafe_hash=True)
+class Message: # pylint: disable=too-many-instance-attributes
"""This class represent a message to be issued by the reporters."""
- @overload
- def __new__(
- cls,
- msg_id: str,
- symbol: str,
- location: MessageLocationTuple,
- msg: str,
- confidence: Confidence | None,
- ) -> Message:
- ...
-
- @overload
- def __new__(
- cls,
- msg_id: str,
- symbol: str,
- location: tuple[str, str, str, str, int, int],
- msg: str,
- confidence: Confidence | None,
- ) -> Message:
- # Remove for pylint 3.0
- ...
+ msg_id: str
+ symbol: str
+ msg: str
+ C: str
+ category: str
+ confidence: Confidence
+ abspath: str
+ path: str
+ module: str
+ obj: str
+ line: int
+ column: int
+ end_line: int | None
+ end_column: int | None
- def __new__(
- cls,
+ def __init__(
+ self,
msg_id: str,
symbol: str,
location: tuple[str, str, str, str, int, int] | MessageLocationTuple,
msg: str,
confidence: Confidence | None,
- ) -> Message:
+ ) -> None:
if not isinstance(location, MessageLocationTuple):
warn(
"In pylint 3.0, Messages will only accept a MessageLocationTuple as location parameter",
DeprecationWarning,
)
- location = location + (None, None) # type: ignore[assignment] # Temporary fix until deprecation
- return _MsgBase.__new__(
- cls,
- msg_id,
- symbol,
- msg,
- msg_id[0],
- MSG_TYPES[msg_id[0]],
- confidence,
- *location,
- )
+ location = MessageLocationTuple(
+ location[0],
+ location[1],
+ location[2],
+ location[3],
+ location[4],
+ location[5],
+ None,
+ None,
+ )
+
+ self.msg_id = msg_id
+ self.symbol = symbol
+ self.msg = msg
+ self.C = msg_id[0]
+ self.category = MSG_TYPES[msg_id[0]]
+ self.confidence = confidence or UNDEFINED
+ self.abspath = location.abspath
+ self.path = location.path
+ self.module = location.module
+ self.obj = location.obj
+ self.line = location.line
+ self.column = location.column
+ self.end_line = location.end_line
+ self.end_column = location.end_column
def format(self, template: str) -> str:
"""Format the message according to the given template.
@@ -90,4 +76,4 @@ class Message(_MsgBase):
The template format is the one of the format method :
cf. https://docs.python.org/2/library/string.html#formatstrings
"""
- return template.format(**self._asdict())
+ return template.format(**asdict(self))