From d3dc916e35aa40078485236cb4ba4995eba20d0a Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Wed, 22 May 2024 17:00:38 -0700 Subject: [PATCH] Add type annotations for spin utils (#188) With this in place, we can progressively add more type annotations. Closes #177 --- .pre-commit-config.yaml | 5 +++++ example_pkg/doc/conf.py | 2 +- spin/cmds/meson.py | 10 +++++----- spin/cmds/util.py | 12 ++++++++++-- 4 files changed, 21 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/spin/cmds/meson.py b/spin/cmds/meson.py index c425a0a..6f32b6e 100644 --- a/spin/cmds/meson.py +++ b/spin/cmds/meson.py @@ -215,9 +215,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`" ) @@ -363,7 +363,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..a7234ae 100644 --- a/spin/cmds/util.py +++ b/spin/cmds/util.py @@ -2,13 +2,21 @@ import shlex import subprocess import sys +from typing import Optional import click def run( - cmd, cwd=None, replace=False, sys_exit=True, output=True, echo=True, *args, **kwargs -): + cmd: list[str], + cwd: Optional[str] = None, # in 3.10 and up: str | None + replace: bool = False, + sys_exit: bool = True, + output: bool = True, + echo: bool = True, + *args, + **kwargs, +) -> subprocess.CompletedProcess: """Run a shell command. Parameters