1+ # coding=utf-8
12"""
23This module adds capabilities to argparse by patching a few of its functions.
34It also defines a parser class called Cmd2ArgumentParser which improves error
@@ -229,17 +230,24 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
229230 ZERO_OR_MORE ,
230231 ArgumentError ,
231232)
232- from collections .abc import Callable , Iterable , Sequence
233233from gettext import (
234234 gettext ,
235235)
236236from typing import (
237237 IO ,
238238 TYPE_CHECKING ,
239239 Any ,
240+ Callable ,
241+ Dict ,
242+ Iterable ,
243+ List ,
240244 NoReturn ,
241245 Optional ,
242246 Protocol ,
247+ Sequence ,
248+ Set ,
249+ Tuple ,
250+ Type ,
243251 Union ,
244252 cast ,
245253 runtime_checkable ,
@@ -317,7 +325,7 @@ class ChoicesProviderFuncBase(Protocol):
317325 Function that returns a list of choices in support of tab completion
318326 """
319327
320- def __call__ (self ) -> list [str ]: ... # pragma: no cover
328+ def __call__ (self ) -> List [str ]: ... # pragma: no cover
321329
322330
323331@runtime_checkable
@@ -326,7 +334,7 @@ class ChoicesProviderFuncWithTokens(Protocol):
326334 Function that returns a list of choices in support of tab completion and accepts a dictionary of prior arguments.
327335 """
328336
329- def __call__ (self , * , arg_tokens : dict [str , list [str ]] = {}) -> list [str ]: ... # pragma: no cover
337+ def __call__ (self , * , arg_tokens : Dict [str , List [str ]] = {}) -> List [str ]: ... # pragma: no cover
330338
331339
332340ChoicesProviderFunc = Union [ChoicesProviderFuncBase , ChoicesProviderFuncWithTokens ]
@@ -344,7 +352,7 @@ def __call__(
344352 line : str ,
345353 begidx : int ,
346354 endidx : int ,
347- ) -> list [str ]: ... # pragma: no cover
355+ ) -> List [str ]: ... # pragma: no cover
348356
349357
350358@runtime_checkable
@@ -361,8 +369,8 @@ def __call__(
361369 begidx : int ,
362370 endidx : int ,
363371 * ,
364- arg_tokens : dict [str , list [str ]] = {},
365- ) -> list [str ]: ... # pragma: no cover
372+ arg_tokens : Dict [str , List [str ]] = {},
373+ ) -> List [str ]: ... # pragma: no cover
366374
367375
368376CompleterFunc = Union [CompleterFuncBase , CompleterFuncWithTokens ]
@@ -563,7 +571,7 @@ def _action_set_descriptive_header(self: argparse.Action, descriptive_header: Op
563571############################################################################################################
564572# Patch argparse.Action with accessors for nargs_range attribute
565573############################################################################################################
566- def _action_get_nargs_range (self : argparse .Action ) -> Optional [tuple [int , Union [int , float ]]]:
574+ def _action_get_nargs_range (self : argparse .Action ) -> Optional [Tuple [int , Union [int , float ]]]:
567575 """
568576 Get the nargs_range attribute of an argparse Action.
569577
@@ -574,13 +582,13 @@ def _action_get_nargs_range(self: argparse.Action) -> Optional[tuple[int, Union[
574582 :param self: argparse Action being queried
575583 :return: The value of nargs_range or None if attribute does not exist
576584 """
577- return cast ("Optional[tuple [int, Union[int, float]]]" , getattr (self , ATTR_NARGS_RANGE , None ))
585+ return cast ("Optional[Tuple [int, Union[int, float]]]" , getattr (self , ATTR_NARGS_RANGE , None ))
578586
579587
580588setattr (argparse .Action , 'get_nargs_range' , _action_get_nargs_range )
581589
582590
583- def _action_set_nargs_range (self : argparse .Action , nargs_range : Optional [tuple [int , Union [int , float ]]]) -> None :
591+ def _action_set_nargs_range (self : argparse .Action , nargs_range : Optional [Tuple [int , Union [int , float ]]]) -> None :
584592 """
585593 Set the nargs_range attribute of an argparse Action.
586594
@@ -638,11 +646,11 @@ def _action_set_suppress_tab_hint(self: argparse.Action, suppress_tab_hint: bool
638646# Allow developers to add custom action attributes
639647############################################################################################################
640648
641- CUSTOM_ACTION_ATTRIBS : set [str ] = set ()
649+ CUSTOM_ACTION_ATTRIBS : Set [str ] = set ()
642650_CUSTOM_ATTRIB_PFX = '_attr_'
643651
644652
645- def register_argparse_argument_parameter (param_name : str , param_type : Optional [type [Any ]]) -> None :
653+ def register_argparse_argument_parameter (param_name : str , param_type : Optional [Type [Any ]]) -> None :
646654 """
647655 Registers a custom argparse argument parameter.
648656
@@ -711,7 +719,7 @@ def _action_set_custom_parameter(self: argparse.Action, value: Any) -> None:
711719def _add_argument_wrapper (
712720 self : argparse ._ActionsContainer ,
713721 * args : Any ,
714- nargs : Union [int , str , tuple [int ], tuple [int , int ], tuple [int , float ], None ] = None ,
722+ nargs : Union [int , str , Tuple [int ], Tuple [int , int ], Tuple [int , float ], None ] = None ,
715723 choices_provider : Optional [ChoicesProviderFunc ] = None ,
716724 completer : Optional [CompleterFunc ] = None ,
717725 suppress_tab_hint : bool = False ,
@@ -762,7 +770,7 @@ def _add_argument_wrapper(
762770 nargs_range = None
763771
764772 if nargs is not None :
765- nargs_adjusted : Union [int , str , tuple [int ], tuple [int , int ], tuple [int , float ], None ]
773+ nargs_adjusted : Union [int , str , Tuple [int ], Tuple [int , int ], Tuple [int , float ], None ]
766774 # Check if nargs was given as a range
767775 if isinstance (nargs , tuple ):
768776 # Handle 1-item tuple by setting max to INFINITY
@@ -812,7 +820,7 @@ def _add_argument_wrapper(
812820 kwargs ['nargs' ] = nargs_adjusted
813821
814822 # Extract registered custom keyword arguments
815- custom_attribs : dict [str , Any ] = {}
823+ custom_attribs : Dict [str , Any ] = {}
816824 for keyword , value in kwargs .items ():
817825 if keyword in CUSTOM_ACTION_ATTRIBS :
818826 custom_attribs [keyword ] = value
@@ -910,7 +918,7 @@ def _match_argument_wrapper(self: argparse.ArgumentParser, action: argparse.Acti
910918ATTR_AP_COMPLETER_TYPE = 'ap_completer_type'
911919
912920
913- def _ArgumentParser_get_ap_completer_type (self : argparse .ArgumentParser ) -> Optional [type ['ArgparseCompleter' ]]:
921+ def _ArgumentParser_get_ap_completer_type (self : argparse .ArgumentParser ) -> Optional [Type ['ArgparseCompleter' ]]:
914922 """
915923 Get the ap_completer_type attribute of an argparse ArgumentParser.
916924
@@ -921,13 +929,13 @@ def _ArgumentParser_get_ap_completer_type(self: argparse.ArgumentParser) -> Opti
921929 :param self: ArgumentParser being queried
922930 :return: An ArgparseCompleter-based class or None if attribute does not exist
923931 """
924- return cast ("Optional[type [ArgparseCompleter]]" , getattr (self , ATTR_AP_COMPLETER_TYPE , None ))
932+ return cast ("Optional[Type [ArgparseCompleter]]" , getattr (self , ATTR_AP_COMPLETER_TYPE , None ))
925933
926934
927935setattr (argparse .ArgumentParser , 'get_ap_completer_type' , _ArgumentParser_get_ap_completer_type )
928936
929937
930- def _ArgumentParser_set_ap_completer_type (self : argparse .ArgumentParser , ap_completer_type : type ['ArgparseCompleter' ]) -> None :
938+ def _ArgumentParser_set_ap_completer_type (self : argparse .ArgumentParser , ap_completer_type : Type ['ArgparseCompleter' ]) -> None :
931939 """
932940 Set the ap_completer_type attribute of an argparse ArgumentParser.
933941
@@ -1084,9 +1092,9 @@ def _format_usage(
10841092 # End cmd2 customization
10851093
10861094 # helper for wrapping lines
1087- def get_lines (parts : list [str ], indent : str , prefix : Optional [str ] = None ) -> list [str ]:
1088- lines : list [str ] = []
1089- line : list [str ] = []
1095+ def get_lines (parts : List [str ], indent : str , prefix : Optional [str ] = None ) -> List [str ]:
1096+ lines : List [str ] = []
1097+ line : List [str ] = []
10901098 if prefix is not None :
10911099 line_len = len (prefix ) - 1
10921100 else :
@@ -1147,7 +1155,7 @@ def _format_action_invocation(self, action: argparse.Action) -> str:
11471155 (metavar ,) = self ._metavar_formatter (action , default )(1 )
11481156 return metavar
11491157
1150- parts : list [str ] = []
1158+ parts : List [str ] = []
11511159
11521160 # if the Optional doesn't take a value, format is:
11531161 # -s, --long
@@ -1167,8 +1175,8 @@ def _format_action_invocation(self, action: argparse.Action) -> str:
11671175 def _determine_metavar (
11681176 self ,
11691177 action : argparse .Action ,
1170- default_metavar : Union [str , tuple [str , ...]],
1171- ) -> Union [str , tuple [str , ...]]:
1178+ default_metavar : Union [str , Tuple [str , ...]],
1179+ ) -> Union [str , Tuple [str , ...]]:
11721180 """Custom method to determine what to use as the metavar value of an action"""
11731181 if action .metavar is not None :
11741182 result = action .metavar
@@ -1184,18 +1192,18 @@ def _determine_metavar(
11841192 def _metavar_formatter (
11851193 self ,
11861194 action : argparse .Action ,
1187- default_metavar : Union [str , tuple [str , ...]],
1188- ) -> Callable [[int ], tuple [str , ...]]:
1195+ default_metavar : Union [str , Tuple [str , ...]],
1196+ ) -> Callable [[int ], Tuple [str , ...]]:
11891197 metavar = self ._determine_metavar (action , default_metavar )
11901198
1191- def format_tuple (tuple_size : int ) -> tuple [str , ...]:
1199+ def format_tuple (tuple_size : int ) -> Tuple [str , ...]:
11921200 if isinstance (metavar , tuple ):
11931201 return metavar
11941202 return (metavar ,) * tuple_size
11951203
11961204 return format_tuple
11971205
1198- def _format_args (self , action : argparse .Action , default_metavar : Union [str , tuple [str , ...]]) -> str :
1206+ def _format_args (self , action : argparse .Action , default_metavar : Union [str , Tuple [str , ...]]) -> str :
11991207 """Customized to handle ranged nargs and make other output less verbose"""
12001208 metavar = self ._determine_metavar (action , default_metavar )
12011209 metavar_formatter = self ._metavar_formatter (action , default_metavar )
@@ -1233,7 +1241,7 @@ def __init__(
12331241 description : Optional [str ] = None ,
12341242 epilog : Optional [str ] = None ,
12351243 parents : Sequence [argparse .ArgumentParser ] = (),
1236- formatter_class : type [argparse .HelpFormatter ] = Cmd2HelpFormatter ,
1244+ formatter_class : Type [argparse .HelpFormatter ] = Cmd2HelpFormatter ,
12371245 prefix_chars : str = '-' ,
12381246 fromfile_prefix_chars : Optional [str ] = None ,
12391247 argument_default : Optional [str ] = None ,
@@ -1244,7 +1252,7 @@ def __init__(
12441252 suggest_on_error : bool = False ,
12451253 color : bool = False ,
12461254 * ,
1247- ap_completer_type : Optional [type ['ArgparseCompleter' ]] = None ,
1255+ ap_completer_type : Optional [Type ['ArgparseCompleter' ]] = None ,
12481256 ) -> None :
12491257 """
12501258 # Custom parameter added by cmd2
@@ -1402,10 +1410,10 @@ def set(self, new_val: Any) -> None:
14021410
14031411
14041412# The default ArgumentParser class for a cmd2 app
1405- DEFAULT_ARGUMENT_PARSER : type [argparse .ArgumentParser ] = Cmd2ArgumentParser
1413+ DEFAULT_ARGUMENT_PARSER : Type [argparse .ArgumentParser ] = Cmd2ArgumentParser
14061414
14071415
1408- def set_default_argument_parser_type (parser_type : type [argparse .ArgumentParser ]) -> None :
1416+ def set_default_argument_parser_type (parser_type : Type [argparse .ArgumentParser ]) -> None :
14091417 """
14101418 Set the default ArgumentParser class for a cmd2 app. This must be called prior to loading cmd2.py if
14111419 you want to override the parser for cmd2's built-in commands. See examples/override_parser.py.
0 commit comments