Skip to content

Commit c4fa0a7

Browse files
committed
Use python-typing-update on pylint/testutils directory
1 parent 2b2d1b5 commit c4fa0a7

14 files changed

+125
-104
lines changed

pylint/testutils/checker_test_case.py

+5-3
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 contextlib
68
import warnings
7-
from typing import Dict, Generator, Optional, Type
9+
from typing import Generator
810

911
from pylint.checkers.base_checker import BaseChecker
1012
from pylint.constants import PY38_PLUS
@@ -17,8 +19,8 @@
1719
class CheckerTestCase:
1820
"""A base testcase class for unit testing individual checker classes."""
1921

20-
CHECKER_CLASS: Optional[Type[BaseChecker]]
21-
CONFIG: Dict = {}
22+
CHECKER_CLASS: type[BaseChecker] | None
23+
CONFIG: dict = {}
2224

2325
def setup_method(self):
2426
self.linter = UnittestLinter()

pylint/testutils/configuration_test.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
44

55
"""Utility functions for configuration testing."""
6+
7+
from __future__ import annotations
8+
69
import copy
710
import json
811
import logging
912
import re
1013
import unittest
1114
from pathlib import Path
12-
from typing import Any, Dict, List, Tuple, Union
15+
from typing import Any, Dict
1316
from unittest.mock import Mock
1417

1518
from pylint.constants import PY38_PLUS
@@ -29,7 +32,7 @@
2932

3033

3134
def get_expected_or_default(
32-
tested_configuration_file: Union[str, Path],
35+
tested_configuration_file: str | Path,
3336
suffix: str,
3437
default: str,
3538
) -> str:
@@ -79,8 +82,8 @@ def get_expected_configuration(
7982

8083

8184
def get_related_files(
82-
tested_configuration_file: Union[str, Path], suffix_filter: str
83-
) -> List[Path]:
85+
tested_configuration_file: str | Path, suffix_filter: str
86+
) -> list[Path]:
8487
"""Return all the file related to a test conf file endind with a suffix."""
8588
conf_path = Path(tested_configuration_file)
8689
return [
@@ -91,8 +94,8 @@ def get_related_files(
9194

9295

9396
def get_expected_output(
94-
configuration_path: Union[str, Path], user_specific_path: Path
95-
) -> Tuple[int, str]:
97+
configuration_path: str | Path, user_specific_path: Path
98+
) -> tuple[int, str]:
9699
"""Get the expected output of a functional test."""
97100
exit_code = 0
98101
msg = (
@@ -140,8 +143,8 @@ def get_expected_output(
140143

141144

142145
def run_using_a_configuration_file(
143-
configuration_path: Union[Path, str], file_to_lint: str = __file__
144-
) -> Tuple[Mock, Mock, Run]:
146+
configuration_path: Path | str, file_to_lint: str = __file__
147+
) -> tuple[Mock, Mock, Run]:
145148
"""Simulate a run with a configuration without really launching the checks."""
146149
configuration_path = str(configuration_path)
147150
args = ["--rcfile", configuration_path, file_to_lint]

pylint/testutils/functional/find_functional_tests.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +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 os
68
from pathlib import Path
7-
from typing import List, Set, Union
89

910
from pylint.testutils.functional.test_file import FunctionalTestFile
1011

@@ -30,8 +31,8 @@
3031

3132

3233
def get_functional_test_files_from_directory(
33-
input_dir: Union[Path, str]
34-
) -> List[FunctionalTestFile]:
34+
input_dir: Path | str,
35+
) -> list[FunctionalTestFile]:
3536
"""Get all functional tests in the input_dir."""
3637
suite = []
3738

@@ -52,8 +53,8 @@ def _check_functional_tests_structure(directory: Path) -> None:
5253
if Path(directory).stem.startswith("_"):
5354
return
5455

55-
files: Set[Path] = set()
56-
dirs: Set[Path] = set()
56+
files: set[Path] = set()
57+
dirs: set[Path] = set()
5758

5859
# Collect all subdirectories and files in directory
5960
for file_or_dir in directory.iterdir():

pylint/testutils/functional/lint_module_output_update.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +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 csv
68
import os
7-
from typing import List, Optional
89

910
from _pytest.config import Config
1011

@@ -26,7 +27,7 @@ class TestDialect(csv.excel):
2627
csv.register_dialect("test", TestDialect)
2728

2829
def __init__(
29-
self, test_file: FunctionalTestFile, config: Optional[Config] = None
30+
self, test_file: FunctionalTestFile, config: Config | None = None
3031
) -> None:
3132
if not PY38_PLUS:
3233
raise RuntimeError(
@@ -39,8 +40,8 @@ def __init__(
3940
def _check_output_text(
4041
self,
4142
_: MessageCounter,
42-
expected_output: List[OutputLine],
43-
actual_output: List[OutputLine],
43+
expected_output: list[OutputLine],
44+
actual_output: list[OutputLine],
4445
) -> None:
4546
"""Overwrite or remove the expected output file based on actual output."""
4647
# Remove the file if no output is actually expected and a file exists

pylint/testutils/functional/test_file.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
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 configparser
68
import sys
79
from os.path import basename, exists, join
8-
from typing import Callable, Dict, List, Tuple, Union
10+
from typing import Callable
911

1012

11-
def parse_python_version(ver_str: str) -> Tuple[int, ...]:
13+
def parse_python_version(ver_str: str) -> tuple[int, ...]:
1214
"""Convert python version to a tuple of integers for easy comparison."""
1315
return tuple(int(digit) for digit in ver_str.split("."))
1416

@@ -24,12 +26,12 @@ class NoFileError(Exception):
2426

2527

2628
class TestFileOptions(TypedDict):
27-
min_pyver: Tuple[int, ...]
28-
max_pyver: Tuple[int, ...]
29-
min_pyver_end_position: Tuple[int, ...]
30-
requires: List[str]
31-
except_implementations: List[str]
32-
exclude_platforms: List[str]
29+
min_pyver: tuple[int, ...]
30+
max_pyver: tuple[int, ...]
31+
min_pyver_end_position: tuple[int, ...]
32+
requires: list[str]
33+
except_implementations: list[str]
34+
exclude_platforms: list[str]
3335

3436

3537
# mypy need something literal, we can't create this dynamically from TestFileOptions
@@ -46,7 +48,7 @@ class TestFileOptions(TypedDict):
4648
class FunctionalTestFile:
4749
"""A single functional test case file with options."""
4850

49-
_CONVERTERS: Dict[str, Callable[[str], Union[Tuple[int, ...], List[str]]]] = {
51+
_CONVERTERS: dict[str, Callable[[str], tuple[int, ...] | list[str]]] = {
5052
"min_pyver": parse_python_version,
5153
"max_pyver": parse_python_version,
5254
"min_pyver_end_position": parse_python_version,

pylint/testutils/get_test_info.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
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
from glob import glob
68
from os.path import basename, join, splitext
7-
from typing import List, Tuple
89

910
from pylint.testutils.constants import SYS_VERS_STR
1011

1112

1213
def _get_tests_info(
1314
input_dir: str, msg_dir: str, prefix: str, suffix: str
14-
) -> List[Tuple[str, str]]:
15+
) -> list[tuple[str, str]]:
1516
"""Get python input examples and output messages.
1617
1718
We use following conventions for input files and messages:

pylint/testutils/lint_module_test.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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 csv
68
import operator
79
import platform
@@ -10,7 +12,7 @@
1012
from io import StringIO
1113
from pathlib import Path
1214
from typing import Counter as CounterType
13-
from typing import Dict, List, Optional, TextIO, Tuple, Union
15+
from typing import TextIO, Tuple
1416

1517
import pytest
1618
from _pytest.config import Config
@@ -37,15 +39,15 @@ class LintModuleTest:
3739
maxDiff = None
3840

3941
def __init__(
40-
self, test_file: FunctionalTestFile, config: Optional[Config] = None
42+
self, test_file: FunctionalTestFile, config: Config | None = None
4143
) -> None:
4244
_test_reporter = FunctionalTestReporter()
4345
self._linter = PyLinter()
4446
self._linter.namespace.persistent = 0
4547
checkers.initialize(self._linter)
4648

4749
# See if test has its own .rc file, if so we use that one
48-
rc_file: Union[Path, str] = PYLINTRC
50+
rc_file: Path | str = PYLINTRC
4951
try:
5052
rc_file = test_file.option_file
5153
self._linter.disable("suppressed-message")
@@ -140,7 +142,7 @@ def get_expected_messages(stream: TextIO) -> MessageCounter:
140142
def multiset_difference(
141143
expected_entries: MessageCounter,
142144
actual_entries: MessageCounter,
143-
) -> Tuple[MessageCounter, Dict[Tuple[int, str], int]]:
145+
) -> tuple[MessageCounter, dict[tuple[int, str], int]]:
144146
"""Takes two multisets and compares them.
145147
146148
A multiset is a dict with the cardinality of the key as the value.
@@ -168,7 +170,7 @@ def _open_source_file(self) -> TextIO:
168170
return open(self._test_file.source, encoding="latin1")
169171
return open(self._test_file.source, encoding="utf8")
170172

171-
def _get_expected(self) -> Tuple[MessageCounter, List[OutputLine]]:
173+
def _get_expected(self) -> tuple[MessageCounter, list[OutputLine]]:
172174
with self._open_source_file() as f:
173175
expected_msgs = self.get_expected_messages(f)
174176
if not expected_msgs:
@@ -180,8 +182,8 @@ def _get_expected(self) -> Tuple[MessageCounter, List[OutputLine]]:
180182
]
181183
return expected_msgs, expected_output_lines
182184

183-
def _get_actual(self) -> Tuple[MessageCounter, List[OutputLine]]:
184-
messages: List[Message] = self._linter.reporter.messages
185+
def _get_actual(self) -> tuple[MessageCounter, list[OutputLine]]:
186+
messages: list[Message] = self._linter.reporter.messages
185187
messages.sort(key=lambda m: (m.line, m.symbol, m.msg))
186188
received_msgs: MessageCounter = Counter()
187189
received_output_lines = []
@@ -212,7 +214,7 @@ def error_msg_for_unequal_messages(
212214
self,
213215
actual_messages: MessageCounter,
214216
expected_messages: MessageCounter,
215-
actual_output: List[OutputLine],
217+
actual_output: list[OutputLine],
216218
) -> str:
217219
msg = [f'Wrong results for file "{self._test_file.base}":']
218220
missing, unexpected = self.multiset_difference(
@@ -232,8 +234,8 @@ def error_msg_for_unequal_messages(
232234

233235
def error_msg_for_unequal_output(
234236
self,
235-
expected_lines: List[OutputLine],
236-
received_lines: List[OutputLine],
237+
expected_lines: list[OutputLine],
238+
received_lines: list[OutputLine],
237239
) -> str:
238240
missing = set(expected_lines) - set(received_lines)
239241
unexpected = set(received_lines) - set(expected_lines)
@@ -257,8 +259,8 @@ def error_msg_for_unequal_output(
257259
def _check_output_text(
258260
self,
259261
_: MessageCounter,
260-
expected_output: List[OutputLine],
261-
actual_output: List[OutputLine],
262+
expected_output: list[OutputLine],
263+
actual_output: list[OutputLine],
262264
) -> None:
263265
"""This is a function because we want to be able to update the text in LintModuleOutputUpdate."""
264266
assert expected_output == actual_output, self.error_msg_for_unequal_output(

0 commit comments

Comments
 (0)