Skip to content

Commit 85a4804

Browse files
committed
Use python-typing-update on pylint/utils and message dirs
1 parent c4fa0a7 commit 85a4804

8 files changed

+101
-95
lines changed

pylint/message/message_definition.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
33
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
44

5+
from __future__ import annotations
6+
57
import sys
6-
from typing import TYPE_CHECKING, Any, List, Optional, Tuple
8+
from typing import TYPE_CHECKING, Any
79

810
from astroid import nodes
911

@@ -18,15 +20,15 @@
1820
class MessageDefinition:
1921
def __init__(
2022
self,
21-
checker: "BaseChecker",
23+
checker: BaseChecker,
2224
msgid: str,
2325
msg: str,
2426
description: str,
2527
symbol: str,
2628
scope: str,
27-
minversion: Optional[Tuple[int, int]] = None,
28-
maxversion: Optional[Tuple[int, int]] = None,
29-
old_names: Optional[List[Tuple[str, str]]] = None,
29+
minversion: tuple[int, int] | None = None,
30+
maxversion: tuple[int, int] | None = None,
31+
old_names: list[tuple[str, str]] | None = None,
3032
) -> None:
3133
self.checker_name = checker.name
3234
self.check_msgid(msgid)
@@ -37,7 +39,7 @@ def __init__(
3739
self.scope = scope
3840
self.minversion = minversion
3941
self.maxversion = maxversion
40-
self.old_names: List[Tuple[str, str]] = []
42+
self.old_names: list[tuple[str, str]] = []
4143
if old_names:
4244
for old_msgid, old_symbol in old_names:
4345
self.check_msgid(old_msgid)
@@ -100,7 +102,7 @@ def format_help(self, checkerref: bool = False) -> str:
100102
return f":{message_id}:\n{msg_help}"
101103

102104
def check_message_definition(
103-
self, line: Optional[int], node: Optional[nodes.NodeNG]
105+
self, line: int | None, node: nodes.NodeNG | None
104106
) -> None:
105107
"""Check MessageDefinition for possible errors."""
106108
if self.msgid[0] not in _SCOPE_EXEMPT:

pylint/message/message_definition_store.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
33
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
44

5+
from __future__ import annotations
6+
57
import collections
68
import functools
7-
from typing import TYPE_CHECKING, Dict, List, Tuple, ValuesView
9+
from typing import TYPE_CHECKING, ValuesView
810

911
from pylint.exceptions import UnknownMessageError
1012
from pylint.message.message_definition import MessageDefinition
@@ -25,16 +27,16 @@ def __init__(self) -> None:
2527
# Primary registry for all active messages definitions.
2628
# It contains the 1:1 mapping from msgid to MessageDefinition.
2729
# Keys are msgid, values are MessageDefinition
28-
self._messages_definitions: Dict[str, MessageDefinition] = {}
30+
self._messages_definitions: dict[str, MessageDefinition] = {}
2931
# MessageDefinition kept by category
30-
self._msgs_by_category: Dict[str, List[str]] = collections.defaultdict(list)
32+
self._msgs_by_category: dict[str, list[str]] = collections.defaultdict(list)
3133

3234
@property
3335
def messages(self) -> ValuesView[MessageDefinition]:
3436
"""The list of all active messages."""
3537
return self._messages_definitions.values()
3638

37-
def register_messages_from_checker(self, checker: "BaseChecker") -> None:
39+
def register_messages_from_checker(self, checker: BaseChecker) -> None:
3840
"""Register all messages definitions from a checker."""
3941
checker.check_consistency()
4042
for message in checker.messages:
@@ -53,7 +55,7 @@ def register_message(self, message: MessageDefinition) -> None:
5355
# risk of creating a large memory leak.
5456
# See discussion in: https://github.com/PyCQA/pylint/pull/5673
5557
@functools.lru_cache(maxsize=None) # pylint: disable=cache-max-size-none
56-
def get_message_definitions(self, msgid_or_symbol: str) -> List[MessageDefinition]:
58+
def get_message_definitions(self, msgid_or_symbol: str) -> list[MessageDefinition]:
5759
"""Returns the Message definition for either a numeric or symbolic id.
5860
5961
The cache has no limit as its size will likely stay minimal. For each message we store
@@ -72,7 +74,7 @@ def get_msg_display_string(self, msgid_or_symbol: str) -> str:
7274
return repr(message_definitions[0].symbol)
7375
return repr([md.symbol for md in message_definitions])
7476

75-
def help_message(self, msgids_or_symbols: List[str]) -> None:
77+
def help_message(self, msgids_or_symbols: list[str]) -> None:
7678
"""Display help messages for the given message identifiers."""
7779
for msgids_or_symbol in msgids_or_symbols:
7880
try:
@@ -99,7 +101,7 @@ def list_messages(self) -> None:
99101

100102
def find_emittable_messages(
101103
self,
102-
) -> Tuple[List[MessageDefinition], List[MessageDefinition]]:
104+
) -> tuple[list[MessageDefinition], list[MessageDefinition]]:
103105
"""Finds all emittable and non-emittable messages."""
104106
messages = sorted(self._messages_definitions.values(), key=lambda m: m.msgid)
105107
emittable = []

pylint/message/message_id_store.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
33
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
44

5-
from typing import Dict, List, NoReturn, Optional, Tuple
5+
from __future__ import annotations
6+
7+
from typing import NoReturn
68

79
from pylint.exceptions import InvalidMessageError, UnknownMessageError
810

@@ -12,10 +14,10 @@ class MessageIdStore:
1214
"""The MessageIdStore store MessageId and make sure that there is a 1-1 relation between msgid and symbol."""
1315

1416
def __init__(self) -> None:
15-
self.__msgid_to_symbol: Dict[str, str] = {}
16-
self.__symbol_to_msgid: Dict[str, str] = {}
17-
self.__old_names: Dict[str, List[str]] = {}
18-
self.__active_msgids: Dict[str, List[str]] = {}
17+
self.__msgid_to_symbol: dict[str, str] = {}
18+
self.__symbol_to_msgid: dict[str, str] = {}
19+
self.__old_names: dict[str, list[str]] = {}
20+
self.__active_msgids: dict[str, list[str]] = {}
1921

2022
def __len__(self) -> int:
2123
return len(self.__msgid_to_symbol)
@@ -42,7 +44,7 @@ def get_msgid(self, symbol: str) -> str:
4244
raise UnknownMessageError(msg) from e
4345

4446
def register_message_definition(
45-
self, msgid: str, symbol: str, old_names: List[Tuple[str, str]]
47+
self, msgid: str, symbol: str, old_names: list[tuple[str, str]]
4648
) -> None:
4749
self.check_msgid_and_symbol(msgid, symbol)
4850
self.add_msgid_and_symbol(msgid, symbol)
@@ -74,8 +76,8 @@ def add_legacy_msgid_and_symbol(
7476
self.__old_names[msgid] = existing_old_names
7577

7678
def check_msgid_and_symbol(self, msgid: str, symbol: str) -> None:
77-
existing_msgid: Optional[str] = self.__symbol_to_msgid.get(symbol)
78-
existing_symbol: Optional[str] = self.__msgid_to_symbol.get(msgid)
79+
existing_msgid: str | None = self.__symbol_to_msgid.get(symbol)
80+
existing_symbol: str | None = self.__msgid_to_symbol.get(msgid)
7981
if existing_symbol is None and existing_msgid is None:
8082
return # both symbol and msgid are usable
8183
if existing_msgid is not None:
@@ -106,7 +108,7 @@ def _raise_duplicate_msgid(symbol: str, msgid: str, other_msgid: str) -> NoRetur
106108
)
107109
raise InvalidMessageError(error_message)
108110

109-
def get_active_msgids(self, msgid_or_symbol: str) -> List[str]:
111+
def get_active_msgids(self, msgid_or_symbol: str) -> list[str]:
110112
"""Return msgids but the input can be a symbol.
111113
112114
self.__active_msgids is used to implement a primitive cache for this function.
@@ -117,7 +119,7 @@ def get_active_msgids(self, msgid_or_symbol: str) -> List[str]:
117119
pass
118120

119121
# If we don't have a cached value yet we compute it
120-
msgid: Optional[str]
122+
msgid: str | None
121123
if msgid_or_symbol[1:].isdigit():
122124
# Only msgid can have a digit as second letter
123125
msgid = msgid_or_symbol.upper()

pylint/utils/docs.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
"""Various helper functions to create the docs of a linter object."""
66

7+
from __future__ import annotations
8+
79
import sys
810
import warnings
9-
from typing import TYPE_CHECKING, Dict, TextIO
11+
from typing import TYPE_CHECKING, TextIO
1012

1113
from pylint.constants import MAIN_CHECKER_NAME
1214
from pylint.utils.utils import get_rst_section, get_rst_title
@@ -15,9 +17,9 @@
1517
from pylint.lint.pylinter import PyLinter
1618

1719

18-
def _get_checkers_infos(linter: "PyLinter") -> Dict[str, Dict]:
20+
def _get_checkers_infos(linter: PyLinter) -> dict[str, dict]:
1921
"""Get info from a checker and handle KeyError."""
20-
by_checker: Dict[str, Dict] = {}
22+
by_checker: dict[str, dict] = {}
2123
for checker in linter.get_checkers():
2224
name = checker.name
2325
if name != "master":
@@ -40,7 +42,7 @@ def _get_checkers_infos(linter: "PyLinter") -> Dict[str, Dict]:
4042
return by_checker
4143

4244

43-
def _get_checkers_documentation(linter: "PyLinter") -> str:
45+
def _get_checkers_documentation(linter: PyLinter) -> str:
4446
"""Get documentation for individual checkers."""
4547
result = get_rst_title("Pylint global options and switches", "-")
4648
result += """
@@ -81,6 +83,6 @@ def _get_checkers_documentation(linter: "PyLinter") -> str:
8183
return result
8284

8385

84-
def print_full_documentation(linter: "PyLinter", stream: TextIO = sys.stdout) -> None:
86+
def print_full_documentation(linter: PyLinter, stream: TextIO = sys.stdout) -> None:
8587
"""Output a full documentation in ReST format."""
8688
print(_get_checkers_documentation(linter)[:-1], file=stream)

pylint/utils/file_state.py

+15-22
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,11 @@
22
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
33
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
44

5+
from __future__ import annotations
6+
57
import collections
68
import sys
7-
from typing import (
8-
TYPE_CHECKING,
9-
DefaultDict,
10-
Dict,
11-
Iterator,
12-
Optional,
13-
Set,
14-
Tuple,
15-
Union,
16-
)
9+
from typing import TYPE_CHECKING, DefaultDict, Dict, Iterator
1710

1811
from astroid import nodes
1912

@@ -38,18 +31,18 @@
3831
class FileState:
3932
"""Hold internal state specific to the currently analyzed file."""
4033

41-
def __init__(self, modname: Optional[str] = None) -> None:
34+
def __init__(self, modname: str | None = None) -> None:
4235
self.base_name = modname
4336
self._module_msgs_state: MessageStateDict = {}
4437
self._raw_module_msgs_state: MessageStateDict = {}
4538
self._ignored_msgs: DefaultDict[
46-
Tuple[str, int], Set[int]
39+
tuple[str, int], set[int]
4740
] = collections.defaultdict(set)
48-
self._suppression_mapping: Dict[Tuple[str, int], int] = {}
49-
self._effective_max_line_number: Optional[int] = None
41+
self._suppression_mapping: dict[tuple[str, int], int] = {}
42+
self._effective_max_line_number: int | None = None
5043

5144
def collect_block_lines(
52-
self, msgs_store: "MessageDefinitionStore", module_node: nodes.Module
45+
self, msgs_store: MessageDefinitionStore, module_node: nodes.Module
5346
) -> None:
5447
"""Walk the AST to collect block level options line numbers."""
5548
for msg, lines in self._module_msgs_state.items():
@@ -62,7 +55,7 @@ def collect_block_lines(
6255

6356
def _collect_block_lines(
6457
self,
65-
msgs_store: "MessageDefinitionStore",
58+
msgs_store: MessageDefinitionStore,
6659
node: nodes.NodeNG,
6760
msg_state: MessageStateDict,
6861
) -> None:
@@ -126,7 +119,7 @@ def _collect_block_lines(
126119
self._module_msgs_state[msgid] = {line: state}
127120
del lines[lineno]
128121

129-
def set_msg_status(self, msg: "MessageDefinition", line: int, status: bool) -> None:
122+
def set_msg_status(self, msg: MessageDefinition, line: int, status: bool) -> None:
130123
"""Set status (enabled/disable) for a given message at a given line."""
131124
assert line > 0
132125
try:
@@ -135,7 +128,7 @@ def set_msg_status(self, msg: "MessageDefinition", line: int, status: bool) -> N
135128
self._module_msgs_state[msg.msgid] = {line: status}
136129

137130
def handle_ignored_message(
138-
self, state_scope: Optional[Literal[0, 1, 2]], msgid: str, line: Optional[int]
131+
self, state_scope: Literal[0, 1, 2] | None, msgid: str, line: int | None
139132
) -> None:
140133
"""Report an ignored message.
141134
@@ -154,12 +147,12 @@ def handle_ignored_message(
154147

155148
def iter_spurious_suppression_messages(
156149
self,
157-
msgs_store: "MessageDefinitionStore",
150+
msgs_store: MessageDefinitionStore,
158151
) -> Iterator[
159-
Tuple[
152+
tuple[
160153
Literal["useless-suppression", "suppressed-message"],
161154
int,
162-
Union[Tuple[str], Tuple[str, int]],
155+
tuple[str] | tuple[str, int],
163156
]
164157
]:
165158
for warning, lines in self._raw_module_msgs_state.items():
@@ -180,5 +173,5 @@ def iter_spurious_suppression_messages(
180173
from_,
181174
)
182175

183-
def get_effective_max_line_number(self) -> Optional[int]:
176+
def get_effective_max_line_number(self) -> int | None:
184177
return self._effective_max_line_number

pylint/utils/linterstats.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
33
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
44

5+
from __future__ import annotations
6+
57
import sys
6-
from typing import Dict, List, Optional, Set, cast
8+
from typing import cast
79

810
from pylint.typing import MessageTypesFullName
911

@@ -83,14 +85,14 @@ class LinterStats:
8385

8486
def __init__(
8587
self,
86-
bad_names: Optional[BadNames] = None,
87-
by_module: Optional[Dict[str, ModuleStats]] = None,
88-
by_msg: Optional[Dict[str, int]] = None,
89-
code_type_count: Optional[CodeTypeCount] = None,
90-
dependencies: Optional[Dict[str, Set[str]]] = None,
91-
duplicated_lines: Optional[DuplicatedLines] = None,
92-
node_count: Optional[NodeCount] = None,
93-
undocumented: Optional[UndocumentedNodes] = None,
88+
bad_names: BadNames | None = None,
89+
by_module: dict[str, ModuleStats] | None = None,
90+
by_msg: dict[str, int] | None = None,
91+
code_type_count: CodeTypeCount | None = None,
92+
dependencies: dict[str, set[str]] | None = None,
93+
duplicated_lines: DuplicatedLines | None = None,
94+
node_count: NodeCount | None = None,
95+
undocumented: UndocumentedNodes | None = None,
9496
) -> None:
9597
self.bad_names = bad_names or BadNames(
9698
argument=0,
@@ -106,13 +108,13 @@ def __init__(
106108
variable=0,
107109
typevar=0,
108110
)
109-
self.by_module: Dict[str, ModuleStats] = by_module or {}
110-
self.by_msg: Dict[str, int] = by_msg or {}
111+
self.by_module: dict[str, ModuleStats] = by_module or {}
112+
self.by_msg: dict[str, int] = by_msg or {}
111113
self.code_type_count = code_type_count or CodeTypeCount(
112114
code=0, comment=0, docstring=0, empty=0, total=0
113115
)
114116

115-
self.dependencies: Dict[str, Set[str]] = dependencies or {}
117+
self.dependencies: dict[str, set[str]] = dependencies or {}
116118
self.duplicated_lines = duplicated_lines or DuplicatedLines(
117119
nb_duplicated_lines=0, percent_duplicated_lines=0.0
118120
)
@@ -309,7 +311,7 @@ def reset_message_count(self) -> None:
309311
self.warning = 0
310312

311313

312-
def merge_stats(stats: List[LinterStats]):
314+
def merge_stats(stats: list[LinterStats]):
313315
"""Used to merge multiple stats objects into a new one when pylint is run in parallel mode."""
314316
merged = LinterStats()
315317
for stat in stats:

0 commit comments

Comments
 (0)