From 66a56cc8f8f95f42e101c4f318f79162284dac84 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Wed, 8 May 2024 16:08:03 -0700 Subject: [PATCH] Add type annotations for spin utils With this in place, we can progressively add more type annotations. Closes #177 --- .pre-commit-config.yaml | 5 +++++ example_pkg/doc/conf.py | 2 +- pyproject.toml | 2 ++ spin/cmds/meson.py | 10 +++++----- spin/cmds/util.py | 11 +++++++++-- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a1045eb..e921ad5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -40,3 +40,8 @@ repos: hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] + + - repo: https://github.com/pre-commit/mirrors-mypy + rev: e5ea6670624c24f8321f6328ef3176dbba76db46 # frozen: v1.10.0 + hooks: + - id: mypy diff --git a/example_pkg/doc/conf.py b/example_pkg/doc/conf.py index 6390d13..23d7f52 100644 --- a/example_pkg/doc/conf.py +++ b/example_pkg/doc/conf.py @@ -13,7 +13,7 @@ # -- General configuration --------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration -extensions = [] +extensions: list[str] = [] templates_path = ["_templates"] exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] diff --git a/pyproject.toml b/pyproject.toml index e3ca486..da6f553 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -54,6 +54,8 @@ filterwarnings = [ [tool.ruff] line-length = 88 target-version = "py38" + +[tool.ruff.lint] select = [ "C", "E", diff --git a/spin/cmds/meson.py b/spin/cmds/meson.py index 27671e1..1b070b2 100644 --- a/spin/cmds/meson.py +++ b/spin/cmds/meson.py @@ -213,9 +213,9 @@ def _check_coverage_tool_installation(coverage_type: GcovReportFormat): # Verify the tools are installed prior to the build p = _run(["ninja", "-C", build_dir, "-t", "targets", "all"], output=False) - if f"coverage-{coverage_type.value}" not in p.stdout.decode("ascii"): + if f"coverage-{coverage_type}" not in p.stdout.decode("ascii"): raise click.ClickException( - f"coverage-{coverage_type.value} is not supported... " + f"coverage-{coverage_type} is not supported... " f"Ensure the following are installed: {', '.join(requirements[coverage_type])} " "and rerun `spin test --gcov`" ) @@ -361,7 +361,7 @@ def _get_configured_command(command_name): ) @click.option( "--gcov-format", - type=click.Choice(GcovReportFormat), + type=click.Choice([e.name for e in GcovReportFormat]), default="html", help=f"Format of the gcov report. Can be one of {', '.join(e.value for e in GcovReportFormat)}.", ) @@ -512,7 +512,7 @@ def test( # Generate report click.secho( - f"Generating {gcov_format.value} coverage report...", + f"Generating {gcov_format} coverage report...", bold=True, fg="bright_yellow", ) @@ -521,7 +521,7 @@ def test( "ninja", "-C", build_dir, - f"coverage-{gcov_format.value.lower()}", + f"coverage-{gcov_format.lower()}", ], output=False, ) diff --git a/spin/cmds/util.py b/spin/cmds/util.py index db4077e..86107f1 100644 --- a/spin/cmds/util.py +++ b/spin/cmds/util.py @@ -7,8 +7,15 @@ def run( - cmd, cwd=None, replace=False, sys_exit=True, output=True, echo=True, *args, **kwargs -): + cmd: list[str], + cwd: str | None = None, + replace: bool = False, + sys_exit: bool = True, + output: bool = True, + echo: bool = True, + *args, + **kwargs, +) -> subprocess.CompletedProcess: """Run a shell command. Parameters