Skip to content

Commit

Permalink
Use python-typing-update on pylint/extensions directory (#6308)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord authored Apr 14, 2022
1 parent 99ef057 commit 47e168c
Show file tree
Hide file tree
Showing 22 changed files with 125 additions and 77 deletions.
4 changes: 3 additions & 1 deletion pylint/extensions/__init__.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

from typing import TYPE_CHECKING

from pylint.utils import register_plugins
Expand All @@ -10,7 +12,7 @@
from pylint.lint import PyLinter


def initialize(linter: "PyLinter") -> None:
def initialize(linter: PyLinter) -> None:
"""Initialize linter with checkers in the extensions' directory."""
register_plugins(linter, __path__[0])

Expand Down
15 changes: 8 additions & 7 deletions pylint/extensions/_check_docs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

"""Utility methods for docstring checking."""

from __future__ import annotations

import re
from typing import List, Optional, Set, Tuple

import astroid
from astroid import nodes
Expand Down Expand Up @@ -95,12 +96,12 @@ def _get_raise_target(node):
return None


def _split_multiple_exc_types(target: str) -> List[str]:
def _split_multiple_exc_types(target: str) -> list[str]:
delimiters = r"(\s*,(?:\s*or\s)?\s*|\s+or\s+)"
return re.split(delimiters, target)


def possible_exc_types(node: nodes.NodeNG) -> Set[nodes.ClassDef]:
def possible_exc_types(node: nodes.NodeNG) -> set[nodes.ClassDef]:
"""Gets all the possible raised exception types for the given raise node.
.. note::
Expand Down Expand Up @@ -157,8 +158,8 @@ def possible_exc_types(node: nodes.NodeNG) -> Set[nodes.ClassDef]:


def docstringify(
docstring: Optional[nodes.Const], default_type: str = "default"
) -> "Docstring":
docstring: nodes.Const | None, default_type: str = "default"
) -> Docstring:
best_match = (0, DOCSTRING_TYPES.get(default_type, Docstring)(docstring))
for docstring_type in (
SphinxDocstring,
Expand Down Expand Up @@ -190,7 +191,7 @@ class Docstring:

# These methods are designed to be overridden
# pylint: disable=no-self-use
def __init__(self, doc: Optional[nodes.Const]) -> None:
def __init__(self, doc: nodes.Const | None) -> None:
docstring = doc.value if doc else ""
self.doc = docstring.expandtabs()

Expand Down Expand Up @@ -768,7 +769,7 @@ class NumpyDocstring(GoogleDocstring):

supports_yields = True

def match_param_docs(self) -> Tuple[Set[str], Set[str]]:
def match_param_docs(self) -> tuple[set[str], set[str]]:
"""Matches parameter documentation section to parameter documentation rules."""
params_with_doc = set()
params_with_type = set()
Expand Down
5 changes: 4 additions & 1 deletion pylint/extensions/bad_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

"""Checker for deprecated builtins."""

from __future__ import annotations

from typing import TYPE_CHECKING

from astroid import nodes
Expand Down Expand Up @@ -61,5 +64,5 @@ def visit_call(self, node: nodes.Call) -> None:
self.add_message("bad-builtin", node=node, args=args)


def register(linter: "PyLinter") -> None:
def register(linter: PyLinter) -> None:
linter.register_checker(BadBuiltinChecker(linter))
9 changes: 6 additions & 3 deletions pylint/extensions/broad_try_clause.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

"""Looks for try/except statements with too much code in the try clause."""
from typing import TYPE_CHECKING, Union

from __future__ import annotations

from typing import TYPE_CHECKING

from astroid import nodes

Expand Down Expand Up @@ -55,7 +58,7 @@ def _count_statements(self, try_node):

return statement_count

def visit_tryexcept(self, node: Union[nodes.TryExcept, nodes.TryFinally]) -> None:
def visit_tryexcept(self, node: nodes.TryExcept | nodes.TryFinally) -> None:
try_clause_statements = self._count_statements(node)
if try_clause_statements > self.linter.namespace.max_try_statements:
msg = f"try clause contains {try_clause_statements} statements, expected at most {self.linter.namespace.max_try_statements}"
Expand All @@ -67,5 +70,5 @@ def visit_tryfinally(self, node: nodes.TryFinally) -> None:
self.visit_tryexcept(node)


def register(linter: "PyLinter") -> None:
def register(linter: PyLinter) -> None:
linter.register_checker(BroadTryClauseChecker(linter))
4 changes: 3 additions & 1 deletion pylint/extensions/check_elif.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

from typing import TYPE_CHECKING

from astroid import nodes
Expand Down Expand Up @@ -57,5 +59,5 @@ def visit_if(self, node: nodes.If) -> None:
self.add_message("else-if-used", node=node, confidence=HIGH)


def register(linter: "PyLinter") -> None:
def register(linter: PyLinter) -> None:
linter.register_checker(ElseifUsedChecker(linter))
22 changes: 12 additions & 10 deletions pylint/extensions/code_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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 sys
from typing import TYPE_CHECKING, List, Optional, Set, Tuple, Type, Union, cast
from typing import TYPE_CHECKING, Tuple, Type, Union, cast

from astroid import nodes

Expand Down Expand Up @@ -125,7 +127,7 @@ def _check_dict_consider_namedtuple_dataclass(self, node: nodes.Dict) -> None:
KeyTupleT = Tuple[Type[nodes.NodeNG], str]

# Makes sure all keys are 'Const' string nodes
keys_checked: Set[KeyTupleT] = set()
keys_checked: set[KeyTupleT] = set()
for _, dict_value in node.items:
dict_value = cast(nodes.Dict, dict_value)
for key, _ in dict_value.items:
Expand All @@ -141,13 +143,13 @@ def _check_dict_consider_namedtuple_dataclass(self, node: nodes.Dict) -> None:
keys_checked.add(key_tuple)

# Makes sure all subdicts have at least 1 common key
key_tuples: List[Tuple[KeyTupleT, ...]] = []
key_tuples: list[tuple[KeyTupleT, ...]] = []
for _, dict_value in node.items:
dict_value = cast(nodes.Dict, dict_value)
key_tuples.append(
tuple((type(key), key.as_string()) for key, _ in dict_value.items)
)
keys_intersection: Set[KeyTupleT] = set(key_tuples[0])
keys_intersection: set[KeyTupleT] = set(key_tuples[0])
for sub_key_tuples in key_tuples[1:]:
keys_intersection.intersection_update(sub_key_tuples)
if not keys_intersection:
Expand Down Expand Up @@ -194,7 +196,7 @@ def _check_consider_using_assignment_expr(self, node: nodes.If) -> None:
Note: Assignment expressions were added in Python 3.8
"""
# Check if `node.test` contains a `Name` node
node_name: Optional[nodes.Name] = None
node_name: nodes.Name | None = None
if isinstance(node.test, nodes.Name):
node_name = node.test
elif (
Expand Down Expand Up @@ -249,8 +251,8 @@ def _check_consider_using_assignment_expr(self, node: nodes.If) -> None:

@staticmethod
def _check_prev_sibling_to_if_stmt(
prev_sibling: Optional[nodes.NodeNG], name: Optional[str]
) -> TypeGuard[Union[nodes.Assign, nodes.AnnAssign]]:
prev_sibling: nodes.NodeNG | None, name: str | None
) -> TypeGuard[nodes.Assign | nodes.AnnAssign]:
"""Check if previous sibling is an assignment with the same name.
Ignore statements which span multiple lines.
Expand All @@ -275,15 +277,15 @@ def _check_prev_sibling_to_if_stmt(

@staticmethod
def _check_ignore_assignment_expr_suggestion(
node: nodes.If, name: Optional[str]
node: nodes.If, name: str | None
) -> bool:
"""Return True if suggestion for assignment expr should be ignored.
E.g., in cases where a match statement would be a better fit
(multiple conditions).
"""
if isinstance(node.test, nodes.Compare):
next_if_node: Optional[nodes.If] = None
next_if_node: nodes.If | None = None
next_sibling = node.next_sibling()
if len(node.orelse) == 1 and isinstance(node.orelse[0], nodes.If):
# elif block
Expand All @@ -306,5 +308,5 @@ def _check_ignore_assignment_expr_suggestion(
return False


def register(linter: "PyLinter") -> None:
def register(linter: PyLinter) -> None:
linter.register_checker(CodeStyleChecker(linter))
4 changes: 3 additions & 1 deletion pylint/extensions/comparetozero.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

"""Looks for comparisons to zero."""

from __future__ import annotations

import itertools
from typing import TYPE_CHECKING, Any, Iterable

Expand Down Expand Up @@ -72,5 +74,5 @@ def visit_compare(self, node: nodes.Compare) -> None:
self.add_message("compare-to-zero", node=node)


def register(linter: "PyLinter") -> None:
def register(linter: PyLinter) -> None:
linter.register_checker(CompareToZeroChecker(linter))
3 changes: 2 additions & 1 deletion pylint/extensions/comparison_placement.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
See https://en.wikipedia.org/wiki/Yoda_conditions
"""

from __future__ import annotations

from typing import TYPE_CHECKING

Expand Down Expand Up @@ -67,5 +68,5 @@ def visit_compare(self, node: nodes.Compare) -> None:
self._check_misplaced_constant(node, left, right, operator)


def register(linter: "PyLinter") -> None:
def register(linter: PyLinter) -> None:
linter.register_checker(MisplacedComparisonConstantChecker(linter))
4 changes: 3 additions & 1 deletion pylint/extensions/confusing_elif.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

from typing import TYPE_CHECKING

from astroid import nodes
Expand Down Expand Up @@ -48,5 +50,5 @@ def _has_no_else_clause(node: nodes.If):
return False


def register(linter: "PyLinter") -> None:
def register(linter: PyLinter) -> None:
linter.register_checker(ConfusingConsecutiveElifChecker(linter))
4 changes: 3 additions & 1 deletion pylint/extensions/consider_ternary_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

"""Check for if / assign blocks that can be rewritten with if-expressions."""

from __future__ import annotations

from typing import TYPE_CHECKING

from astroid import nodes
Expand Down Expand Up @@ -53,5 +55,5 @@ def visit_if(self, node: nodes.If) -> None:
self.add_message("consider-ternary-expression", node=node)


def register(linter: "PyLinter") -> None:
def register(linter: PyLinter) -> None:
linter.register_checker(ConsiderTernaryExpressionChecker(linter))
23 changes: 13 additions & 10 deletions pylint/extensions/docparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

"""Pylint plugin for checking in Sphinx, Google, or Numpy style docstrings."""

from __future__ import annotations

import re
from typing import TYPE_CHECKING, Optional, Set
from typing import TYPE_CHECKING

import astroid
from astroid import nodes
Expand Down Expand Up @@ -366,10 +369,10 @@ def visit_yieldfrom(self, node: nodes.YieldFrom) -> None:

def _compare_missing_args(
self,
found_argument_names: Set[str],
found_argument_names: set[str],
message_id: str,
not_needed_names: Set[str],
expected_argument_names: Set[str],
not_needed_names: set[str],
expected_argument_names: set[str],
warning_node: nodes.NodeNG,
) -> None:
"""Compare the found argument names with the expected ones and
Expand Down Expand Up @@ -405,10 +408,10 @@ def _compare_missing_args(

def _compare_different_args(
self,
found_argument_names: Set[str],
found_argument_names: set[str],
message_id: str,
not_needed_names: Set[str],
expected_argument_names: Set[str],
not_needed_names: set[str],
expected_argument_names: set[str],
warning_node: nodes.NodeNG,
) -> None:
"""Compare the found argument names with the expected ones and
Expand All @@ -425,7 +428,7 @@ def _compare_different_args(
:param warning_node: The node to be analyzed
"""
# Handle variadic and keyword args without asterisks
modified_expected_argument_names: Set[str] = set()
modified_expected_argument_names: set[str] = set()
for name in expected_argument_names:
if name.replace("*", "") in found_argument_names:
modified_expected_argument_names.add(name.replace("*", ""))
Expand Down Expand Up @@ -481,7 +484,7 @@ def check_arguments_in_docstring(
doc: Docstring,
arguments_node: astroid.Arguments,
warning_node: astroid.NodeNG,
accept_no_param_doc: Optional[bool] = None,
accept_no_param_doc: bool | None = None,
):
"""Check that all parameters are consistent with the parameters mentioned
in the parameter documentation (e.g. the Sphinx tags 'param' and 'type').
Expand Down Expand Up @@ -652,5 +655,5 @@ def _add_raise_message(self, missing_excs, node):
)


def register(linter: "PyLinter") -> None:
def register(linter: PyLinter) -> None:
linter.register_checker(DocstringParameterChecker(linter))
4 changes: 3 additions & 1 deletion pylint/extensions/docstyle.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 linecache
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -82,5 +84,5 @@ def _check_docstring(self, node_type, node):
)


def register(linter: "PyLinter") -> None:
def register(linter: PyLinter) -> None:
linter.register_checker(DocStringStyleChecker(linter))
4 changes: 3 additions & 1 deletion pylint/extensions/empty_comment.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

from typing import TYPE_CHECKING

from astroid import nodes
Expand Down Expand Up @@ -62,5 +64,5 @@ def process_module(self, node: nodes.Module) -> None:
self.add_message("empty-comment", line=line_num + 1)


def register(linter: "PyLinter") -> None:
def register(linter: PyLinter) -> None:
linter.register_checker(CommentChecker(linter))
4 changes: 3 additions & 1 deletion pylint/extensions/emptystring.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

"""Looks for comparisons to empty string."""

from __future__ import annotations

import itertools
from typing import TYPE_CHECKING, Any, Iterable

Expand Down Expand Up @@ -67,5 +69,5 @@ def visit_compare(self, node: nodes.Compare) -> None:
self.add_message("compare-to-empty-string", node=node)


def register(linter: "PyLinter") -> None:
def register(linter: PyLinter) -> None:
linter.register_checker(CompareToEmptyStringChecker(linter))
Loading

0 comments on commit 47e168c

Please sign in to comment.