Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ def get_exception_handlers(
return [
handler for handler in context.handlers if error_of_type(handler, exception)
]
return None
return []


def is_node_inside_try_except(node: astroid.Raise) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions pylint/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
from pylint import checkers, config, exceptions, interfaces, reporters
from pylint.__pkginfo__ import version
from pylint.constants import MAIN_CHECKER_NAME, MSG_TYPES, OPTION_RGX
from pylint.message import Message, MessagesHandlerMixIn, MessagesStore
from pylint.message import Message, MessageDefinitionStore, MessagesHandlerMixIn
from pylint.reporters.ureports import nodes as report_nodes
from pylint.utils import ASTWalker, FileState, utils

Expand Down Expand Up @@ -593,7 +593,7 @@ def __init__(self, options=(), reporter=None, option_groups=(), pylintrc=None):
# some stuff has to be done before ancestors initialization...
#
# messages store / checkers / reporter / astroid manager
self.msgs_store = MessagesStore()
self.msgs_store = MessageDefinitionStore()
self.reporter = None
self._reporter_name = None
self._reporters = {}
Expand Down
11 changes: 9 additions & 2 deletions pylint/message/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@

from pylint.message.message import Message
from pylint.message.message_definition import MessageDefinition
from pylint.message.message_definition_store import MessageDefinitionStore
from pylint.message.message_handler_mix_in import MessagesHandlerMixIn
from pylint.message.message_store import MessagesStore
from pylint.message.message_id import MessageId

__all__ = ["Message", "MessageDefinition", "MessagesHandlerMixIn", "MessagesStore"]
__all__ = [
"Message",
"MessageDefinition",
"MessageDefinitionStore",
"MessagesHandlerMixIn",
"MessageId",
]
28 changes: 13 additions & 15 deletions pylint/message/message_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

import sys

from pylint.constants import MSG_TYPES
from pylint.exceptions import InvalidMessageError
from pylint.message.message_id import MessageId
from pylint.utils import normalize_text


Expand All @@ -24,19 +23,23 @@ def __init__(
old_names=None,
):
self.checker = checker
if len(msgid) != 5:
raise InvalidMessageError("Invalid message id %r" % msgid)
if not msgid[0] in MSG_TYPES:
raise InvalidMessageError("Bad message type %s in %r" % (msgid[0], msgid))
self.msgid = msgid
MessageId.check_msgid(msgid)
self.message_id = MessageId(msgid, symbol)
self.msg = msg
self.description = description
self.symbol = symbol
self.scope = scope
self.minversion = minversion
self.maxversion = maxversion
self.old_names = old_names or []

@property
def symbol(self):
return self.message_id.symbol

@property
def msgid(self):
return self.message_id.msgid

def __repr__(self):
return "MessageDefinition:%s (%s)" % (self.symbol, self.msgid)

Expand All @@ -57,10 +60,6 @@ def format_help(self, checkerref=False):
if checkerref:
desc += " This message belongs to the %s checker." % self.checker.name
title = self.msg
if self.symbol:
msgid = "%s (%s)" % (self.symbol, self.msgid)
else:
msgid = self.msgid
if self.minversion or self.maxversion:
restr = []
if self.minversion:
Expand All @@ -75,6 +74,5 @@ def format_help(self, checkerref=False):
desc = normalize_text(" ".join(desc.split()), indent=" ")
if title != "%s":
title = title.splitlines()[0]

return ":%s: *%s*\n%s" % (msgid, title.rstrip(" "), desc)
return ":%s:\n%s" % (msgid, desc)
return ":%s: *%s*\n%s" % (self.message_id, title.rstrip(" "), desc)
return ":%s:\n%s" % (self.message_id, desc)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pylint.exceptions import InvalidMessageError, UnknownMessageError


class MessagesStore:
class MessageDefinitionStore:

"""The messages store knows information about every possible message but has
no particular state during analysis.
Expand Down
17 changes: 10 additions & 7 deletions pylint/message/message_handler_mix_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,11 @@ def add_message(
message_definition, line, node, args, confidence, col_offset
)

def add_one_message(
self, message_definition, line, node, args, confidence, col_offset
):
# backward compatibility, message may not have a symbol
symbol = message_definition.symbol or message_definition.msgid
# Fatal messages and reports are special, the node/scope distinction
# does not apply to them.
@staticmethod
def check_message_definition(message_definition, line, node):
if message_definition.msgid[0] not in _SCOPE_EXEMPT:
# Fatal messages and reports are special, the node/scope distinction
# does not apply to them.
if message_definition.scope == WarningScope.LINE:
if line is None:
raise InvalidMessageError(
Expand All @@ -271,6 +268,12 @@ def add_one_message(
% message_definition.msgid
)

def add_one_message(
self, message_definition, line, node, args, confidence, col_offset
):
# backward compatibility, message may not have a symbol
symbol = message_definition.symbol or message_definition.msgid
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"):
Expand Down
31 changes: 31 additions & 0 deletions pylint/message/message_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-

# 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

from pylint.constants import MSG_TYPES
from pylint.exceptions import InvalidMessageError


class MessageId:
def __init__(self, msgid, symbol):
self.msgid = msgid
self.symbol = symbol

def __str__(self):
return "%s (%s)" % (self.symbol, self.msgid)

def __hash__(self):
return "{}-{}".format(self.msgid, self.symbol).__hash__()

def __eq__(self, other):
return self.msgid == other.msgid and self.symbol == other.symbol

@staticmethod
def check_msgid(msgid: str) -> None:
"""This is a static method used in MessageDefinition and not the
MessageId constructor for performance reasons."""
if len(msgid) != 5:
raise InvalidMessageError("Invalid message id %r" % msgid)
if msgid[0] not in MSG_TYPES:
raise InvalidMessageError("Bad message type %s in %r" % (msgid[0], msgid))
17 changes: 2 additions & 15 deletions tests/message/unittest_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

from pylint.checkers import BaseChecker
from pylint.exceptions import InvalidMessageError
from pylint.message import MessageDefinition, MessagesStore
from pylint.message import MessageDefinition, MessageDefinitionStore


@pytest.fixture
def store():
return MessagesStore()
return MessageDefinitionStore()


@pytest.mark.parametrize(
Expand Down Expand Up @@ -141,16 +141,3 @@ class CheckerTwo(BaseChecker):
{"W1234": ("message two", "msg-symbol-two", "another msg description.")},
"Message id 'W1234' cannot have both 'msg-symbol-one' and 'msg-symbol-two' as symbolic name.",
)


@pytest.mark.parametrize(
"msgid,expected",
[
("Q1234", "Bad message type Q in 'Q1234'"),
("W12345", "Invalid message id 'W12345'"),
],
)
def test_create_invalid_message_type(msgid, expected):
with pytest.raises(InvalidMessageError) as cm:
MessageDefinition("checker", msgid, "msg", "descr", "symbol", "scope")
assert str(cm.value) == expected
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

from pylint.checkers import BaseChecker
from pylint.exceptions import InvalidMessageError, UnknownMessageError
from pylint.message import MessageDefinition, MessagesStore
from pylint.message import MessageDefinition, MessageDefinitionStore


@pytest.fixture
def store():
store = MessagesStore()
store = MessageDefinitionStore()

class Checker(BaseChecker):
name = "achecker"
Expand Down
53 changes: 53 additions & 0 deletions tests/message/unittest_message_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-

# 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 pytest

from pylint.exceptions import InvalidMessageError
from pylint.message import MessageId


@pytest.fixture
def msgid():
return "W1234"


@pytest.fixture
def symbol():
return "msg-symbol"


@pytest.fixture
def message_id(msgid, symbol):
return MessageId(msgid, symbol)


@pytest.mark.parametrize(
"msgid,expected",
[
("Q1234", "Bad message type Q in 'Q1234'"),
("W12345", "Invalid message id 'W12345'"),
],
)
def test_create_invalid_message_type(msgid, expected):
with pytest.raises(InvalidMessageError) as cm:
MessageId.check_msgid(msgid)
assert str(cm.value) == expected


def test_hash_and_eq(message_id):
"""MessageId should be hashable"""
dict_ = {}
message_id_2 = MessageId("W1235", "msg-symbol-2")
dict_[message_id] = 1
dict_[message_id_2] = 2
assert dict_[message_id] != dict_[message_id_2]
assert message_id != message_id_2


def test_str(message_id, msgid, symbol):
result = str(message_id)
assert msgid in result
assert symbol in result