cmake_consistency_check: use type hints

'mypy --strict' passes without errors.
This commit is contained in:
Campbell Barton 2021-04-12 12:19:27 +10:00
parent cf8773b525
commit b60c168e43
Notes: blender-bot 2023-02-14 06:42:53 +01:00
Referenced by issue #87409, Current state of type hinting:
1 changed files with 38 additions and 24 deletions

View File

@ -28,6 +28,14 @@ if sys.version_info.major < 3:
sys.version.partition(" ")[0])
sys.exit(1)
import os
from os.path import (
dirname,
join,
normpath,
splitext,
)
from cmake_consistency_check_config import (
IGNORE_SOURCE,
IGNORE_SOURCE_MISSING,
@ -37,32 +45,34 @@ from cmake_consistency_check_config import (
BUILD_DIR,
)
import os
from os.path import (
dirname,
join,
normpath,
splitext,
from typing import (
Callable,
Dict,
Generator,
List,
Optional,
Tuple,
)
global_h = set()
global_c = set()
global_refs = {}
global_refs: Dict[str, List[Tuple[str, int]]] = {}
# Flatten `IGNORE_SOURCE_MISSING` to avoid nested looping.
IGNORE_SOURCE_MISSING = [
IGNORE_SOURCE_MISSING_FLAT = [
(k, ignore_path) for k, ig_list in IGNORE_SOURCE_MISSING
for ignore_path in ig_list
]
# Ignore cmake file, path pairs.
global_ignore_source_missing = {}
for k, v in IGNORE_SOURCE_MISSING:
global_ignore_source_missing: Dict[str, List[str]] = {}
for k, v in IGNORE_SOURCE_MISSING_FLAT:
global_ignore_source_missing.setdefault(k, []).append(v)
del IGNORE_SOURCE_MISSING_FLAT
def replace_line(f, i, text, keep_indent=True):
def replace_line(f: str, i: int, text: str, keep_indent: bool = True) -> None:
file_handle = open(f, 'r')
data = file_handle.readlines()
file_handle.close()
@ -77,7 +87,10 @@ def replace_line(f, i, text, keep_indent=True):
file_handle.close()
def source_list(path, filename_check=None):
def source_list(
path: str,
filename_check: Optional[Callable[[str], bool]] = None,
) -> Generator[str, None, None]:
for dirpath, dirnames, filenames in os.walk(path):
# skip '.git'
dirnames[:] = [d for d in dirnames if not d.startswith(".")]
@ -88,37 +101,37 @@ def source_list(path, filename_check=None):
# extension checking
def is_cmake(filename):
def is_cmake(filename: str) -> bool:
ext = splitext(filename)[1]
return (ext == ".cmake") or (filename == "CMakeLists.txt")
def is_c_header(filename):
def is_c_header(filename: str) -> bool:
ext = splitext(filename)[1]
return (ext in {".h", ".hpp", ".hxx", ".hh"})
def is_c(filename):
def is_c(filename: str) -> bool:
ext = splitext(filename)[1]
return (ext in {".c", ".cpp", ".cxx", ".m", ".mm", ".rc", ".cc", ".inl"})
def is_c_any(filename):
def is_c_any(filename: str) -> bool:
return is_c(filename) or is_c_header(filename)
def cmake_get_src(f):
def cmake_get_src(f: str) -> None:
sources_h = []
sources_c = []
filen = open(f, "r", encoding="utf8")
it = iter(filen)
it: Optional[Iterator[str]] = iter(filen)
found = False
i = 0
# print(f)
def is_definition(l, f, i, name):
def is_definition(l: str, f: str, i: int, name: str) -> bool:
if l.startswith("unset("):
return False
@ -131,6 +144,7 @@ def cmake_get_src(f):
if l.endswith(")"):
raise Exception("strict formatting not kept 'list(APPEND %s...)' on 1 line %s:%d" % (name, f, i))
return True
return False
while it is not None:
context_name = ""
@ -269,7 +283,7 @@ def cmake_get_src(f):
filen.close()
def is_ignore_source(f, ignore_used):
def is_ignore_source(f: str, ignore_used: List[bool]) -> bool:
for index, ignore_path in enumerate(IGNORE_SOURCE):
if ignore_path in f:
ignore_used[index] = True
@ -277,7 +291,7 @@ def is_ignore_source(f, ignore_used):
return False
def is_ignore_cmake(f, ignore_used):
def is_ignore_cmake(f: str, ignore_used: List[bool]) -> bool:
for index, ignore_path in enumerate(IGNORE_CMAKE):
if ignore_path in f:
ignore_used[index] = True
@ -285,7 +299,7 @@ def is_ignore_cmake(f, ignore_used):
return False
def main():
def main() -> None:
print("Scanning:", SOURCE_DIR)
@ -359,7 +373,7 @@ def main():
if "extern" not in f:
i = 1
try:
for l in open(f, "r", encoding="utf8"):
for _ in open(f, "r", encoding="utf8"):
i += 1
except UnicodeDecodeError:
print("Non utf8: %s:%d" % (f, i))