From 3d70935d5c5a441dfe3d81ec3372f405f1ae1ba2 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Sun, 7 Apr 2024 13:21:28 -0400 Subject: [PATCH] chore: integrate types and typecheck (#482) Signed-off-by: Henry Schreiner --- .pre-commit-config.yaml | 1 - src/cmake/__init__.py | 25 ++++++++++++++++++------- src/cmake/__init__.pyi | 15 --------------- 3 files changed, 18 insertions(+), 23 deletions(-) delete mode 100644 src/cmake/__init__.pyi diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index af8726a8e..a4a3546c1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -28,6 +28,5 @@ repos: hooks: - id: mypy files: ^(src|scripts) - exclude: ^src/cmake/__init__.py$ additional_dependencies: [types-requests] args: [] diff --git a/src/cmake/__init__.py b/src/cmake/__init__.py index cefd53bf3..84df7e0d1 100644 --- a/src/cmake/__init__.py +++ b/src/cmake/__init__.py @@ -1,6 +1,9 @@ +from __future__ import annotations + import os import subprocess import sys +from pathlib import Path if sys.version_info < (3, 8): from importlib_metadata import distribution @@ -9,17 +12,25 @@ from ._version import version as __version__ +TYPE_CHECKING = False + +if TYPE_CHECKING: + from typing import Iterable, NoReturn + + __all__ = ["__version__", "CMAKE_DATA", "CMAKE_BIN_DIR", "CMAKE_DOC_DIR", "CMAKE_SHARE_DIR", "cmake", "cpack", "ctest"] -def __dir__(): +def __dir__() -> list[str]: return __all__ cmake_executable_path = None -for script in distribution("cmake").files: +cmake_files = distribution("cmake").files +assert cmake_files is not None, "This is the cmake package so it must be installed and have files" +for script in cmake_files: if str(script).startswith("cmake/data/bin/cmake"): - resolved_script = script.locate().resolve(strict=True) + resolved_script = Path(script.locate()).resolve(strict=True) cmake_executable_path = resolved_script.parents[1] break CMAKE_DATA = str(cmake_executable_path) if cmake_executable_path else None @@ -32,17 +43,17 @@ def __dir__(): CMAKE_SHARE_DIR = os.path.join(CMAKE_DATA, 'share') -def _program(name, args): +def _program(name: str, args: Iterable[str]) -> int: return subprocess.call([os.path.join(CMAKE_BIN_DIR, name), *args], close_fds=False) -def cmake(): +def cmake() -> NoReturn: raise SystemExit(_program('cmake', sys.argv[1:])) -def cpack(): +def cpack() -> NoReturn: raise SystemExit(_program('cpack', sys.argv[1:])) -def ctest(): +def ctest() -> NoReturn: raise SystemExit(_program('ctest', sys.argv[1:])) diff --git a/src/cmake/__init__.pyi b/src/cmake/__init__.pyi deleted file mode 100644 index ffa3ffe93..000000000 --- a/src/cmake/__init__.pyi +++ /dev/null @@ -1,15 +0,0 @@ -from typing import Iterator, NoReturn - -__version__: str - -CMAKE_DATA: str - -CMAKE_BIN_DIR: str -CMAKE_DOC_DIR: str -CMAKE_SHARE_DIR: str - -def _program(name: str, args: Iterator[str]) -> int: ... - -def cmake() -> NoReturn: ... -def cpack() -> NoReturn: ... -def ctest() -> NoReturn: ...