From e2c924eb4bc59a56e0acae2d66144165142fe419 Mon Sep 17 00:00:00 2001 From: Stefan Tatschner Date: Thu, 28 Nov 2024 13:51:14 +0100 Subject: [PATCH] chore: Enable mypy for pydantic_argparse --- Makefile | 2 +- pyproject.toml | 15 --------------- src/gallia/pydantic_argparse/argparse/actions.py | 2 +- src/gallia/pydantic_argparse/argparse/parser.py | 8 +++++--- src/gallia/pydantic_argparse/parsers/command.py | 2 +- src/gallia/pydantic_argparse/utils/nesting.py | 4 ++-- src/gallia/pydantic_argparse/utils/types.py | 5 ++--- 7 files changed, 12 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index f8eb812b1..85be0fec9 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ default: .PHONY: lint lint: - uv run mypy src tests + uv run mypy --pretty src tests uv run ruff check src tests uv run ruff format --check src tests find tests/bats \( -iname "*.bash" -or -iname "*.bats" -or -iname "*.sh" \) | xargs shellcheck diff --git a/pyproject.toml b/pyproject.toml index 29b8b3675..d4db2c890 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -79,21 +79,6 @@ strict = true plugins = [ "pydantic.mypy" ] -exclude = [ - "src/gallia/pydantic_argparse/.*" -] - -# TODO: Make this compatible to --strict. -[[tool.mypy.overrides]] -module = ["gallia.pydantic_argparse"] -check_untyped_defs = true -disallow_untyped_defs = true -disallow_incomplete_defs = true -disallow_untyped_decorators = true -disallow_any_unimported = true -warn_return_any = true -warn_unused_ignores = true -no_implicit_optional = true [tool.ruff] target-version = "py311" diff --git a/src/gallia/pydantic_argparse/argparse/actions.py b/src/gallia/pydantic_argparse/argparse/actions.py index 62934ea60..f822f08c1 100644 --- a/src/gallia/pydantic_argparse/argparse/actions.py +++ b/src/gallia/pydantic_argparse/argparse/actions.py @@ -22,7 +22,7 @@ T = TypeVar("T") -class SubParsersAction(argparse._SubParsersAction): +class SubParsersAction(argparse._SubParsersAction): # type: ignore """Recursively Nesting Sub-Parsers Action for Typed Argument Parsing. This custom action differs in functionality from the existing standard diff --git a/src/gallia/pydantic_argparse/argparse/parser.py b/src/gallia/pydantic_argparse/argparse/parser.py index e75bdba72..99c3bd748 100644 --- a/src/gallia/pydantic_argparse/argparse/parser.py +++ b/src/gallia/pydantic_argparse/argparse/parser.py @@ -98,7 +98,7 @@ def __init__( self.extra_defaults = extra_defaults # Add Arguments Groups - self._subcommands: argparse._SubParsersAction | None = None + self._subcommands: argparse._SubParsersAction[Any] | None = None # Add Arguments from Model self._submodels: dict[str, type[BaseModel]] = {} @@ -136,7 +136,9 @@ def parse_typed_args( # to report it to the user self._validation_error(exc, nested_parser) - def _validation_error(self, error: ValidationError, parser: _NestedArgumentParser) -> Never: + def _validation_error( + self, error: ValidationError, parser: _NestedArgumentParser[Any] + ) -> Never: self.print_usage(sys.stderr) model = parser.model @@ -216,7 +218,7 @@ def error(self, message: str) -> NoReturn: # Raise Error # raise argparse.ArgumentError(None, msg) - def _commands(self) -> argparse._SubParsersAction: + def _commands(self) -> argparse._SubParsersAction: # type: ignore """Creates and Retrieves Subcommands Action for the ArgumentParser. Returns: diff --git a/src/gallia/pydantic_argparse/parsers/command.py b/src/gallia/pydantic_argparse/parsers/command.py index 276f161d8..43b2a0636 100644 --- a/src/gallia/pydantic_argparse/parsers/command.py +++ b/src/gallia/pydantic_argparse/parsers/command.py @@ -19,7 +19,7 @@ def parse_field( - subparser: argparse._SubParsersAction, + subparser: argparse._SubParsersAction, # type: ignore field: PydanticField, extra_defaults: dict[type, dict[str, Any]] | None = None, ) -> None: diff --git a/src/gallia/pydantic_argparse/utils/nesting.py b/src/gallia/pydantic_argparse/utils/nesting.py index 1f2dec4fd..0562450df 100644 --- a/src/gallia/pydantic_argparse/utils/nesting.py +++ b/src/gallia/pydantic_argparse/utils/nesting.py @@ -7,7 +7,7 @@ from argparse import Namespace from typing import Any, Generic, TypeAlias -from boltons.iterutils import get_path, remap +from boltons.iterutils import get_path, remap # type: ignore from pydantic import BaseModel from .namespaces import to_dict @@ -30,7 +30,7 @@ def __init__( self.schema: dict[str, Any] = self._get_nested_model_fields(self.model, namespace) self.schema = self._remove_null_leaves(self.schema) - def _get_nested_model_fields(self, model: ModelT, namespace: Namespace) -> dict[str, Any]: + def _get_nested_model_fields(self, model: ModelT[Any], namespace: Namespace) -> dict[str, Any]: def contains_subcommand(ns: Namespace, subcommand_path: tuple[str, ...]) -> bool: for step in subcommand_path: tmp = getattr(ns, step, None) diff --git a/src/gallia/pydantic_argparse/utils/types.py b/src/gallia/pydantic_argparse/utils/types.py index 6e3a4e24d..f8577476e 100644 --- a/src/gallia/pydantic_argparse/utils/types.py +++ b/src/gallia/pydantic_argparse/utils/types.py @@ -9,11 +9,10 @@ """ from collections.abc import Iterable +from typing import Any -# Version-Guarded - -def all_types(types: Iterable) -> bool: +def all_types(types: Iterable[Any]) -> bool: """Check if all inputs are `type`s and not instances. Args: