summaryrefslogtreecommitdiff
path: root/pylint
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2022-04-25 22:55:47 +0200
committerGitHub <noreply@github.com>2022-04-25 22:55:47 +0200
commit1fe352583390d0fb3f390eccba95c9a78814e3f2 (patch)
treec77bf36b8d042fb11dc1edbdc31a7656ddac587e /pylint
parentaca9c1739c15ba5ea74a7d814f1b6cc23e79267b (diff)
downloadpylint-git-1fe352583390d0fb3f390eccba95c9a78814e3f2.tar.gz
Add typing to ``BaseChecker`` ``msgs`` and ``reports`` (#6456)
Diffstat (limited to 'pylint')
-rw-r--r--pylint/checkers/base_checker.py6
-rw-r--r--pylint/checkers/classes/class_checker.py5
-rw-r--r--pylint/checkers/design_analysis.py5
-rw-r--r--pylint/checkers/exceptions.py5
-rw-r--r--pylint/checkers/format.py3
-rw-r--r--pylint/checkers/imports.py3
-rw-r--r--pylint/checkers/logging.py5
-rw-r--r--pylint/checkers/newstyle.py3
-rw-r--r--pylint/checkers/similar.py4
-rw-r--r--pylint/checkers/strings.py5
-rw-r--r--pylint/checkers/typecheck.py3
-rw-r--r--pylint/checkers/variables.py3
-rw-r--r--pylint/lint/pylinter.py3
-rw-r--r--pylint/lint/report_functions.py6
-rw-r--r--pylint/reporters/reports_handler_mix_in.py4
-rw-r--r--pylint/typing.py15
16 files changed, 57 insertions, 21 deletions
diff --git a/pylint/checkers/base_checker.py b/pylint/checkers/base_checker.py
index 7b04a74bb..e1c39cf9f 100644
--- a/pylint/checkers/base_checker.py
+++ b/pylint/checkers/base_checker.py
@@ -18,7 +18,7 @@ from pylint.constants import _MSG_ORDER, MAIN_CHECKER_NAME, WarningScope
from pylint.exceptions import InvalidMessageError
from pylint.interfaces import Confidence, IRawChecker, ITokenChecker, implements
from pylint.message.message_definition import MessageDefinition
-from pylint.typing import Options
+from pylint.typing import MessageDefinitionTuple, Options, ReportsCallable
from pylint.utils import get_rst_section, get_rst_title
if TYPE_CHECKING:
@@ -33,9 +33,9 @@ class BaseChecker(_ArgumentsProvider):
# ordered list of options to control the checker behaviour
options: Options = ()
# messages issued by this checker
- msgs: Any = {}
+ msgs: dict[str, MessageDefinitionTuple] = {}
# reports issued by this checker
- reports: Any = ()
+ reports: tuple[tuple[str, str, ReportsCallable], ...] = ()
# mark this checker as enabled or not.
enabled: bool = True
diff --git a/pylint/checkers/classes/class_checker.py b/pylint/checkers/classes/class_checker.py
index 2683a8c1c..10184a264 100644
--- a/pylint/checkers/classes/class_checker.py
+++ b/pylint/checkers/classes/class_checker.py
@@ -39,6 +39,7 @@ from pylint.checkers.utils import (
uninferable_final_decorators,
)
from pylint.interfaces import HIGH, INFERENCE
+from pylint.typing import MessageDefinitionTuple
if sys.version_info >= (3, 8):
from functools import cached_property
@@ -428,7 +429,9 @@ def _has_same_layout_slots(slots, assigned_value):
return False
-MSGS = { # pylint: disable=consider-using-namedtuple-or-dataclass
+MSGS: dict[
+ str, MessageDefinitionTuple
+] = { # pylint: disable=consider-using-namedtuple-or-dataclass
"F0202": (
"Unable to check methods signature (%s / %s)",
"method-check-failed",
diff --git a/pylint/checkers/design_analysis.py b/pylint/checkers/design_analysis.py
index 9fccb9798..530517c9d 100644
--- a/pylint/checkers/design_analysis.py
+++ b/pylint/checkers/design_analysis.py
@@ -17,6 +17,7 @@ from astroid import nodes
from pylint.checkers import BaseChecker
from pylint.checkers.utils import only_required_for_messages
+from pylint.typing import MessageDefinitionTuple
if sys.version_info >= (3, 8):
from functools import cached_property
@@ -26,7 +27,9 @@ else:
if TYPE_CHECKING:
from pylint.lint import PyLinter
-MSGS = { # pylint: disable=consider-using-namedtuple-or-dataclass
+MSGS: dict[
+ str, MessageDefinitionTuple
+] = { # pylint: disable=consider-using-namedtuple-or-dataclass
"R0901": (
"Too many ancestors (%s/%s)",
"too-many-ancestors",
diff --git a/pylint/checkers/exceptions.py b/pylint/checkers/exceptions.py
index 2dc7b959e..7e182c3dc 100644
--- a/pylint/checkers/exceptions.py
+++ b/pylint/checkers/exceptions.py
@@ -15,6 +15,7 @@ from astroid import nodes, objects
from pylint import checkers
from pylint.checkers import utils
+from pylint.typing import MessageDefinitionTuple
if TYPE_CHECKING:
from pylint.lint import PyLinter
@@ -54,7 +55,9 @@ def _is_raising(body: list) -> bool:
OVERGENERAL_EXCEPTIONS = ("BaseException", "Exception")
-MSGS = { # pylint: disable=consider-using-namedtuple-or-dataclass
+MSGS: dict[
+ str, MessageDefinitionTuple
+] = { # pylint: disable=consider-using-namedtuple-or-dataclass
"E0701": (
"Bad except clauses order (%s)",
"bad-except-order",
diff --git a/pylint/checkers/format.py b/pylint/checkers/format.py
index 21e91e269..9ff5f5ebe 100644
--- a/pylint/checkers/format.py
+++ b/pylint/checkers/format.py
@@ -27,6 +27,7 @@ from pylint.checkers.utils import (
only_required_for_messages,
)
from pylint.constants import WarningScope
+from pylint.typing import MessageDefinitionTuple
from pylint.utils.pragma_parser import OPTION_PO, PragmaParserError, parse_pragma
if TYPE_CHECKING:
@@ -50,7 +51,7 @@ _KEYWORD_TOKENS = {
_JUNK_TOKENS = {tokenize.COMMENT, tokenize.NL}
-MSGS = {
+MSGS: dict[str, MessageDefinitionTuple] = {
"C0301": (
"Line too long (%s/%s)",
"line-too-long",
diff --git a/pylint/checkers/imports.py b/pylint/checkers/imports.py
index 1839c98c0..c37984ecd 100644
--- a/pylint/checkers/imports.py
+++ b/pylint/checkers/imports.py
@@ -26,6 +26,7 @@ from pylint.checkers.utils import (
from pylint.exceptions import EmptyReportError
from pylint.graph import DotBackend, get_cycles
from pylint.reporters.ureports.nodes import Paragraph, Section, VerbatimText
+from pylint.typing import MessageDefinitionTuple
from pylint.utils import IsortDriver
if TYPE_CHECKING:
@@ -195,7 +196,7 @@ def _make_graph(
# the import checker itself ###################################################
-MSGS = {
+MSGS: dict[str, MessageDefinitionTuple] = {
"E0401": (
"Unable to import %s",
"import-error",
diff --git a/pylint/checkers/logging.py b/pylint/checkers/logging.py
index 7c6e75fcb..a82afcdef 100644
--- a/pylint/checkers/logging.py
+++ b/pylint/checkers/logging.py
@@ -15,11 +15,14 @@ from astroid import nodes
from pylint import checkers
from pylint.checkers import utils
from pylint.checkers.utils import infer_all
+from pylint.typing import MessageDefinitionTuple
if TYPE_CHECKING:
from pylint.lint import PyLinter
-MSGS = { # pylint: disable=consider-using-namedtuple-or-dataclass
+MSGS: dict[
+ str, MessageDefinitionTuple
+] = { # pylint: disable=consider-using-namedtuple-or-dataclass
"W1201": (
"Use %s formatting in logging functions",
"logging-not-lazy",
diff --git a/pylint/checkers/newstyle.py b/pylint/checkers/newstyle.py
index dbcaa08be..6b0aa5095 100644
--- a/pylint/checkers/newstyle.py
+++ b/pylint/checkers/newstyle.py
@@ -17,11 +17,12 @@ from pylint.checkers.utils import (
node_frame_class,
only_required_for_messages,
)
+from pylint.typing import MessageDefinitionTuple
if TYPE_CHECKING:
from pylint.lint import PyLinter
-MSGS = {
+MSGS: dict[str, MessageDefinitionTuple] = {
"E1003": (
"Bad first argument %r given to super()",
"bad-super-call",
diff --git a/pylint/checkers/similar.py b/pylint/checkers/similar.py
index f26cda437..d50c15c0a 100644
--- a/pylint/checkers/similar.py
+++ b/pylint/checkers/similar.py
@@ -50,7 +50,7 @@ from astroid import nodes
from pylint.checkers import BaseChecker, BaseRawFileChecker, table_lines_from_stats
from pylint.reporters.ureports.nodes import Table
-from pylint.typing import Options
+from pylint.typing import MessageDefinitionTuple, Options
from pylint.utils import LinterStats, decoding_stream
if TYPE_CHECKING:
@@ -713,7 +713,7 @@ class LineSet:
return self._real_lines
-MSGS = {
+MSGS: dict[str, MessageDefinitionTuple] = {
"R0801": (
"Similar lines in %s files\n%s",
"duplicate-code",
diff --git a/pylint/checkers/strings.py b/pylint/checkers/strings.py
index f8ff5108e..2ff1b52fa 100644
--- a/pylint/checkers/strings.py
+++ b/pylint/checkers/strings.py
@@ -19,6 +19,7 @@ from astroid import nodes
from pylint.checkers import BaseChecker, BaseRawFileChecker, BaseTokenChecker, utils
from pylint.checkers.utils import only_required_for_messages
+from pylint.typing import MessageDefinitionTuple
if TYPE_CHECKING:
from pylint.lint import PyLinter
@@ -56,7 +57,9 @@ SINGLE_QUOTED_REGEX = re.compile(f"({'|'.join(_PREFIXES)})?'''")
DOUBLE_QUOTED_REGEX = re.compile(f"({'|'.join(_PREFIXES)})?\"\"\"")
QUOTE_DELIMITER_REGEX = re.compile(f"({'|'.join(_PREFIXES)})?(\"|')", re.DOTALL)
-MSGS = { # pylint: disable=consider-using-namedtuple-or-dataclass
+MSGS: dict[
+ str, MessageDefinitionTuple
+] = { # pylint: disable=consider-using-namedtuple-or-dataclass
"E1300": (
"Unsupported format character %r (%#02x) at index %d",
"bad-format-character",
diff --git a/pylint/checkers/typecheck.py b/pylint/checkers/typecheck.py
index 1fae8496f..c7058e5f1 100644
--- a/pylint/checkers/typecheck.py
+++ b/pylint/checkers/typecheck.py
@@ -46,6 +46,7 @@ from pylint.checkers.utils import (
supports_setitem,
)
from pylint.interfaces import INFERENCE
+from pylint.typing import MessageDefinitionTuple
if sys.version_info >= (3, 8):
from functools import cached_property
@@ -218,7 +219,7 @@ def _missing_member_hint(owner, attrname, distance_threshold, max_choices):
return f"; maybe {names_hint}?"
-MSGS = {
+MSGS: dict[str, MessageDefinitionTuple] = {
"E1101": (
"%s %r has no %r member%s",
"no-member",
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
index 08847fdf6..73b58caf0 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -28,6 +28,7 @@ from pylint.checkers.utils import (
)
from pylint.constants import PY39_PLUS, TYPING_TYPE_CHECKS_GUARDS
from pylint.interfaces import CONTROL_FLOW, HIGH, INFERENCE, INFERENCE_FAILURE
+from pylint.typing import MessageDefinitionTuple
if sys.version_info >= (3, 8):
from functools import cached_property
@@ -363,7 +364,7 @@ def _has_locals_call_after_node(stmt, scope):
return False
-MSGS = {
+MSGS: dict[str, MessageDefinitionTuple] = {
"E0601": (
"Using variable %r before assignment",
"used-before-assignment",
diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py
index 8b6466089..14ca1f934 100644
--- a/pylint/lint/pylinter.py
+++ b/pylint/lint/pylinter.py
@@ -52,6 +52,7 @@ from pylint.reporters.ureports import nodes as report_nodes
from pylint.typing import (
FileItem,
ManagedMessage,
+ MessageDefinitionTuple,
MessageLocationTuple,
ModuleDescriptionDict,
Options,
@@ -99,7 +100,7 @@ def _load_reporter_by_class(reporter_class: str) -> type[BaseReporter]:
# Python Linter class #########################################################
-MSGS = {
+MSGS: dict[str, MessageDefinitionTuple] = {
"F0001": (
"%s",
"fatal",
diff --git a/pylint/lint/report_functions.py b/pylint/lint/report_functions.py
index 9925b46f2..7d1674977 100644
--- a/pylint/lint/report_functions.py
+++ b/pylint/lint/report_functions.py
@@ -15,7 +15,7 @@ from pylint.utils import LinterStats
def report_total_messages_stats(
sect: Section,
stats: LinterStats,
- previous_stats: LinterStats,
+ previous_stats: LinterStats | None,
) -> None:
"""Make total errors / warnings report."""
lines = ["type", "number", "previous", "difference"]
@@ -26,7 +26,7 @@ def report_total_messages_stats(
def report_messages_stats(
sect: Section,
stats: LinterStats,
- _: LinterStats,
+ _: LinterStats | None,
) -> None:
"""Make messages type report."""
by_msg_stats = stats.by_msg
@@ -45,7 +45,7 @@ def report_messages_stats(
def report_messages_by_module_stats(
sect: Section,
stats: LinterStats,
- _: LinterStats,
+ _: LinterStats | None,
) -> None:
"""Make errors / warnings by modules report."""
module_stats = stats.by_module
diff --git a/pylint/reporters/reports_handler_mix_in.py b/pylint/reporters/reports_handler_mix_in.py
index f6abdc092..cec76690f 100644
--- a/pylint/reporters/reports_handler_mix_in.py
+++ b/pylint/reporters/reports_handler_mix_in.py
@@ -6,17 +6,17 @@ from __future__ import annotations
import collections
from collections.abc import MutableSequence
-from typing import TYPE_CHECKING, Callable, DefaultDict, List, Optional, Tuple
+from typing import TYPE_CHECKING, DefaultDict, List, Tuple
from pylint.exceptions import EmptyReportError
from pylint.reporters.ureports.nodes import Section
+from pylint.typing import ReportsCallable
from pylint.utils import LinterStats
if TYPE_CHECKING:
from pylint.checkers import BaseChecker
from pylint.lint.pylinter import PyLinter
-ReportsCallable = Callable[[Section, LinterStats, Optional[LinterStats]], None]
ReportsDict = DefaultDict["BaseChecker", List[Tuple[str, str, ReportsCallable]]]
diff --git a/pylint/typing.py b/pylint/typing.py
index 718bb6d3b..c0ff59422 100644
--- a/pylint/typing.py
+++ b/pylint/typing.py
@@ -13,7 +13,9 @@ from typing import (
Callable,
Dict,
Iterable,
+ List,
NamedTuple,
+ Optional,
Pattern,
Tuple,
Type,
@@ -31,6 +33,8 @@ if TYPE_CHECKING:
from pylint.checkers import BaseChecker
from pylint.config.callback_actions import _CallbackAction
+ from pylint.reporters.ureports.nodes import Section
+ from pylint.utils import LinterStats
class FileItem(NamedTuple):
@@ -117,3 +121,14 @@ AstCallback = Callable[["nodes.NodeNG"], None]
CheckerT_co = TypeVar("CheckerT_co", bound="BaseChecker", covariant=True)
AstCallbackMethod = Callable[[CheckerT_co, "nodes.NodeNG"], None]
"""Callable representing a visit or leave method."""
+
+ReportsCallable = Callable[["Section", "LinterStats", Optional["LinterStats"]], None]
+"""Callable to create a report."""
+
+SimpleMessageTuple = Tuple[str, str, str]
+OldNamesMessageTuple = Tuple[str, str, str, Dict[str, List[Tuple[str, str]]]]
+VersionMessageTuple = Tuple[str, str, str, Dict[str, Tuple[int, int]]]
+ScopedMessageTuple = Tuple[str, str, str, Dict[str, str]]
+MessageDefinitionTuple = Union[
+ SimpleMessageTuple, OldNamesMessageTuple, VersionMessageTuple, ScopedMessageTuple
+]