Skip to content

Commit 81ed650

Browse files
[typing] Use __future__ annotations where possible (#8264)
1 parent a0b28f9 commit 81ed650

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

doc/exts/pylint_messages.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
"""Script used to generate the messages files."""
66

7+
from __future__ import annotations
8+
79
import os
810
from collections import defaultdict
911
from inspect import getmodule
1012
from itertools import chain, groupby
1113
from pathlib import Path
12-
from typing import DefaultDict, Dict, List, NamedTuple, Optional, Tuple
14+
from typing import DefaultDict, Dict, List, NamedTuple, Tuple
1315

1416
from sphinx.application import Sphinx
1517

@@ -60,7 +62,7 @@ def _register_all_checkers_and_extensions(linter: PyLinter) -> None:
6062
initialize_extensions(linter)
6163

6264

63-
def _get_message_data(data_path: Path) -> Tuple[str, str, str, str]:
65+
def _get_message_data(data_path: Path) -> tuple[str, str, str, str]:
6466
"""Get the message data from the specified path."""
6567
good_py_path = data_path / "good.py"
6668
bad_py_path = data_path / "bad.py"
@@ -145,7 +147,7 @@ def _create_placeholders(
145147

146148
def _get_all_messages(
147149
linter: PyLinter,
148-
) -> Tuple[MessagesDict, OldMessagesDict]:
150+
) -> tuple[MessagesDict, OldMessagesDict]:
149151
"""Get all messages registered to a linter and return a dictionary indexed by
150152
message type.
151153
@@ -305,7 +307,7 @@ def _generate_checker_url(message: MessageData) -> str:
305307

306308

307309
def _write_single_shared_message_page(
308-
category_dir: Path, messages: List[MessageData]
310+
category_dir: Path, messages: list[MessageData]
309311
) -> None:
310312
message = messages[0]
311313
with open(category_dir / f"{message.name}.rst", "w", encoding="utf-8") as stream:
@@ -405,8 +407,8 @@ def _write_redirect_pages(old_messages: OldMessagesDict) -> None:
405407

406408
def _write_redirect_old_page(
407409
category_dir: Path,
408-
old_name: Tuple[str, str],
409-
new_names: List[Tuple[str, str]],
410+
old_name: tuple[str, str],
411+
new_names: list[tuple[str, str]],
410412
) -> None:
411413
old_name_file = os.path.join(category_dir, f"{old_name[0]}.rst")
412414
new_names_string = "".join(
@@ -428,7 +430,7 @@ def _write_redirect_old_page(
428430

429431

430432
# pylint: disable-next=unused-argument
431-
def build_messages_pages(app: Optional[Sphinx]) -> None:
433+
def build_messages_pages(app: Sphinx | None) -> None:
432434
"""Overwrite messages files by printing the documentation to a stream.
433435
434436
Documentation is written in ReST format.

doc/test_messages_documentation.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
"""Functional tests for the code examples in the messages' documentation."""
66

7+
from __future__ import annotations
8+
79
import sys
810

911
if sys.version_info[:2] > (3, 9):
@@ -18,7 +20,7 @@ def total(self):
1820

1921
from pathlib import Path
2022
from typing import Counter as CounterType
21-
from typing import List, Optional, TextIO, Tuple
23+
from typing import TextIO, Tuple
2224

2325
import pytest
2426

@@ -32,12 +34,12 @@ def total(self):
3234
MessageCounter = CounterType[Tuple[int, str]]
3335

3436

35-
def get_functional_test_files_from_directory(input_dir: Path) -> List[Tuple[str, Path]]:
37+
def get_functional_test_files_from_directory(input_dir: Path) -> list[tuple[str, Path]]:
3638
"""Get all functional tests in the input_dir.
3739
3840
This also checks the formatting of related.rst files.
3941
"""
40-
suite: List[Tuple[str, Path]] = []
42+
suite: list[tuple[str, Path]] = []
4143

4244
for subdirectory in input_dir.iterdir():
4345
for message_dir in subdirectory.iterdir():
@@ -69,7 +71,7 @@ def get_functional_test_files_from_directory(input_dir: Path) -> List[Tuple[str,
6971

7072

7173
class LintModuleTest:
72-
def __init__(self, test_file: Tuple[str, Path]) -> None:
74+
def __init__(self, test_file: tuple[str, Path]) -> None:
7375
self._test_file = test_file
7476

7577
_test_reporter = FunctionalTestReporter()
@@ -80,7 +82,7 @@ def __init__(self, test_file: Tuple[str, Path]) -> None:
8082

8183
# Check if this message has a custom configuration file (e.g. for enabling optional checkers).
8284
# If not, use the default configuration.
83-
config_file: Optional[Path]
85+
config_file: Path | None
8486
msgid, full_path = test_file
8587
pylintrc = full_path.parent / "pylintrc"
8688
config_file = pylintrc if pylintrc.exists() else None
@@ -136,7 +138,7 @@ def _get_expected(self) -> MessageCounter:
136138

137139
def _get_actual(self) -> MessageCounter:
138140
"""Get the actual messages after a run."""
139-
messages: List[Message] = self._linter.reporter.messages
141+
messages: list[Message] = self._linter.reporter.messages
140142
messages.sort(key=lambda m: (m.line, m.symbol, m.msg))
141143
received_msgs: MessageCounter = Counter()
142144
for msg in messages:
@@ -176,6 +178,6 @@ def assert_message_good(self, actual_messages: MessageCounter) -> str:
176178

177179
@pytest.mark.parametrize("test_file", TESTS, ids=TESTS_NAMES)
178180
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
179-
def test_code_examples(test_file: Tuple[str, Path]) -> None:
181+
def test_code_examples(test_file: tuple[str, Path]) -> None:
180182
lint_test = LintModuleTest(test_file)
181183
lint_test.runTest()

examples/custom.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from typing import TYPE_CHECKING
24

35
from astroid import nodes
@@ -59,7 +61,7 @@ def visit_call(self, node: nodes.Call) -> None:
5961
in_class.locals[param.name] = node
6062

6163

62-
def register(linter: "PyLinter") -> None:
64+
def register(linter: PyLinter) -> None:
6365
"""This required method auto registers the checker during initialization.
6466
6567
:param linter: The linter to register the checker to.

examples/custom_raw.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from typing import TYPE_CHECKING
24

35
from astroid import nodes
@@ -37,7 +39,7 @@ def process_module(self, node: nodes.Module) -> None:
3739
self.add_message("backslash-line-continuation", line=lineno)
3840

3941

40-
def register(linter: "PyLinter") -> None:
42+
def register(linter: PyLinter) -> None:
4143
"""This required method auto registers the checker during initialization.
4244
4345
:param linter: The linter to register the checker to.

pylint/checkers/classes/class_checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
11611161
self._check_property_with_parameters(node)
11621162

11631163
# 'is_method()' is called and makes sure that this is a 'nodes.ClassDef'
1164-
klass = node.parent.frame(future=True) # type: nodes.ClassDef
1164+
klass: nodes.ClassDef = node.parent.frame(future=True)
11651165
# check first argument is self if this is actually a method
11661166
self._check_first_arg_for_type(node, klass.type == "metaclass")
11671167
if node.name == "__init__":

0 commit comments

Comments
 (0)