diff --git a/src/argh/assembling.py b/src/argh/assembling.py index 9401843..6f0041e 100644 --- a/src/argh/assembling.py +++ b/src/argh/assembling.py @@ -15,7 +15,7 @@ """ from argparse import ArgumentParser from collections import OrderedDict -from typing import Any, Callable, Iterator, Optional +from typing import Any, Callable, Dict, Iterator, List, Optional, Tuple from argh.completion import COMPLETION_ENABLED from argh.constants import ( @@ -43,7 +43,7 @@ def _get_args_from_signature(function: Callable) -> Iterator[dict]: spec = get_arg_spec(function) - defaults: dict[str, Any] = dict( + defaults: Dict[str, Any] = dict( zip(reversed(spec.args), reversed(spec.defaults or tuple())) ) defaults.update(getattr(spec, "kwonlydefaults", None) or {}) @@ -62,8 +62,8 @@ def _get_args_from_signature(function: Callable) -> Iterator[dict]: ) for name in spec.args + kwonly: - flags: list[str] = [] # name_or_flags - akwargs: dict[str, Any] = {} # keyword arguments for add_argument() + flags: List[str] = [] # name_or_flags + akwargs: Dict[str, Any] = {} # keyword arguments for add_argument() if name in defaults or name in kwonly: if name in defaults: @@ -92,14 +92,14 @@ def _get_args_from_signature(function: Callable) -> Iterator[dict]: } -def _guess(kwargs: dict[str, Any]) -> dict[str, Any]: +def _guess(kwargs: Dict[str, Any]) -> Dict[str, Any]: """ Adds types, actions, etc. to given argument specification. For example, ``default=3`` implies ``type=int``. :param arg: a :class:`argh.utils.Arg` instance """ - guessed: dict[str, Any] = {} + guessed: Dict[str, Any] = {} # Parser actions that accept argument 'type' TYPE_AWARE_ACTIONS = "store", "append" @@ -124,7 +124,7 @@ def _guess(kwargs: dict[str, Any]) -> dict[str, Any]: return dict(kwargs, **guessed) -def _is_positional(args: list[str], prefix_chars: str = "-") -> bool: +def _is_positional(args: List[str], prefix_chars: str = "-") -> bool: if not args or not args[0]: raise ValueError("Expected at least one argument") if args[0][0].startswith(tuple(prefix_chars)): @@ -133,8 +133,8 @@ def _is_positional(args: list[str], prefix_chars: str = "-") -> bool: def _get_parser_param_kwargs( - parser: ArgumentParser, argspec: dict[str, Any] -) -> dict[str, Any]: + parser: ArgumentParser, argspec: Dict[str, Any] +) -> Dict[str, Any]: argspec = argspec.copy() # parser methods modify source data args = argspec["option_strings"] @@ -178,8 +178,8 @@ def set_default_command(parser, function: Callable) -> None: """ spec = get_arg_spec(function) - declared_args: list[dict] = getattr(function, ATTR_ARGS, []) - inferred_args: list[dict] = list(_get_args_from_signature(function)) + declared_args: List[dict] = getattr(function, ATTR_ARGS, []) + inferred_args: List[dict] = list(_get_args_from_signature(function)) if inferred_args and declared_args: # We've got a mixture of declared and inferred arguments @@ -282,10 +282,10 @@ def set_default_command(parser, function: Callable) -> None: def add_commands( parser: ArgumentParser, - functions: list[Callable], + functions: List[Callable], group_name: Optional[str] = None, - group_kwargs: Optional[dict[str, Any]] = None, - func_kwargs: Optional[dict[str, Any]] = None, + group_kwargs: Optional[Dict[str, Any]] = None, + func_kwargs: Optional[Dict[str, Any]] = None, ) -> None: """ Adds given functions as commands to given parser. @@ -372,11 +372,11 @@ def add_commands( set_default_command(command_parser, func) -def _extract_command_meta_from_func(func: Callable) -> tuple[str, dict]: +def _extract_command_meta_from_func(func: Callable) -> Tuple[str, dict]: # use explicitly defined name; if none, use function name (a_b → a-b) cmd_name = getattr(func, ATTR_NAME, func.__name__.replace("_", "-")) - func_parser_kwargs: dict[str, Any] = { + func_parser_kwargs: Dict[str, Any] = { # add command help from function's docstring "help": func.__doc__, # set default formatter @@ -390,7 +390,7 @@ def _extract_command_meta_from_func(func: Callable) -> tuple[str, dict]: def add_subcommands( - parser: ArgumentParser, group_name: str, functions: list[Callable], **group_kwargs + parser: ArgumentParser, group_name: str, functions: List[Callable], **group_kwargs ) -> None: """ A wrapper for :func:`add_commands`. diff --git a/src/argh/decorators.py b/src/argh/decorators.py index 5d71764..6a706f2 100644 --- a/src/argh/decorators.py +++ b/src/argh/decorators.py @@ -11,7 +11,7 @@ Command decorators ~~~~~~~~~~~~~~~~~~ """ -from typing import Callable, Optional +from typing import Callable, List, Optional from argh.constants import ( ATTR_ALIASES, @@ -49,7 +49,7 @@ def wrapper(func: Callable) -> Callable: return wrapper -def aliases(*names: list[str]) -> Callable: +def aliases(*names: List[str]) -> Callable: """ Defines alternative command name(s) for given function (along with its original name). Usage:: @@ -139,7 +139,7 @@ def wrapper(func: Callable) -> Callable: def wrap_errors( - errors: Optional[list[Exception]] = None, + errors: Optional[List[Exception]] = None, processor: Optional[Callable] = None, *args, ) -> Callable: diff --git a/src/argh/dispatching.py b/src/argh/dispatching.py index 9092d08..8301df8 100644 --- a/src/argh/dispatching.py +++ b/src/argh/dispatching.py @@ -15,7 +15,7 @@ import io import sys from types import GeneratorType -from typing import IO, Any, Callable, Iterator, Optional +from typing import IO, Any, Callable, Dict, Iterator, List, Optional from argh.assembling import add_commands, set_default_command from argh.completion import autocomplete @@ -46,7 +46,7 @@ class ArghNamespace(argparse.Namespace): def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) - self._functions_stack: list[Callable] = [] + self._functions_stack: List[Callable] = [] def __setattr__(self, key: str, value: Any) -> None: if key == DEST_FUNCTION: @@ -68,7 +68,7 @@ def get_function(self) -> Callable: def dispatch( parser: argparse.ArgumentParser, - argv: Optional[list[str]] = None, + argv: Optional[List[str]] = None, add_help_command: bool = True, completion: bool = True, output_file: IO = sys.stdout, @@ -349,7 +349,7 @@ def main(foo: int = 123) -> int: dispatch(parser, *args, **kwargs) -def dispatch_commands(functions: list[Callable], *args, **kwargs) -> None: +def dispatch_commands(functions: List[Callable], *args, **kwargs) -> None: """ A wrapper for :func:`dispatch` that creates a parser, adds commands to the parser and dispatches them. @@ -402,10 +402,10 @@ def greet() -> None: """ def __init__( - self, name: Optional[str] = None, parser_kwargs: Optional[dict[str, Any]] = None + self, name: Optional[str] = None, parser_kwargs: Optional[Dict[str, Any]] = None ) -> None: self.name = name or "unnamed" - self.commands: list[Callable] = [] + self.commands: List[Callable] = [] self.parser_kwargs = parser_kwargs or {} def __call__(self, function: Optional[Callable] = None):