Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use python-typing-update on pylint/testutils directory #6303

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pylint/testutils/checker_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

from __future__ import annotations

import contextlib
import warnings
from typing import Dict, Generator, Optional, Type
from typing import Generator

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

CHECKER_CLASS: Optional[Type[BaseChecker]]
CONFIG: Dict = {}
CHECKER_CLASS: type[BaseChecker] | None
CONFIG: dict = {}

def setup_method(self):
self.linter = UnittestLinter()
Expand Down
19 changes: 11 additions & 8 deletions pylint/testutils/configuration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

"""Utility functions for configuration testing."""

from __future__ import annotations

import copy
import json
import logging
import re
import unittest
from pathlib import Path
from typing import Any, Dict, List, Tuple, Union
from typing import Any, Dict
from unittest.mock import Mock

from pylint.constants import PY38_PLUS
Expand All @@ -29,7 +32,7 @@


def get_expected_or_default(
tested_configuration_file: Union[str, Path],
tested_configuration_file: str | Path,
suffix: str,
default: str,
) -> str:
Expand Down Expand Up @@ -79,8 +82,8 @@ def get_expected_configuration(


def get_related_files(
tested_configuration_file: Union[str, Path], suffix_filter: str
) -> List[Path]:
tested_configuration_file: str | Path, suffix_filter: str
) -> list[Path]:
"""Return all the file related to a test conf file endind with a suffix."""
conf_path = Path(tested_configuration_file)
return [
Expand All @@ -91,8 +94,8 @@ def get_related_files(


def get_expected_output(
configuration_path: Union[str, Path], user_specific_path: Path
) -> Tuple[int, str]:
configuration_path: str | Path, user_specific_path: Path
) -> tuple[int, str]:
"""Get the expected output of a functional test."""
exit_code = 0
msg = (
Expand Down Expand Up @@ -140,8 +143,8 @@ def get_expected_output(


def run_using_a_configuration_file(
configuration_path: Union[Path, str], file_to_lint: str = __file__
) -> Tuple[Mock, Mock, Run]:
configuration_path: Path | str, file_to_lint: str = __file__
) -> tuple[Mock, Mock, Run]:
"""Simulate a run with a configuration without really launching the checks."""
configuration_path = str(configuration_path)
args = ["--rcfile", configuration_path, file_to_lint]
Expand Down
11 changes: 6 additions & 5 deletions pylint/testutils/functional/find_functional_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

from __future__ import annotations

import os
from pathlib import Path
from typing import List, Set, Union

from pylint.testutils.functional.test_file import FunctionalTestFile

Expand All @@ -30,8 +31,8 @@


def get_functional_test_files_from_directory(
input_dir: Union[Path, str]
) -> List[FunctionalTestFile]:
input_dir: Path | str,
) -> list[FunctionalTestFile]:
"""Get all functional tests in the input_dir."""
suite = []

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

files: Set[Path] = set()
dirs: Set[Path] = set()
files: set[Path] = set()
dirs: set[Path] = set()

# Collect all subdirectories and files in directory
for file_or_dir in directory.iterdir():
Expand Down
9 changes: 5 additions & 4 deletions pylint/testutils/functional/lint_module_output_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

from __future__ import annotations

import csv
import os
from typing import List, Optional

from _pytest.config import Config

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

def __init__(
self, test_file: FunctionalTestFile, config: Optional[Config] = None
self, test_file: FunctionalTestFile, config: Config | None = None
) -> None:
if not PY38_PLUS:
raise RuntimeError(
Expand All @@ -39,8 +40,8 @@ def __init__(
def _check_output_text(
self,
_: MessageCounter,
expected_output: List[OutputLine],
actual_output: List[OutputLine],
expected_output: list[OutputLine],
actual_output: list[OutputLine],
) -> None:
"""Overwrite or remove the expected output file based on actual output."""
# Remove the file if no output is actually expected and a file exists
Expand Down
20 changes: 11 additions & 9 deletions pylint/testutils/functional/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

from __future__ import annotations

import configparser
import sys
from os.path import basename, exists, join
from typing import Callable, Dict, List, Tuple, Union
from typing import Callable


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

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


class TestFileOptions(TypedDict):
min_pyver: Tuple[int, ...]
max_pyver: Tuple[int, ...]
min_pyver_end_position: Tuple[int, ...]
requires: List[str]
except_implementations: List[str]
exclude_platforms: List[str]
min_pyver: tuple[int, ...]
max_pyver: tuple[int, ...]
min_pyver_end_position: tuple[int, ...]
requires: list[str]
except_implementations: list[str]
exclude_platforms: list[str]


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

_CONVERTERS: Dict[str, Callable[[str], Union[Tuple[int, ...], List[str]]]] = {
_CONVERTERS: dict[str, Callable[[str], tuple[int, ...] | list[str]]] = {
"min_pyver": parse_python_version,
"max_pyver": parse_python_version,
"min_pyver_end_position": parse_python_version,
Expand Down
5 changes: 3 additions & 2 deletions pylint/testutils/get_test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

from __future__ import annotations

from glob import glob
from os.path import basename, join, splitext
from typing import List, Tuple

from pylint.testutils.constants import SYS_VERS_STR


def _get_tests_info(
input_dir: str, msg_dir: str, prefix: str, suffix: str
) -> List[Tuple[str, str]]:
) -> list[tuple[str, str]]:
"""Get python input examples and output messages.

We use following conventions for input files and messages:
Expand Down
26 changes: 14 additions & 12 deletions pylint/testutils/lint_module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

from __future__ import annotations

import csv
import operator
import platform
Expand All @@ -10,7 +12,7 @@
from io import StringIO
from pathlib import Path
from typing import Counter as CounterType
from typing import Dict, List, Optional, TextIO, Tuple, Union
from typing import TextIO, Tuple

import pytest
from _pytest.config import Config
Expand All @@ -37,15 +39,15 @@ class LintModuleTest:
maxDiff = None

def __init__(
self, test_file: FunctionalTestFile, config: Optional[Config] = None
self, test_file: FunctionalTestFile, config: Config | None = None
) -> None:
_test_reporter = FunctionalTestReporter()
self._linter = PyLinter()
self._linter.namespace.persistent = 0
checkers.initialize(self._linter)

# See if test has its own .rc file, if so we use that one
rc_file: Union[Path, str] = PYLINTRC
rc_file: Path | str = PYLINTRC
try:
rc_file = test_file.option_file
self._linter.disable("suppressed-message")
Expand Down Expand Up @@ -140,7 +142,7 @@ def get_expected_messages(stream: TextIO) -> MessageCounter:
def multiset_difference(
expected_entries: MessageCounter,
actual_entries: MessageCounter,
) -> Tuple[MessageCounter, Dict[Tuple[int, str], int]]:
) -> tuple[MessageCounter, dict[tuple[int, str], int]]:
"""Takes two multisets and compares them.

A multiset is a dict with the cardinality of the key as the value.
Expand Down Expand Up @@ -168,7 +170,7 @@ def _open_source_file(self) -> TextIO:
return open(self._test_file.source, encoding="latin1")
return open(self._test_file.source, encoding="utf8")

def _get_expected(self) -> Tuple[MessageCounter, List[OutputLine]]:
def _get_expected(self) -> tuple[MessageCounter, list[OutputLine]]:
with self._open_source_file() as f:
expected_msgs = self.get_expected_messages(f)
if not expected_msgs:
Expand All @@ -180,8 +182,8 @@ def _get_expected(self) -> Tuple[MessageCounter, List[OutputLine]]:
]
return expected_msgs, expected_output_lines

def _get_actual(self) -> Tuple[MessageCounter, List[OutputLine]]:
messages: List[Message] = self._linter.reporter.messages
def _get_actual(self) -> tuple[MessageCounter, list[OutputLine]]:
messages: list[Message] = self._linter.reporter.messages
messages.sort(key=lambda m: (m.line, m.symbol, m.msg))
received_msgs: MessageCounter = Counter()
received_output_lines = []
Expand Down Expand Up @@ -212,7 +214,7 @@ def error_msg_for_unequal_messages(
self,
actual_messages: MessageCounter,
expected_messages: MessageCounter,
actual_output: List[OutputLine],
actual_output: list[OutputLine],
) -> str:
msg = [f'Wrong results for file "{self._test_file.base}":']
missing, unexpected = self.multiset_difference(
Expand All @@ -232,8 +234,8 @@ def error_msg_for_unequal_messages(

def error_msg_for_unequal_output(
self,
expected_lines: List[OutputLine],
received_lines: List[OutputLine],
expected_lines: list[OutputLine],
received_lines: list[OutputLine],
) -> str:
missing = set(expected_lines) - set(received_lines)
unexpected = set(received_lines) - set(expected_lines)
Expand All @@ -257,8 +259,8 @@ def error_msg_for_unequal_output(
def _check_output_text(
self,
_: MessageCounter,
expected_output: List[OutputLine],
actual_output: List[OutputLine],
expected_output: list[OutputLine],
actual_output: list[OutputLine],
) -> None:
"""This is a function because we want to be able to update the text in LintModuleOutputUpdate."""
assert expected_output == actual_output, self.error_msg_for_unequal_output(
Expand Down
Loading