Skip to content
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
24 changes: 12 additions & 12 deletions cpp_linter/clang_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path
import re
import subprocess
from typing import Optional, List, Dict, Tuple, cast
from typing import cast
import shutil

from ..common_fs import FileObj, FileIOTimeout
Expand All @@ -14,7 +14,7 @@
from ..cli import Args


def assemble_version_exec(tool_name: str, specified_version: str) -> Optional[str]:
def assemble_version_exec(tool_name: str, specified_version: str) -> str | None:
"""Assembles the command to the executable of the given clang tool based on given
version information.

Expand All @@ -37,13 +37,13 @@ def assemble_version_exec(tool_name: str, specified_version: str) -> Optional[st
def _run_on_single_file(
file: FileObj,
log_lvl: int,
tidy_cmd: Optional[str],
db_json: Optional[List[Dict[str, str]]],
format_cmd: Optional[str],
format_filter: Optional[FormatFileFilter],
tidy_filter: Optional[TidyFileFilter],
tidy_cmd: str | None,
db_json: list[dict[str, str]] | None,
format_cmd: str | None,
format_filter: FormatFileFilter | None,
tidy_filter: TidyFileFilter | None,
args: Args,
) -> Tuple[str, str, Optional[TidyAdvice], Optional[FormatAdvice]]:
) -> tuple[str, str, TidyAdvice | None, FormatAdvice | None]:
log_stream = worker_log_init(log_lvl)
filename = Path(file.name).as_posix()

Expand Down Expand Up @@ -115,11 +115,11 @@ def _capture_tool_version(cmd: str) -> str:

class ClangVersions:
def __init__(self) -> None:
self.tidy: Optional[str] = None
self.format: Optional[str] = None
self.tidy: str | None = None
self.format: str | None = None


def capture_clang_tools_output(files: List[FileObj], args: Args) -> ClangVersions:
def capture_clang_tools_output(files: list[FileObj], args: Args) -> ClangVersions:
"""Execute and capture all output from clang-tidy and clang-format. This aggregates
results in the :attr:`~cpp_linter.Globals.OUTPUT`.

Expand Down Expand Up @@ -150,7 +150,7 @@ def capture_clang_tools_output(files: List[FileObj], args: Args) -> ClangVersion
ignore_value=args.ignore_tidy,
)

db_json: Optional[List[Dict[str, str]]] = None
db_json: list[dict[str, str]] | None = None
if args.database:
db = Path(args.database)
if not db.is_absolute():
Expand Down
12 changes: 6 additions & 6 deletions cpp_linter/clang_tools/clang_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pathlib import PurePath
import subprocess
from typing import List, cast
from typing import cast

import xml.etree.ElementTree as ET

Expand Down Expand Up @@ -45,7 +45,7 @@ def __init__(self, line_numb: int):
self.line = line_numb

#: A list of `FormatReplacement` object(s) representing suggestions.
self.replacements: List[FormatReplacement] = []
self.replacements: list[FormatReplacement] = []

def __repr__(self):
return (
Expand All @@ -66,7 +66,7 @@ def __init__(self, filename: str):
#: The source file that the suggestion concerns.
self.filename = PurePath(filename).as_posix()

self.replaced_lines: List[FormatReplacementLine] = []
self.replaced_lines: list[FormatReplacementLine] = []
"""A list of `FormatReplacementLine` representing replacement(s)
on a single line."""

Expand All @@ -85,7 +85,7 @@ def get_tool_name(self) -> str:
return "clang-format"


def tally_format_advice(files: List[FileObj]) -> int:
def tally_format_advice(files: list[FileObj]) -> int:
"""Returns the sum of clang-format errors"""
format_checks_failed = 0
for file_obj in files:
Expand Down Expand Up @@ -126,7 +126,7 @@ def parse_format_replacements_xml(
if not xml_out:
return format_advice
ranges = cast(
List[List[int]],
list[list[int]],
file_obj.range_of_changed_lines(lines_changed_only, get_ranges=True),
)
tree = ET.fromstring(xml_out)
Expand Down Expand Up @@ -183,7 +183,7 @@ def run_clang_format(
"--output-replacements-xml",
]
ranges = cast(
List[List[int]],
list[list[int]],
file_obj.range_of_changed_lines(lines_changed_only, get_ranges=True),
)
for span in ranges:
Expand Down
22 changes: 11 additions & 11 deletions cpp_linter/clang_tools/clang_tidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib import Path, PurePath
import re
import subprocess
from typing import Tuple, Union, List, cast, Optional, Dict, Set
from typing import cast
from ..loggers import logger
from ..common_fs import FileObj
from .patcher import PatchMixin, ReviewComments, Suggestion
Expand All @@ -30,8 +30,8 @@ class TidyNotification:

def __init__(
self,
notification_line: Tuple[str, Union[int, str], Union[int, str], str, str, str],
database: Optional[List[Dict[str, str]]] = None,
notification_line: tuple[str, int | str, int | str, str, str, str],
database: list[dict[str, str]] | None = None,
):
# logger.debug("Creating tidy note from line %s", notification_line)
(
Expand Down Expand Up @@ -78,9 +78,9 @@ def __init__(
#: The source filename concerning the notification.
self.filename = rel_path
#: A `list` of lines for the code-block in the notification.
self.fixit_lines: List[str] = []
self.fixit_lines: list[str] = []
#: A list of line numbers where a suggested fix was applied.
self.applied_fixes: Set[int] = set()
self.applied_fixes: set[int] = set()

@property
def diagnostic_link(self) -> str:
Expand All @@ -105,7 +105,7 @@ def __repr__(self) -> str:


class TidyAdvice(PatchMixin):
def __init__(self, notes: List[TidyNotification]) -> None:
def __init__(self, notes: list[TidyNotification]) -> None:
#: A buffer of the applied fixes from clang-tidy
super().__init__()
self.notes = notes
Expand Down Expand Up @@ -174,7 +174,7 @@ def _has_related_suggestion(suggestion: Suggestion) -> bool:
review_comments.suggestions.append(suggestion)


def tally_tidy_advice(files: List[FileObj]) -> int:
def tally_tidy_advice(files: list[FileObj]) -> int:
"""Returns the sum of clang-format errors"""
tidy_checks_failed = 0
for file_obj in files:
Expand All @@ -194,8 +194,8 @@ def run_clang_tidy(
checks: str,
lines_changed_only: int,
database: str,
extra_args: List[str],
db_json: Optional[List[Dict[str, str]]],
extra_args: list[str],
db_json: list[dict[str, str]] | None,
tidy_review: bool,
style: str,
) -> TidyAdvice:
Expand Down Expand Up @@ -277,7 +277,7 @@ def run_clang_tidy(


def parse_tidy_output(
tidy_out: str, database: Optional[List[Dict[str, str]]]
tidy_out: str, database: list[dict[str, str]] | None
) -> TidyAdvice:
"""Parse clang-tidy stdout.

Expand All @@ -300,7 +300,7 @@ def parse_tidy_output(
if " " not in diagnostic_name and "-" in diagnostic_name:
notification = TidyNotification(
cast(
Tuple[str, Union[int, str], Union[int, str], str, str, str],
tuple[str, int | str, int | str, str, str, str],
note_match.groups(),
),
database,
Expand Down
18 changes: 9 additions & 9 deletions cpp_linter/clang_tools/patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
by the clang tool's output."""

from abc import ABC
from typing import Optional, Dict, Any, List, Tuple
from typing import Any
from pygit2 import Patch # type: ignore
from ..common_fs import FileObj
from pygit2.enums import DiffOption # type: ignore
Expand All @@ -27,7 +27,7 @@ def __init__(self, file_name: str) -> None:
#: The markdown comment about the suggestion.
self.comment: str = ""

def serialize_to_github_payload(self) -> Dict[str, Any]:
def serialize_to_github_payload(self) -> dict[str, Any]:
"""Serialize this object into a JSON compatible with Github's REST API."""
assert self.line_end > 0, "ending line number unknown"
from ..rest_api import COMMENT_MARKER # workaround circular import
Expand All @@ -47,9 +47,9 @@ class ReviewComments:

def __init__(self) -> None:
#: The list of actual comments
self.suggestions: List[Suggestion] = []
self.suggestions: list[Suggestion] = []

self.tool_total: Dict[str, Optional[int]] = {
self.tool_total: dict[str, int | None] = {
"clang-tidy": None,
"clang-format": None,
}
Expand All @@ -62,7 +62,7 @@ def __init__(self) -> None:
A `None` value means a review was not requested from the corresponding tool.
"""

self.full_patch: Dict[str, str] = {"clang-tidy": "", "clang-format": ""}
self.full_patch: dict[str, str] = {"clang-tidy": "", "clang-format": ""}
"""The full patch of all the suggestions (including those that will not
fit within the diff)"""

Expand All @@ -84,9 +84,9 @@ def merge_similar_suggestion(self, suggestion: Suggestion) -> bool:
def serialize_to_github_payload(
# avoid circular imports by accepting primitive types (instead of ClangVersions)
self,
tidy_version: Optional[str],
format_version: Optional[str],
) -> Tuple[str, List[Dict[str, Any]]]:
tidy_version: str | None,
format_version: str | None,
) -> tuple[str, list[dict[str, Any]]]:
"""Serialize this object into a summary and list of comments compatible
with Github's REST API.

Expand Down Expand Up @@ -143,7 +143,7 @@ class PatchMixin(ABC):

def __init__(self) -> None:
#: A unified diff of the applied fixes from the clang tool's output
self.patched: Optional[bytes] = None
self.patched: bytes | None = None

def get_suggestion_help(self, start, end) -> str:
"""Create helpful text about what the suggestion aims to fix.
Expand Down
18 changes: 9 additions & 9 deletions cpp_linter/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import argparse
from collections import UserDict
from typing import Optional, List, Dict, Any, Sequence, Union
from typing import Any, Sequence


class Args(UserDict):
Expand All @@ -14,7 +14,7 @@ class Args(UserDict):
#: See :std:option:`--database`.
database: str = ""
#: See :std:option:`--diff-base`.
diff_base: Optional[Union[int, str]] = None
diff_base: int | str | None = None
#: See :std:option:`--ignore-index`.
ignore_index: bool = False
#: See :std:option:`--style`.
Expand All @@ -27,7 +27,7 @@ class Args(UserDict):
#: See :std:option:`--version`.
version: str = ""
#: See :std:option:`--extensions`.
extensions: List[str] = [
extensions: list[str] = [
"c",
"h",
"C",
Expand Down Expand Up @@ -56,28 +56,28 @@ class Args(UserDict):
#: See :std:option:`--file-annotations`.
file_annotations: bool = True
#: See :std:option:`--extra-arg`.
extra_arg: List[str] = []
extra_arg: list[str] = []
#: See :std:option:`--no-lgtm`.
no_lgtm: bool = True
#: See :std:option:`files`.
files: List[str] = []
files: list[str] = []
#: See :std:option:`--tidy-review`.
tidy_review: bool = False
#: See :std:option:`--format-review`.
format_review: bool = False
#: See :std:option:`--jobs`.
jobs: Optional[int] = 1
jobs: int | None = 1
#: See :std:option:`--ignore-tidy`.
ignore_tidy: str = ""
#: See :std:option:`--ignore-format`.
ignore_format: str = ""
#: See :std:option:`--passive-reviews`.
passive_reviews: bool = False
#: A subcommand if provided
command: Optional[str] = None
command: str | None = None


_parser_args: Dict[Sequence[str], Any] = {}
_parser_args: dict[Sequence[str], Any] = {}
_parser_args[("-v", "--verbosity")] = dict(
type=lambda a: a.lower() in ["debug", "10"],
default="info",
Expand Down Expand Up @@ -388,7 +388,7 @@ class Args(UserDict):
)


def _parse_jobs(val: str) -> Optional[int]:
def _parse_jobs(val: str) -> int | None:
try:
jobs = int(val)
except ValueError as exc:
Expand Down
Loading
Loading