Skip to content

Commit

Permalink
fix: Python 3.8 typing syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
neithere committed Sep 28, 2023
1 parent 430ac95 commit ac40391
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
34 changes: 17 additions & 17 deletions src/argh/assembling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {})
Expand All @@ -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:
Expand Down Expand Up @@ -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"
Expand All @@ -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)):
Expand All @@ -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"]

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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`.
Expand Down
6 changes: 3 additions & 3 deletions src/argh/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Command decorators
~~~~~~~~~~~~~~~~~~
"""
from typing import Callable, Optional
from typing import Callable, List, Optional

from argh.constants import (
ATTR_ALIASES,
Expand Down Expand Up @@ -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::
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions src/argh/dispatching.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit ac40391

Please sign in to comment.