Cleanup: update use of typing for tools/utils

This commit is contained in:
Campbell Barton
2024-10-22 16:44:21 +11:00
parent 0ae9ae736e
commit 5955e0566c
9 changed files with 54 additions and 84 deletions

View File

@@ -25,11 +25,8 @@ import subprocess
import sys
import time
from typing import (
List,
Optional,
from collections.abc import (
Sequence,
Tuple,
)
RE_ADDR = re.compile("\\[(0x[A-Fa-f0-9]+)\\]")
@@ -49,7 +46,7 @@ else:
sys.stdout.write("[{:s}]: {:s}\n".format(value_as_percentage(value_partial, value_final), info))
def find_gitroot(filepath_reference: str) -> Optional[str]:
def find_gitroot(filepath_reference: str) -> str | None:
path = filepath_reference
path_prev = ""
found = False
@@ -61,7 +58,7 @@ def find_gitroot(filepath_reference: str) -> Optional[str]:
return None
def addr2line_fn(arg_pair: Tuple[Tuple[str, str, bool], Sequence[str]]) -> Sequence[Tuple[str, str]]:
def addr2line_fn(arg_pair: tuple[tuple[str, str, bool], Sequence[str]]) -> Sequence[tuple[str, str]]:
shared_args, addr_list = arg_pair
(exe, base_path, time_command) = shared_args
cmd = (
@@ -77,7 +74,7 @@ def addr2line_fn(arg_pair: Tuple[Tuple[str, str, bool], Sequence[str]]) -> Seque
output = subprocess.check_output(cmd).rstrip().decode("utf-8", errors="surrogateescape")
output_lines = output.split("\n")
result: List[Tuple[str, str]] = []
result: list[tuple[str, str]] = []
while output_lines:
# Swap (function, line), to (line, function).

View File

@@ -24,10 +24,8 @@ import os
import sys
import unicodedata
from typing import (
Dict,
from collections.abc import (
Iterable,
List,
)
from git_log import (
@@ -119,11 +117,11 @@ class Credits:
)
def __init__(self) -> None:
self.users: Dict[str, CreditUser] = {}
self.users: dict[str, CreditUser] = {}
self.process_commits_count = 0
@classmethod
def commit_authors_get(cls, c: GitCommit) -> List[str]:
def commit_authors_get(cls, c: GitCommit) -> list[str]:
if (authors_overwrite := author_override_table.get(c.sha1, None)) is not None:
# Ignore git commit info for these having an entry in `author_override_table`.
return [author_table.get(author, author) for author in authors_overwrite]

View File

@@ -9,10 +9,10 @@ from os.path import join
from autopep8_clean_config import PATHS, PATHS_EXCLUDE
from typing import (
from collections.abc import (
Callable,
Generator,
Optional,
Iterator,
Sequence,
)
@@ -35,8 +35,8 @@ def is_source_and_included(filename: str) -> bool:
def path_iter(
path: str,
filename_check: Optional[Callable[[str], bool]] = None,
) -> Generator[str, None, None]:
filename_check: Callable[[str], bool] | None = None,
) -> Iterator[str]:
for dirpath, dirnames, filenames in os.walk(path):
# skip ".git"
dirnames[:] = [d for d in dirnames if not d.startswith(".")]
@@ -51,8 +51,8 @@ def path_iter(
def path_expand(
paths: Sequence[str],
filename_check: Optional[Callable[[str], bool]] = None,
) -> Generator[str, None, None]:
filename_check: Callable[[str], bool] | None = None,
) -> Iterator[str]:
for f in paths:
if not os.path.exists(f):
print("Missing:", f)

View File

@@ -4,14 +4,12 @@
import os
from typing import (
Generator,
from collections.abc import (
Callable,
Set,
Tuple,
Iterator,
)
PATHS: Tuple[str, ...] = (
PATHS: tuple[str, ...] = (
"build_files",
"doc",
"release/datafiles",
@@ -34,7 +32,7 @@ PATHS = tuple(
for p in PATHS
)
PATHS_EXCLUDE: Set[str] = set(
PATHS_EXCLUDE: set[str] = set(
os.path.join(SOURCE_DIR, p.replace("/", os.sep))
for p in
(
@@ -46,7 +44,7 @@ PATHS_EXCLUDE: Set[str] = set(
)
def files(path: str, test_fn: Callable[[str], bool]) -> Generator[str, None, None]:
def files(path: str, test_fn: Callable[[str], bool]) -> Iterator[str]:
for dirpath, dirnames, filenames in os.walk(path):
# skip '.git'
dirnames[:] = [d for d in dirnames if not d.startswith(".")]

View File

@@ -24,11 +24,8 @@ import os
import sys
import unicodedata
from typing import (
Dict,
Tuple,
from collections.abc import (
Iterable,
List,
)
from git_log import (
@@ -99,11 +96,11 @@ class Credits:
)
def __init__(self) -> None:
self.users: Dict[str, CreditUser] = {}
self.users: dict[str, CreditUser] = {}
self.process_commits_count = 0
@classmethod
def commit_authors_get(cls, c: GitCommit) -> List[str]:
def commit_authors_get(cls, c: GitCommit) -> list[str]:
if (authors_overwrite := author_override_table.get(c.sha1, None)) is not None:
# Ignore git commit info for these having an entry in `author_override_table`.
return [author_table.get(author, author) for author in authors_overwrite]
@@ -208,7 +205,7 @@ class Credits:
fh: io.TextIOWrapper,
*,
is_main_credits: bool = True,
contrib_companies: Tuple[str, ...] = (),
contrib_companies: tuple[str, ...] = (),
sort: str = "name",
use_email: bool = False,
) -> None:
@@ -256,7 +253,7 @@ class Credits:
filepath: str,
*,
is_main_credits: bool = True,
contrib_companies: Tuple[str, ...] = (),
contrib_companies: tuple[str, ...] = (),
sort: str = "name",
use_email: bool = False,
) -> None:

View File

@@ -12,12 +12,8 @@ __all__ = (
"canonical_author_map",
)
from typing import (
Dict,
)
def canonical_author_map() -> Dict[str, str]:
def canonical_author_map() -> dict[str, str]:
"""
Return a map of authors to their canonical author & email.
"""

View File

@@ -12,13 +12,8 @@ __all__ = (
"sha1_authors_map",
)
from typing import (
Dict,
Tuple,
)
def sha1_authors_map() -> Dict[bytes, Tuple[str, ...]]:
def sha1_authors_map() -> dict[bytes, tuple[str, ...]]:
"""
Return SHA1 to authors map.
"""

View File

@@ -9,14 +9,6 @@ import subprocess
import datetime
import re
from typing import (
List,
Union,
Optional,
Tuple,
)
_GIT_COMMIT_COAUTHORS_RE = re.compile(r"^Co-authored-by:[ \t]*([^\n]+)$", re.MULTILINE)
@@ -39,12 +31,12 @@ class GitCommit:
self.sha1 = sha1
self._git_dir = git_dir
self._author: Optional[str] = None
self._date: Optional[datetime.datetime] = None
self._body: Optional[str] = None
self._files: Optional[List[bytes]] = None
self._files_status: Optional[List[List[bytes]]] = None
self._diff: Optional[str] = None
self._author: str | None = None
self._date: datetime.datetime | None = None
self._body: str | None = None
self._files: list[bytes] | None = None
self._files_status: list[list[bytes]] | None = None
self._diff: str | None = None
def cache(self) -> None:
"""
@@ -61,11 +53,11 @@ class GitCommit:
self,
format: str,
*,
args_prefix: Tuple[Union[str, bytes], ...] = (),
args_suffix: Tuple[Union[str, bytes], ...] = (),
args_prefix: tuple[str | bytes, ...] = (),
args_suffix: tuple[str | bytes, ...] = (),
) -> bytes:
# sha1 = self.sha1.decode('ascii')
cmd: Tuple[Union[str, bytes], ...] = (
cmd: tuple[str | bytes, ...] = (
"git",
*args_prefix,
"--git-dir",
@@ -113,7 +105,7 @@ class GitCommit:
return ret
@property
def co_authors(self) -> List[str]:
def co_authors(self) -> list[str]:
authors = []
for author in _GIT_COMMIT_COAUTHORS_RE.findall(self.body):
if not ("<" in author and ">" in author):
@@ -145,7 +137,7 @@ class GitCommit:
return self.body.lstrip().partition("\n")[0]
@property
def files(self) -> List[bytes]:
def files(self) -> list[bytes]:
ret = self._files
if ret is None:
ret = [
@@ -159,7 +151,7 @@ class GitCommit:
return ret
@property
def files_status(self) -> List[List[bytes]]:
def files_status(self) -> list[list[bytes]]:
ret = self._files_status
if ret is None:
ret = [
@@ -199,7 +191,7 @@ class GitCommitIter:
self._path = path
self._git_dir = os.path.join(path, ".git")
self._sha1_range = sha1_range
self._process: Optional[subprocess.Popen[bytes]] = None
self._process: subprocess.Popen[bytes] | None = None
def __iter__(self) -> "GitCommitIter":
cmd = (

View File

@@ -23,17 +23,14 @@ import iso8601
from typing import (
cast,
Callable,
Dict,
Iterable,
List,
NewType,
Optional,
Tuple,
Type,
TypeVar,
Union,
)
from collections.abc import (
Callable,
Iterable,
)
from requests.structures import CaseInsensitiveDict
logger = logging.getLogger(__file__)
@@ -65,7 +62,7 @@ retry: Callable[[F], F] = retry_decorator(
tries=10, delay=1, backoff=2, logger=logger)
def assert_cast(typ: Type[T], obj: object) -> T:
def assert_cast(typ: type[T], obj: object) -> T:
assert isinstance(obj, typ), f'object is not of type {typ}: {obj}'
# NOTE: MYPY warns the cast is redundant, we might consider removing it.
return cast(T, obj) # type: ignore
@@ -80,7 +77,7 @@ def get_date_object(date_string: str) -> datetime.datetime:
results_per_page = 25
def get_next_page(headers: CaseInsensitiveDict[str], page: Page) -> Optional[Page]:
def get_next_page(headers: CaseInsensitiveDict[str], page: Page) -> Page | None:
"""
Parse the header looking for reference to next.
"""
@@ -100,9 +97,9 @@ def fetch_single(
api_url: yarl.URL,
api_token: str,
method: str,
data: Dict[str, str],
data: dict[str, str],
page: Page,
) -> Tuple[List[object], Optional[Page]]:
) -> tuple[list[object], Page | None]:
"""Generic function to query a single item from the API.
Returns:
@@ -113,7 +110,7 @@ def fetch_single(
'Authorization': 'token ' + api_token,
}
params: Dict[str, Union[str, int]] = {
params: dict[str, str | int] = {
'limit': results_per_page,
'page': page,
**data,
@@ -132,14 +129,14 @@ def fetch_all(
api_url: yarl.URL,
api_token: str,
method: str,
data: Dict[str, str],
data: dict[str, str],
) -> Iterable[object]:
"""Generic function to query lists from API.
Yields:
response_data - the result of fetch_single()
"""
page = Page(1)
page: Page | None = Page(1)
while page is not None:
response_data, page = fetch_single(api_url, api_token, method, data, page)
yield from response_data if response_data is not None else ()
@@ -150,7 +147,7 @@ def fetch_team_members(
api_token: str,
organization_name: str,
team_name: str,
) -> List[TeamMember]:
) -> list[TeamMember]:
"""Query API for all the members of a team.
Yields:
@@ -161,7 +158,7 @@ def fetch_team_members(
team_id = None
for team in cast(
Iterable[Dict[object, object]],
Iterable[dict[object, object]],
fetch_all(
api_url,
api_token,
@@ -183,7 +180,7 @@ def fetch_team_members(
users = list()
for member in cast(
Iterable[Dict[object, object]],
Iterable[dict[object, object]],
fetch_all(
api_url,
api_token,