From f82dd2200d2e1828c0ce5bf9e2142f04b9bef6ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Tue, 11 Jan 2022 09:49:55 +0100 Subject: [PATCH 1/4] Fix remaining typing issues for ``PyLinter`` and related functions --- pylint/constants.py | 8 +++++++- pylint/lint/pylinter.py | 15 ++++++++++++--- pylint/utils/file_state.py | 4 +++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/pylint/constants.py b/pylint/constants.py index db54113ee4..b310ca2139 100644 --- a/pylint/constants.py +++ b/pylint/constants.py @@ -8,6 +8,12 @@ from pylint.__pkginfo__ import __version__ +if sys.version_info >= (3, 8): + from typing import Final +else: + from typing_extensions import Final + + PY37_PLUS = sys.version_info[:2] >= (3, 7) PY38_PLUS = sys.version_info[:2] >= (3, 8) PY39_PLUS = sys.version_info[:2] >= (3, 9) @@ -24,7 +30,7 @@ # The line/node distinction does not apply to fatal errors and reports. _SCOPE_EXEMPT = "FR" -MSG_TYPES = { +MSG_TYPES: Final = { "I": "info", "C": "convention", "R": "refactor", diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index 8b1993b4ea..9f4f3ca2be 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -537,7 +537,7 @@ def make_options() -> Tuple[Tuple[str, OptionDict], ...]: def __init__( self, options=(), - reporter=None, + reporter: Union[reporters.BaseReporter, reporters.MultiReporter, None] = None, option_groups=(), pylintrc=None, ): @@ -1496,7 +1496,11 @@ def _add_one_message( msg_cat = MSG_TYPES[message_definition.msgid[0]] self.msg_status |= MSG_TYPES_STATUS[message_definition.msgid[0]] self.stats.increase_single_message_count(msg_cat, 1) - self.stats.increase_single_module_message_count(self.current_name, msg_cat, 1) + self.stats.increase_single_module_message_count( + self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580 + msg_cat, # type: ignore[arg-type] # Mypy doesn't see a Final dict as dict of literals + 1, + ) try: self.stats.by_msg[message_definition.symbol] += 1 except KeyError: @@ -1613,6 +1617,9 @@ def _set_one_msg_status( ) -> None: """Set the status of an individual message""" if scope == "module": + if not isinstance(line, int): + raise ValueError + self.file_state.set_msg_status(msg, line, enable) if not enable and msg.symbol != "locally-disabled": self.add_message( @@ -1641,7 +1648,9 @@ def _get_messages_to_set( else: category_id_formatted = category_id if category_id_formatted is not None: - for _msgid in self.msgs_store._msgs_by_category.get(category_id_formatted): + for _msgid in self.msgs_store._msgs_by_category.get( + category_id_formatted, [] + ): message_definitions.extend( self._get_messages_to_set(_msgid, enable, ignore_unknown) ) diff --git a/pylint/utils/file_state.py b/pylint/utils/file_state.py index 8527031eab..4b437f7f0f 100644 --- a/pylint/utils/file_state.py +++ b/pylint/utils/file_state.py @@ -130,7 +130,7 @@ def set_msg_status(self, msg: "MessageDefinition", line: int, status: bool) -> N self._module_msgs_state[msg.msgid] = {line: status} def handle_ignored_message( - self, state_scope: Optional[Literal[0, 1, 2]], msgid: str, line: int + self, state_scope: Optional[Literal[0, 1, 2]], msgid: str, line: Optional[int] ) -> None: """Report an ignored message. @@ -139,6 +139,8 @@ def handle_ignored_message( or globally. """ if state_scope == MSG_STATE_SCOPE_MODULE: + if not isinstance(line, int): + raise ValueError try: orig_line = self._suppression_mapping[(msgid, line)] self._ignored_msgs[(msgid, orig_line)].add(line) From 01dcf704b916e5cd6e87b72d9fc8fc9031a335e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Thu, 3 Feb 2022 21:27:56 +0100 Subject: [PATCH 2/4] Code review --- pylint/lint/pylinter.py | 3 +-- pylint/utils/file_state.py | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index a8f41b7e4d..ba8301b288 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -1635,8 +1635,7 @@ def _set_one_msg_status( ) -> None: """Set the status of an individual message""" if scope == "module": - if not isinstance(line, int): - raise ValueError + assert isinstance(line, int) self.file_state.set_msg_status(msg, line, enable) if not enable and msg.symbol != "locally-disabled": diff --git a/pylint/utils/file_state.py b/pylint/utils/file_state.py index 4b437f7f0f..136eff78f6 100644 --- a/pylint/utils/file_state.py +++ b/pylint/utils/file_state.py @@ -139,8 +139,8 @@ def handle_ignored_message( or globally. """ if state_scope == MSG_STATE_SCOPE_MODULE: - if not isinstance(line, int): - raise ValueError + assert isinstance(line, int) + try: orig_line = self._suppression_mapping[(msgid, line)] self._ignored_msgs[(msgid, orig_line)].add(line) From f5b05e5701f6c358647bce2da9a798e38458889c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Sat, 5 Feb 2022 22:16:13 +0100 Subject: [PATCH 3/4] Code review --- pylint/constants.py | 8 +------- pylint/lint/pylinter.py | 2 +- pylint/utils/file_state.py | 2 +- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/pylint/constants.py b/pylint/constants.py index b310ca2139..db54113ee4 100644 --- a/pylint/constants.py +++ b/pylint/constants.py @@ -8,12 +8,6 @@ from pylint.__pkginfo__ import __version__ -if sys.version_info >= (3, 8): - from typing import Final -else: - from typing_extensions import Final - - PY37_PLUS = sys.version_info[:2] >= (3, 7) PY38_PLUS = sys.version_info[:2] >= (3, 8) PY39_PLUS = sys.version_info[:2] >= (3, 9) @@ -30,7 +24,7 @@ # The line/node distinction does not apply to fatal errors and reports. _SCOPE_EXEMPT = "FR" -MSG_TYPES: Final = { +MSG_TYPES = { "I": "info", "C": "convention", "R": "refactor", diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index ba8301b288..f39faecd23 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -1635,7 +1635,7 @@ def _set_one_msg_status( ) -> None: """Set the status of an individual message""" if scope == "module": - assert isinstance(line, int) + assert isinstance(line, int) # should always be int inside module scope self.file_state.set_msg_status(msg, line, enable) if not enable and msg.symbol != "locally-disabled": diff --git a/pylint/utils/file_state.py b/pylint/utils/file_state.py index 136eff78f6..4252f01542 100644 --- a/pylint/utils/file_state.py +++ b/pylint/utils/file_state.py @@ -139,7 +139,7 @@ def handle_ignored_message( or globally. """ if state_scope == MSG_STATE_SCOPE_MODULE: - assert isinstance(line, int) + assert isinstance(line, int) # should always be int inside module scope try: orig_line = self._suppression_mapping[(msgid, line)] From 1d644a1b47623a593ee6757af7a44b7a2b05f0b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Sat, 5 Feb 2022 22:28:32 +0100 Subject: [PATCH 4/4] Fix? --- pylint/constants.py | 6 ++++-- pylint/lint/pylinter.py | 2 +- pylint/typing.py | 6 ++++++ pylint/utils/linterstats.py | 9 +++------ 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/pylint/constants.py b/pylint/constants.py index db54113ee4..813cd54dd8 100644 --- a/pylint/constants.py +++ b/pylint/constants.py @@ -2,11 +2,13 @@ # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE import platform import sys +from typing import Dict import astroid import platformdirs from pylint.__pkginfo__ import __version__ +from pylint.typing import MessageTypesFullName PY37_PLUS = sys.version_info[:2] >= (3, 7) PY38_PLUS = sys.version_info[:2] >= (3, 8) @@ -24,7 +26,7 @@ # The line/node distinction does not apply to fatal errors and reports. _SCOPE_EXEMPT = "FR" -MSG_TYPES = { +MSG_TYPES: Dict[str, MessageTypesFullName] = { "I": "info", "C": "convention", "R": "refactor", @@ -32,7 +34,7 @@ "E": "error", "F": "fatal", } -MSG_TYPES_LONG = {v: k for k, v in MSG_TYPES.items()} +MSG_TYPES_LONG: Dict[str, str] = {v: k for k, v in MSG_TYPES.items()} MSG_TYPES_STATUS = {"I": 0, "C": 16, "R": 8, "W": 4, "E": 2, "F": 1} diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index f39faecd23..17de0aea01 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -1516,7 +1516,7 @@ def _add_one_message( self.stats.increase_single_message_count(msg_cat, 1) self.stats.increase_single_module_message_count( self.current_name, # type: ignore[arg-type] # Should be removable after https://github.com/PyCQA/pylint/pull/5580 - msg_cat, # type: ignore[arg-type] # Mypy doesn't see a Final dict as dict of literals + msg_cat, 1, ) try: diff --git a/pylint/typing.py b/pylint/typing.py index 402a6f7162..09e4df126c 100644 --- a/pylint/typing.py +++ b/pylint/typing.py @@ -64,3 +64,9 @@ class ManagedMessage(NamedTuple): symbol: str line: Optional[int] is_disabled: bool + + +MessageTypesFullName = Literal[ + "convention", "error", "fatal", "info", "refactor", "statement", "warning" +] +"""All possible message categories.""" diff --git a/pylint/utils/linterstats.py b/pylint/utils/linterstats.py index 4a05c93df0..125954e4eb 100644 --- a/pylint/utils/linterstats.py +++ b/pylint/utils/linterstats.py @@ -4,6 +4,8 @@ import sys from typing import Dict, List, Optional, Set, cast +from pylint.typing import MessageTypesFullName + if sys.version_info >= (3, 8): from typing import Literal, TypedDict else: @@ -73,11 +75,6 @@ class ModuleStats(TypedDict): warning: int -ModuleStatsAttribute = Literal[ - "convention", "error", "fatal", "info", "refactor", "statement", "warning" -] - - # pylint: disable-next=too-many-instance-attributes class LinterStats: """Class used to linter stats""" @@ -290,7 +287,7 @@ def increase_single_message_count(self, type_name: str, increase: int) -> None: setattr(self, type_name, getattr(self, type_name) + increase) def increase_single_module_message_count( - self, modname: str, type_name: ModuleStatsAttribute, increase: int + self, modname: str, type_name: MessageTypesFullName, increase: int ) -> None: """Increase the message type count of an individual message type of a module""" self.by_module[modname][type_name] += increase