diff options
author | Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com> | 2021-09-21 13:56:58 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-21 13:56:58 +0200 |
commit | 1e675abcc2aa931421d7ce300908e734a93fd790 (patch) | |
tree | 9588163cd0c52fe1ffb18f0383d18cf4e0cc6287 /pylint/message | |
parent | 585c6ca9016613a5c7138ad8cd704a91b363d537 (diff) | |
download | pylint-git-1e675abcc2aa931421d7ce300908e734a93fd790.tar.gz |
Type `add_message` and add `MessageLocationTuple` (#5050)
* Type `add_message` and add `MessageLocationTuple`
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Diffstat (limited to 'pylint/message')
-rw-r--r-- | pylint/message/message.py | 41 | ||||
-rw-r--r-- | pylint/message/message_handler_mix_in.py | 46 |
2 files changed, 72 insertions, 15 deletions
diff --git a/pylint/message/message.py b/pylint/message/message.py index df0e7079e..178079223 100644 --- a/pylint/message/message.py +++ b/pylint/message/message.py @@ -3,8 +3,12 @@ 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", @@ -28,7 +32,42 @@ _MsgBase = collections.namedtuple( class Message(_MsgBase): """This class represent a message to be issued by the reporters""" - def __new__(cls, msg_id, symbol, location, msg, confidence): + @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, + ) return _MsgBase.__new__( cls, msg_id, diff --git a/pylint/message/message_handler_mix_in.py b/pylint/message/message_handler_mix_in.py index 6d24131a7..02e193cfa 100644 --- a/pylint/message/message_handler_mix_in.py +++ b/pylint/message/message_handler_mix_in.py @@ -3,7 +3,9 @@ import sys from io import TextIOWrapper -from typing import List, TextIO, Tuple, Union +from typing import TYPE_CHECKING, Any, List, Optional, TextIO, Tuple, Union + +from astroid import nodes from pylint.constants import ( _SCOPE_EXEMPT, @@ -21,10 +23,14 @@ from pylint.exceptions import ( NoLineSuppliedError, UnknownMessageError, ) -from pylint.interfaces import UNDEFINED +from pylint.interfaces import UNDEFINED, Confidence from pylint.message.message import Message from pylint.utils import get_module_and_frameid, get_rst_section, get_rst_title +if TYPE_CHECKING: + from pylint.lint.pylinter import PyLinter + from pylint.message import MessageDefinition + class MessagesHandlerMixIn: """A mix-in class containing all the messages related methods for the main lint class.""" @@ -226,9 +232,15 @@ class MessagesHandlerMixIn: return self._msgs_state.get(msgid, fallback) return self._msgs_state.get(msgid, True) - def add_message( - self, msgid, line=None, node=None, args=None, confidence=None, col_offset=None - ): + def add_message( # type: ignore # MessagesHandlerMixIn is always mixed with PyLinter + self: "PyLinter", + msgid: str, + line: Optional[int] = None, + node: Optional[nodes.NodeNG] = None, + args: Any = None, + confidence: Optional[Confidence] = None, + col_offset: Optional[int] = None, + ) -> None: """Adds a message given by ID or name. If provided, the message string is expanded using args. @@ -267,14 +279,20 @@ class MessagesHandlerMixIn: f"Message {message_definition.msgid} must provide Node, got None" ) - def add_one_message( - self, message_definition, line, node, args, confidence, col_offset - ): + def add_one_message( # type: ignore # MessagesHandlerMixIn is always mixed with PyLinter + self: "PyLinter", + message_definition: "MessageDefinition", + line: Optional[int], + node: Optional[nodes.NodeNG], + args: Any, + confidence: Optional[Confidence], + col_offset: Optional[int], + ) -> None: self.check_message_definition(message_definition, line, node) if line is None and node is not None: line = node.fromlineno if col_offset is None and hasattr(node, "col_offset"): - col_offset = node.col_offset + col_offset = node.col_offset # type: ignore # should this message be displayed if not self.is_message_enabled(message_definition.msgid, line, confidence): @@ -303,13 +321,13 @@ class MessagesHandlerMixIn: "by_module": {self.current_name: {msg_cat: 0}}, "by_msg": {}, } - self.stats[msg_cat] += 1 - self.stats["by_module"][self.current_name][msg_cat] += 1 + self.stats[msg_cat] += 1 # type: ignore + self.stats["by_module"][self.current_name][msg_cat] += 1 # type: ignore try: - self.stats["by_msg"][message_definition.symbol] += 1 + self.stats["by_msg"][message_definition.symbol] += 1 # type: ignore except KeyError: - self.stats["by_msg"][message_definition.symbol] = 1 - # expand message ? + self.stats["by_msg"][message_definition.symbol] = 1 # type: ignore + # Interpolate arguments into message string msg = message_definition.msg if args: msg %= args |