@@ -198,16 +198,21 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
198198 ONE_OR_MORE ,
199199 ZERO_OR_MORE ,
200200 ArgumentError ,
201- _ ,
201+ )
202+ from gettext import (
203+ gettext ,
202204)
203205from typing import (
204206 Any ,
205207 Callable ,
208+ Iterable ,
209+ List ,
206210 NoReturn ,
207211 Optional ,
208212 Tuple ,
209213 Type ,
210214 Union ,
215+ cast ,
211216)
212217
213218from . import (
@@ -261,11 +266,11 @@ class CompletionItem(str):
261266 See header of this file for more information
262267 """
263268
264- def __new__ (cls , value : object , * args , ** kwargs ) -> str :
265- return super ().__new__ (cls , value )
269+ def __new__ (cls , value : object , * args : Any , ** kwargs : Any ) -> 'CompletionItem' :
270+ return cast ( CompletionItem , super (CompletionItem , cls ).__new__ (cls , value )) # type: ignore [call-arg]
266271
267272 # noinspection PyUnusedLocal
268- def __init__ (self , value : object , desc : str = '' , * args ) -> None :
273+ def __init__ (self , value : object , desc : str = '' , * args : Any ) -> None :
269274 """
270275 CompletionItem Initializer
271276
@@ -287,7 +292,11 @@ class ChoicesCallable:
287292 While argparse has the built-in choices attribute, it is limited to an iterable.
288293 """
289294
290- def __init__ (self , is_completer : bool , to_call : Callable ):
295+ def __init__ (
296+ self ,
297+ is_completer : bool ,
298+ to_call : Union [Callable [[], List [str ]], Callable [[str , str , int , int ], List [str ]]],
299+ ) -> None :
291300 """
292301 Initializer
293302 :param is_completer: True if to_call is a tab completion routine which expects
@@ -319,12 +328,12 @@ def _set_choices_callable(action: argparse.Action, choices_callable: ChoicesCall
319328 setattr (action , ATTR_CHOICES_CALLABLE , choices_callable )
320329
321330
322- def set_choices_provider (action : argparse .Action , choices_provider : Callable ) -> None :
331+ def set_choices_provider (action : argparse .Action , choices_provider : Callable [[], List [ str ]] ) -> None :
323332 """Set choices_provider on an argparse action"""
324333 _set_choices_callable (action , ChoicesCallable (is_completer = False , to_call = choices_provider ))
325334
326335
327- def set_completer (action : argparse .Action , completer : Callable ) -> None :
336+ def set_completer (action : argparse .Action , completer : Callable [[ str , str , int , int ], List [ str ]] ) -> None :
328337 """Set completer on an argparse action"""
329338 _set_choices_callable (action , ChoicesCallable (is_completer = True , to_call = completer ))
330339
@@ -339,14 +348,14 @@ def set_completer(action: argparse.Action, completer: Callable) -> None:
339348
340349
341350def _add_argument_wrapper (
342- self ,
343- * args ,
351+ self : argparse . _ActionsContainer ,
352+ * args : Any ,
344353 nargs : Union [int , str , Tuple [int ], Tuple [int , int ], Tuple [int , float ], None ] = None ,
345- choices_provider : Optional [Callable ] = None ,
346- completer : Optional [Callable ] = None ,
354+ choices_provider : Optional [Callable [[], List [ str ]] ] = None ,
355+ completer : Optional [Callable [[ str , str , int , int ], List [ str ]] ] = None ,
347356 suppress_tab_hint : bool = False ,
348357 descriptive_header : Optional [str ] = None ,
349- ** kwargs
358+ ** kwargs : Any
350359) -> argparse .Action :
351360 """
352361 Wrapper around _ActionsContainer.add_argument() which supports more settings used by cmd2
@@ -392,6 +401,7 @@ def _add_argument_wrapper(
392401 nargs_range = None
393402
394403 if nargs is not None :
404+ nargs_adjusted : Union [int , str , Tuple [int ], Tuple [int , int ], Tuple [int , float ], None ]
395405 # Check if nargs was given as a range
396406 if isinstance (nargs , tuple ):
397407
@@ -402,19 +412,19 @@ def _add_argument_wrapper(
402412 # Validate nargs tuple
403413 if (
404414 len (nargs ) != 2
405- or not isinstance (nargs [0 ], int )
406- or not (isinstance (nargs [1 ], int ) or nargs [1 ] == constants .INFINITY )
415+ or not isinstance (nargs [0 ], int ) # type: ignore[unreachable]
416+ or not (isinstance (nargs [1 ], int ) or nargs [1 ] == constants .INFINITY ) # type: ignore[misc]
407417 ):
408418 raise ValueError ('Ranged values for nargs must be a tuple of 1 or 2 integers' )
409- if nargs [0 ] >= nargs [1 ]:
419+ if nargs [0 ] >= nargs [1 ]: # type: ignore[misc]
410420 raise ValueError ('Invalid nargs range. The first value must be less than the second' )
411421 if nargs [0 ] < 0 :
412422 raise ValueError ('Negative numbers are invalid for nargs range' )
413423
414424 # Save the nargs tuple as our range setting
415425 nargs_range = nargs
416426 range_min = nargs_range [0 ]
417- range_max = nargs_range [1 ]
427+ range_max = nargs_range [1 ] # type: ignore[misc]
418428
419429 # Convert nargs into a format argparse recognizes
420430 if range_min == 0 :
@@ -460,7 +470,7 @@ def _add_argument_wrapper(
460470
461471# Overwrite _ActionsContainer.add_argument with our wrapper
462472# noinspection PyProtectedMember
463- argparse ._ActionsContainer . add_argument = _add_argument_wrapper
473+ setattr ( argparse ._ActionsContainer , ' add_argument' , _add_argument_wrapper )
464474
465475############################################################################################################
466476# Patch ArgumentParser._get_nargs_pattern with our wrapper to nargs ranges
@@ -472,7 +482,7 @@ def _add_argument_wrapper(
472482
473483
474484# noinspection PyProtectedMember
475- def _get_nargs_pattern_wrapper (self , action ) -> str :
485+ def _get_nargs_pattern_wrapper (self : argparse . ArgumentParser , action : argparse . Action ) -> str :
476486 # Wrapper around ArgumentParser._get_nargs_pattern behavior to support nargs ranges
477487 nargs_range = getattr (action , ATTR_NARGS_RANGE , None )
478488 if nargs_range is not None :
@@ -494,7 +504,7 @@ def _get_nargs_pattern_wrapper(self, action) -> str:
494504
495505# Overwrite ArgumentParser._get_nargs_pattern with our wrapper
496506# noinspection PyProtectedMember
497- argparse .ArgumentParser . _get_nargs_pattern = _get_nargs_pattern_wrapper
507+ setattr ( argparse .ArgumentParser , ' _get_nargs_pattern' , _get_nargs_pattern_wrapper )
498508
499509
500510############################################################################################################
@@ -505,7 +515,7 @@ def _get_nargs_pattern_wrapper(self, action) -> str:
505515
506516
507517# noinspection PyProtectedMember
508- def _match_argument_wrapper (self , action , arg_strings_pattern ) -> int :
518+ def _match_argument_wrapper (self : argparse . ArgumentParser , action : argparse . Action , arg_strings_pattern : str ) -> int :
509519 # Wrapper around ArgumentParser._match_argument behavior to support nargs ranges
510520 nargs_pattern = self ._get_nargs_pattern (action )
511521 match = re .match (nargs_pattern , arg_strings_pattern )
@@ -521,15 +531,15 @@ def _match_argument_wrapper(self, action, arg_strings_pattern) -> int:
521531
522532# Overwrite ArgumentParser._match_argument with our wrapper
523533# noinspection PyProtectedMember
524- argparse .ArgumentParser . _match_argument = _match_argument_wrapper
534+ setattr ( argparse .ArgumentParser , ' _match_argument' , _match_argument_wrapper )
525535
526536
527537############################################################################################################
528538# Patch argparse._SubParsersAction to add remove_parser function
529539############################################################################################################
530540
531541# noinspection PyPep8Naming
532- def _SubParsersAction_remove_parser (self , name : str ):
542+ def _SubParsersAction_remove_parser (self : argparse . _SubParsersAction , name : str ) -> None :
533543 """
534544 Removes a sub-parser from a sub-parsers group
535545
@@ -572,20 +582,26 @@ class so cmd2 can remove subcommands from a parser.
572582class Cmd2HelpFormatter (argparse .RawTextHelpFormatter ):
573583 """Custom help formatter to configure ordering of help text"""
574584
575- def _format_usage (self , usage , actions , groups , prefix ) -> str :
585+ def _format_usage (
586+ self ,
587+ usage : Optional [str ],
588+ actions : Iterable [argparse .Action ],
589+ groups : Iterable [argparse ._ArgumentGroup ],
590+ prefix : Optional [str ] = None ,
591+ ) -> str :
576592 if prefix is None :
577- prefix = _ ('Usage: ' )
593+ prefix = gettext ('Usage: ' )
578594
579595 # if usage is specified, use that
580596 if usage is not None :
581597 usage %= dict (prog = self ._prog )
582598
583599 # if no optionals or positionals are available, usage is just prog
584- elif usage is None and not actions :
600+ elif not actions :
585601 usage = '%(prog)s' % dict (prog = self ._prog )
586602
587603 # if optionals and positionals are available, calculate usage
588- elif usage is None :
604+ else :
589605 prog = '%(prog)s' % dict (prog = self ._prog )
590606
591607 # split optionals from positionals
@@ -630,7 +646,7 @@ def _format_usage(self, usage, actions, groups, prefix) -> str:
630646
631647 # helper for wrapping lines
632648 # noinspection PyMissingOrEmptyDocstring,PyShadowingNames
633- def get_lines (parts , indent , prefix = None ):
649+ def get_lines (parts : List [ str ] , indent : str , prefix : Optional [ str ] = None ):
634650 lines = []
635651 line = []
636652 if prefix is not None :
0 commit comments