diff options
Diffstat (limited to 'pylint/message')
-rw-r--r-- | pylint/message/message.py | 2 | ||||
-rw-r--r-- | pylint/message/message_definition.py | 37 | ||||
-rw-r--r-- | pylint/message/message_definition_store.py | 39 | ||||
-rw-r--r-- | pylint/message/message_id_store.py | 46 |
4 files changed, 56 insertions, 68 deletions
diff --git a/pylint/message/message.py b/pylint/message/message.py index ae99c94ae..f7a67f5ae 100644 --- a/pylint/message/message.py +++ b/pylint/message/message.py @@ -40,7 +40,7 @@ class Message(_MsgBase): *location ) - def format(self, template): + def format(self, template: str) -> str: """Format the message according to the given template. The template format is the one of the format method : diff --git a/pylint/message/message_definition.py b/pylint/message/message_definition.py index 3b2e917b5..1e8c53ea6 100644 --- a/pylint/message/message_definition.py +++ b/pylint/message/message_definition.py @@ -2,6 +2,7 @@ # For details: https://github.com/PyCQA/pylint/blob/master/LICENSE import sys +from typing import List, Optional, Tuple from pylint.constants import MSG_TYPES from pylint.exceptions import InvalidMessageError @@ -11,15 +12,15 @@ from pylint.utils import normalize_text class MessageDefinition: def __init__( self, - checker, - msgid, - msg, - description, - symbol, - scope, - minversion=None, - maxversion=None, - old_names=None, + checker, # BaseChecker + msgid: str, + msg: str, + description: str, + symbol: str, + scope: str, + minversion: Optional[Tuple[int, int, int, str, int]] = None, + maxversion: Optional[Tuple[int, int, int, str, int]] = None, + old_names: List[Tuple[str, str]] = None, ): self.checker_name = checker.name self.check_msgid(msgid) @@ -30,11 +31,13 @@ class MessageDefinition: self.scope = scope self.minversion = minversion self.maxversion = maxversion - self.old_names = [] + self.old_names: List[Tuple[str, str]] = [] if old_names: for old_msgid, old_symbol in old_names: self.check_msgid(old_msgid) - self.old_names.append([old_msgid, old_symbol]) + self.old_names.append( + (old_msgid, old_symbol), + ) @staticmethod def check_msgid(msgid: str) -> None: @@ -49,7 +52,7 @@ class MessageDefinition: def __str__(self): return f"{repr(self)}:\n{self.msg} {self.description}" - def may_be_emitted(self): + def may_be_emitted(self) -> bool: """return True if message may be emitted using the current interpreter""" if self.minversion is not None and self.minversion > sys.version_info: return False @@ -57,7 +60,7 @@ class MessageDefinition: return False return True - def format_help(self, checkerref=False): + def format_help(self, checkerref: bool = False) -> str: """return the help string for the given message id""" desc = self.description if checkerref: @@ -69,11 +72,13 @@ class MessageDefinition: restr.append("< %s" % ".".join(str(n) for n in self.minversion)) if self.maxversion: restr.append(">= %s" % ".".join(str(n) for n in self.maxversion)) - restr = " or ".join(restr) + restriction = " or ".join(restr) if checkerref: - desc += " It can't be emitted when using Python %s." % restr + desc += " It can't be emitted when using Python %s." % restriction else: - desc += " This message can't be emitted when using Python %s." % restr + desc += ( + " This message can't be emitted when using Python %s." % restriction + ) msg_help = normalize_text(" ".join(desc.split()), indent=" ") message_id = f"{self.symbol} ({self.msgid})" if title != "%s": diff --git a/pylint/message/message_definition_store.py b/pylint/message/message_definition_store.py index 7b01a0bc4..c8007c54d 100644 --- a/pylint/message/message_definition_store.py +++ b/pylint/message/message_definition_store.py @@ -2,8 +2,10 @@ # For details: https://github.com/PyCQA/pylint/blob/master/LICENSE import collections +from typing import Dict, List, ValuesView from pylint.exceptions import UnknownMessageError +from pylint.message.message_definition import MessageDefinition from pylint.message.message_id_store import MessageIdStore @@ -14,57 +16,46 @@ class MessageDefinitionStore: """ def __init__(self): - self.message_id_store = MessageIdStore() + self.message_id_store: MessageIdStore = MessageIdStore() # Primary registry for all active messages definitions. # It contains the 1:1 mapping from msgid to MessageDefinition. # Keys are msgid, values are MessageDefinition - self._messages_definitions = {} + self._messages_definitions: Dict[str, MessageDefinition] = {} # MessageDefinition kept by category - self._msgs_by_category = collections.defaultdict(list) + self._msgs_by_category: Dict[str, List[str]] = collections.defaultdict(list) @property - def messages(self) -> list: + def messages(self) -> ValuesView[MessageDefinition]: """The list of all active messages.""" return self._messages_definitions.values() - def register_messages_from_checker(self, checker): - """Register all messages definitions from a checker. - - :param BaseChecker checker: - """ + def register_messages_from_checker(self, checker) -> None: + """Register all messages definitions from a checker.""" checker.check_consistency() for message in checker.messages: self.register_message(message) - def register_message(self, message): - """Register a MessageDefinition with consistency in mind. - - :param MessageDefinition message: The message definition being added. - """ + def register_message(self, message: MessageDefinition) -> None: + """Register a MessageDefinition with consistency in mind.""" self.message_id_store.register_message_definition(message) self._messages_definitions[message.msgid] = message self._msgs_by_category[message.msgid[0]].append(message.msgid) - def get_message_definitions(self, msgid_or_symbol: str) -> list: - """Returns the Message object for this message. - :param str msgid_or_symbol: msgid_or_symbol may be either a numeric or symbolic id. - :raises UnknownMessageError: if the message id is not defined. - :rtype: List of MessageDefinition - :return: A message definition corresponding to msgid_or_symbol - """ + def get_message_definitions(self, msgid_or_symbol: str) -> List[MessageDefinition]: + """Returns the Message definition for either a numeric or symbolic id.""" return [ self._messages_definitions[m] for m in self.message_id_store.get_active_msgids(msgid_or_symbol) ] - def get_msg_display_string(self, msgid_or_symbol: str): + def get_msg_display_string(self, msgid_or_symbol: str) -> str: """Generates a user-consumable representation of a message.""" message_definitions = self.get_message_definitions(msgid_or_symbol) if len(message_definitions) == 1: return repr(message_definitions[0].symbol) return repr([md.symbol for md in message_definitions]) - def help_message(self, msgids_or_symbols: list): + def help_message(self, msgids_or_symbols: List[str]) -> None: """Display help messages for the given message identifiers""" for msgids_or_symbol in msgids_or_symbols: try: @@ -78,7 +69,7 @@ class MessageDefinitionStore: print("") continue - def list_messages(self): + def list_messages(self) -> None: """Output full messages list documentation in ReST format.""" messages = sorted(self._messages_definitions.values(), key=lambda m: m.msgid) for message in messages: diff --git a/pylint/message/message_id_store.py b/pylint/message/message_id_store.py index 9f4d4fd2a..0530b1026 100644 --- a/pylint/message/message_id_store.py +++ b/pylint/message/message_id_store.py @@ -1,7 +1,6 @@ # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html # For details: https://github.com/PyCQA/pylint/blob/master/LICENSE - -from typing import List +from typing import Dict, List, Optional from pylint.exceptions import InvalidMessageError, UnknownMessageError @@ -11,9 +10,9 @@ class MessageIdStore: """The MessageIdStore store MessageId and make sure that there is a 1-1 relation between msgid and symbol.""" def __init__(self): - self.__msgid_to_symbol = {} - self.__symbol_to_msgid = {} - self.__old_names = {} + self.__msgid_to_symbol: Dict[str, str] = {} + self.__symbol_to_msgid: Dict[str, str] = {} + self.__old_names: Dict[str, List[str]] = {} def __len__(self): return len(self.__msgid_to_symbol) @@ -56,7 +55,9 @@ class MessageIdStore: self.__msgid_to_symbol[msgid] = symbol self.__symbol_to_msgid[symbol] = msgid - def add_legacy_msgid_and_symbol(self, msgid: str, symbol: str, new_msgid: str): + def add_legacy_msgid_and_symbol( + self, msgid: str, symbol: str, new_msgid: str + ) -> None: """Add valid legacy message id. There is a little duplication with add_msgid_and_symbol to avoid a function call, @@ -68,24 +69,20 @@ class MessageIdStore: self.__old_names[msgid] = existing_old_names def check_msgid_and_symbol(self, msgid: str, symbol: str) -> None: - existing_msgid = self.__symbol_to_msgid.get(symbol) - existing_symbol = self.__msgid_to_symbol.get(msgid) + existing_msgid: Optional[str] = self.__symbol_to_msgid.get(symbol) + existing_symbol: Optional[str] = self.__msgid_to_symbol.get(msgid) if existing_symbol is None and existing_msgid is None: - return + return # both symbol and msgid are usable if existing_msgid is not None: if existing_msgid != msgid: self._raise_duplicate_msgid(symbol, msgid, existing_msgid) if existing_symbol != symbol: - self._raise_duplicate_symbol(msgid, symbol, existing_symbol) + # See https://github.com/python/mypy/issues/10559 + self._raise_duplicate_symbol(msgid, symbol, existing_symbol) # type: ignore @staticmethod - def _raise_duplicate_symbol(msgid, symbol, other_symbol): - """Raise an error when a symbol is duplicated. - - :param str msgid: The msgid corresponding to the symbols - :param str symbol: Offending symbol - :param str other_symbol: Other offending symbol - :raises InvalidMessageError:""" + def _raise_duplicate_symbol(msgid: str, symbol: str, other_symbol: str): + """Raise an error when a symbol is duplicated.""" symbols = [symbol, other_symbol] symbols.sort() error_message = f"Message id '{msgid}' cannot have both " @@ -93,13 +90,8 @@ class MessageIdStore: raise InvalidMessageError(error_message) @staticmethod - def _raise_duplicate_msgid(symbol, msgid, other_msgid): - """Raise an error when a msgid is duplicated. - - :param str symbol: The symbol corresponding to the msgids - :param str msgid: Offending msgid - :param str other_msgid: Other offending msgid - :raises InvalidMessageError:""" + def _raise_duplicate_msgid(symbol: str, msgid: str, other_msgid: str) -> None: + """Raise an error when a msgid is duplicated.""" msgids = [msgid, other_msgid] msgids.sort() error_message = ( @@ -112,14 +104,14 @@ class MessageIdStore: def get_active_msgids(self, msgid_or_symbol: str) -> List[str]: """Return msgids but the input can be a symbol.""" # Only msgid can have a digit as second letter - is_msgid = msgid_or_symbol[1:].isdigit() + is_msgid: bool = msgid_or_symbol[1:].isdigit() if is_msgid: msgid = msgid_or_symbol.upper() symbol = self.__msgid_to_symbol.get(msgid) else: - msgid = self.__symbol_to_msgid.get(msgid_or_symbol) + msgid = self.__symbol_to_msgid.get(msgid_or_symbol) # type: ignore symbol = msgid_or_symbol - if not msgid or not symbol: + if msgid is None or symbol is None or not msgid or not symbol: error_msg = f"No such message id or symbol '{msgid_or_symbol}'." raise UnknownMessageError(error_msg) return self.__old_names.get(msgid, [msgid]) |