summaryrefslogtreecommitdiff
path: root/pylint/message/message.py
blob: b431d502b7af6f5c56873ff6cf1b315b6cf9359a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE


import collections
from typing import Optional, Tuple, Union, overload
from warnings import warn

from pylint.constants import MSG_TYPES
from pylint.interfaces import 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):
    """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: Optional[Confidence],
    ) -> "Message":
        ...

    @overload
    def __new__(
        cls,
        msg_id: str,
        symbol: str,
        location: Tuple[str, str, str, str, int, int],
        msg: str,
        confidence: Optional[Confidence],
    ) -> "Message":
        # Remove for pylint 3.0
        ...

    def __new__(
        cls,
        msg_id: str,
        symbol: str,
        location: Union[
            Tuple[str, str, str, str, int, int],
            MessageLocationTuple,
        ],
        msg: str,
        confidence: Optional[Confidence],
    ) -> "Message":
        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
        )

    def format(self, template: str) -> str:
        """Format the message according to the given template.

        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())