From 184777735ba8354bad7781f55e4621a61d0c2a16 Mon Sep 17 00:00:00 2001 From: Stefan Tatschner Date: Tue, 17 Dec 2024 19:25:48 +0100 Subject: [PATCH] chore: Remove TypeVar stuff and use native generic syntax --- src/gallia/pydantic_argparse/argparse/actions.py | 6 +----- src/gallia/pydantic_argparse/parsers/utils.py | 10 ++++------ src/gallia/utils.py | 7 ++----- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/gallia/pydantic_argparse/argparse/actions.py b/src/gallia/pydantic_argparse/argparse/actions.py index f822f08c1..4796315c5 100644 --- a/src/gallia/pydantic_argparse/argparse/actions.py +++ b/src/gallia/pydantic_argparse/argparse/actions.py @@ -14,13 +14,9 @@ from collections.abc import Callable, Iterable, Sequence from typing import ( Any, - TypeVar, cast, ) -# Constants -T = TypeVar("T") - class SubParsersAction(argparse._SubParsersAction): # type: ignore """Recursively Nesting Sub-Parsers Action for Typed Argument Parsing. @@ -141,7 +137,7 @@ class BooleanOptionalAction(argparse.Action): # pragma: no cover """ - def __init__( + def __init__[T]( self, option_strings: Sequence[str], dest: str, diff --git a/src/gallia/pydantic_argparse/parsers/utils.py b/src/gallia/pydantic_argparse/parsers/utils.py index 5231bbb3f..3f7bf8dc9 100644 --- a/src/gallia/pydantic_argparse/parsers/utils.py +++ b/src/gallia/pydantic_argparse/parsers/utils.py @@ -6,23 +6,21 @@ from argparse import Action, FileType from collections.abc import Callable, Iterable -from typing import Any, Protocol, TypeVar - -_T = TypeVar("_T") +from typing import Any, Protocol class SupportsAddArgument(Protocol): """ArgumentParser protocol that captures the base parser and argument groups.""" - def add_argument( # noqa: D102 + def add_argument[T]( # noqa: D102 self, *name_or_flags: str, action: str | type[Action] = ..., nargs: int | str = ..., const: Any = ..., default: Any = ..., - type: Callable[[str], _T] | FileType = ..., # noqa: A002 - choices: Iterable[_T] | None = ..., + type: Callable[[str], T] | FileType = ..., # noqa: A002 + choices: Iterable[T] | None = ..., required: bool = ..., help: str | None = ..., # noqa: A002 metavar: str | tuple[str, ...] | None = ..., diff --git a/src/gallia/utils.py b/src/gallia/utils.py index 336bee5bc..f7a9fe4ab 100644 --- a/src/gallia/utils.py +++ b/src/gallia/utils.py @@ -16,7 +16,7 @@ from collections.abc import Awaitable, Callable from pathlib import Path from types import ModuleType -from typing import TYPE_CHECKING, Any, TypeVar +from typing import TYPE_CHECKING, Any from urllib.parse import urlparse import aiofiles @@ -193,10 +193,7 @@ def unravel_2d(listing: str) -> dict[int, list[int] | None]: } -T = TypeVar("T") - - -async def catch_and_log_exception( +async def catch_and_log_exception[T]( func: Callable[..., Awaitable[T]], *args: Any, **kwargs: Any,