@@ -221,6 +221,8 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
221
221
sub-parser from a sub-parsers group. See _SubParsersAction_remove_parser` for more details.
222
222
"""
223
223
224
+ from __future__ import annotations
225
+
224
226
import argparse
225
227
import re
226
228
import sys
@@ -229,7 +231,6 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
229
231
ZERO_OR_MORE ,
230
232
ArgumentError ,
231
233
)
232
- from collections .abc import Callable , Iterable , Sequence
233
234
from gettext import (
234
235
gettext ,
235
236
)
@@ -245,12 +246,16 @@ def my_completer(self, text, line, begidx, endidx, arg_tokens)
245
246
runtime_checkable ,
246
247
)
247
248
249
+ from typing_extensions import Self
250
+
248
251
from . import (
249
252
ansi ,
250
253
constants ,
251
254
)
252
255
253
256
if TYPE_CHECKING : # pragma: no cover
257
+ from collections .abc import Callable , Iterable , Sequence
258
+
254
259
from .argparse_completer import (
255
260
ArgparseCompleter ,
256
261
)
@@ -281,7 +286,7 @@ class CompletionItem(str): # noqa: SLOT000
281
286
See header of this file for more information
282
287
"""
283
288
284
- def __new__ (cls , value : object , * _args : Any , ** _kwargs : Any ) -> 'CompletionItem' :
289
+ def __new__ (cls , value : object , * _args : Any , ** _kwargs : Any ) -> Self :
285
290
"""Responsible for creating and returning a new instance, called before __init__ when an object is instantiated."""
286
291
return super ().__new__ (cls , value )
287
292
@@ -371,7 +376,7 @@ class ChoicesCallable:
371
376
def __init__ (
372
377
self ,
373
378
is_completer : bool ,
374
- to_call : Union [ CompleterFunc , ChoicesProviderFunc ] ,
379
+ to_call : CompleterFunc | ChoicesProviderFunc ,
375
380
) -> None :
376
381
"""Initialize the ChoiceCallable instance.
377
382
@@ -432,7 +437,7 @@ def choices_provider(self) -> ChoicesProviderFunc:
432
437
############################################################################################################
433
438
# Patch argparse.Action with accessors for choice_callable attribute
434
439
############################################################################################################
435
- def _action_get_choices_callable (self : argparse .Action ) -> Optional [ ChoicesCallable ] :
440
+ def _action_get_choices_callable (self : argparse .Action ) -> ChoicesCallable | None :
436
441
"""Get the choices_callable attribute of an argparse Action.
437
442
438
443
This function is added by cmd2 as a method called ``get_choices_callable()`` to ``argparse.Action`` class.
@@ -518,7 +523,7 @@ def _action_set_completer(
518
523
############################################################################################################
519
524
# Patch argparse.Action with accessors for descriptive_header attribute
520
525
############################################################################################################
521
- def _action_get_descriptive_header (self : argparse .Action ) -> Optional [ str ] :
526
+ def _action_get_descriptive_header (self : argparse .Action ) -> str | None :
522
527
"""Get the descriptive_header attribute of an argparse Action.
523
528
524
529
This function is added by cmd2 as a method called ``get_descriptive_header()`` to ``argparse.Action`` class.
@@ -534,7 +539,7 @@ def _action_get_descriptive_header(self: argparse.Action) -> Optional[str]:
534
539
setattr (argparse .Action , 'get_descriptive_header' , _action_get_descriptive_header )
535
540
536
541
537
- def _action_set_descriptive_header (self : argparse .Action , descriptive_header : Optional [ str ] ) -> None :
542
+ def _action_set_descriptive_header (self : argparse .Action , descriptive_header : str | None ) -> None :
538
543
"""Set the descriptive_header attribute of an argparse Action.
539
544
540
545
This function is added by cmd2 as a method called ``set_descriptive_header()`` to ``argparse.Action`` class.
@@ -553,7 +558,7 @@ def _action_set_descriptive_header(self: argparse.Action, descriptive_header: Op
553
558
############################################################################################################
554
559
# Patch argparse.Action with accessors for nargs_range attribute
555
560
############################################################################################################
556
- def _action_get_nargs_range (self : argparse .Action ) -> Optional [ tuple [int , Union [ int , float ]]] :
561
+ def _action_get_nargs_range (self : argparse .Action ) -> tuple [int , int | float ] | None :
557
562
"""Get the nargs_range attribute of an argparse Action.
558
563
559
564
This function is added by cmd2 as a method called ``get_nargs_range()`` to ``argparse.Action`` class.
@@ -569,7 +574,7 @@ def _action_get_nargs_range(self: argparse.Action) -> Optional[tuple[int, Union[
569
574
setattr (argparse .Action , 'get_nargs_range' , _action_get_nargs_range )
570
575
571
576
572
- def _action_set_nargs_range (self : argparse .Action , nargs_range : Optional [ tuple [int , Union [ int , float ]]] ) -> None :
577
+ def _action_set_nargs_range (self : argparse .Action , nargs_range : tuple [int , int | float ] | None ) -> None :
573
578
"""Set the nargs_range attribute of an argparse Action.
574
579
575
580
This function is added by cmd2 as a method called ``set_nargs_range()`` to ``argparse.Action`` class.
@@ -628,7 +633,7 @@ def _action_set_suppress_tab_hint(self: argparse.Action, suppress_tab_hint: bool
628
633
_CUSTOM_ATTRIB_PFX = '_attr_'
629
634
630
635
631
- def register_argparse_argument_parameter (param_name : str , param_type : Optional [ type [Any ]] ) -> None :
636
+ def register_argparse_argument_parameter (param_name : str , param_type : type [Any ] | None ) -> None :
632
637
"""Register a custom argparse argument parameter.
633
638
634
639
The registered name will then be a recognized keyword parameter to the parser's `add_argument()` function.
@@ -694,11 +699,11 @@ def _action_set_custom_parameter(self: argparse.Action, value: Any) -> None:
694
699
def _add_argument_wrapper (
695
700
self : argparse ._ActionsContainer ,
696
701
* args : Any ,
697
- nargs : Union [ int , str , tuple [int ], tuple [int , int ], tuple [int , float ], None ] = None ,
698
- choices_provider : Optional [ ChoicesProviderFunc ] = None ,
699
- completer : Optional [ CompleterFunc ] = None ,
702
+ nargs : int | str | tuple [int ] | tuple [int , int ] | tuple [int , float ] | None = None ,
703
+ choices_provider : ChoicesProviderFunc | None = None ,
704
+ completer : CompleterFunc | None = None ,
700
705
suppress_tab_hint : bool = False ,
701
- descriptive_header : Optional [ str ] = None ,
706
+ descriptive_header : str | None = None ,
702
707
** kwargs : Any ,
703
708
) -> argparse .Action :
704
709
"""Wrap ActionsContainer.add_argument() which supports more settings used by cmd2.
@@ -744,7 +749,7 @@ def _add_argument_wrapper(
744
749
nargs_range = None
745
750
746
751
if nargs is not None :
747
- nargs_adjusted : Union [ int , str , tuple [int ], tuple [int , int ], tuple [int , float ], None ]
752
+ nargs_adjusted : int | str | tuple [int ] | tuple [int , int ] | tuple [int , float ] | None
748
753
# Check if nargs was given as a range
749
754
if isinstance (nargs , tuple ):
750
755
# Handle 1-item tuple by setting max to INFINITY
@@ -885,7 +890,7 @@ def _match_argument_wrapper(self: argparse.ArgumentParser, action: argparse.Acti
885
890
ATTR_AP_COMPLETER_TYPE = 'ap_completer_type'
886
891
887
892
888
- def _ArgumentParser_get_ap_completer_type (self : argparse .ArgumentParser ) -> Optional [ type [' ArgparseCompleter' ]] : # noqa: N802
893
+ def _ArgumentParser_get_ap_completer_type (self : argparse .ArgumentParser ) -> type [ArgparseCompleter ] | None : # noqa: N802
889
894
"""Get the ap_completer_type attribute of an argparse ArgumentParser.
890
895
891
896
This function is added by cmd2 as a method called ``get_ap_completer_type()`` to ``argparse.ArgumentParser`` class.
@@ -901,7 +906,7 @@ def _ArgumentParser_get_ap_completer_type(self: argparse.ArgumentParser) -> Opti
901
906
setattr (argparse .ArgumentParser , 'get_ap_completer_type' , _ArgumentParser_get_ap_completer_type )
902
907
903
908
904
- def _ArgumentParser_set_ap_completer_type (self : argparse .ArgumentParser , ap_completer_type : type [' ArgparseCompleter' ]) -> None : # noqa: N802
909
+ def _ArgumentParser_set_ap_completer_type (self : argparse .ArgumentParser , ap_completer_type : type [ArgparseCompleter ]) -> None : # noqa: N802
905
910
"""Set the ap_completer_type attribute of an argparse ArgumentParser.
906
911
907
912
This function is added by cmd2 as a method called ``set_ap_completer_type()`` to ``argparse.ArgumentParser`` class.
@@ -996,10 +1001,10 @@ class Cmd2HelpFormatter(argparse.RawTextHelpFormatter):
996
1001
997
1002
def _format_usage (
998
1003
self ,
999
- usage : Optional [ str ] ,
1004
+ usage : str | None ,
1000
1005
actions : Iterable [argparse .Action ],
1001
1006
groups : Iterable [argparse ._ArgumentGroup ],
1002
- prefix : Optional [ str ] = None ,
1007
+ prefix : str | None = None ,
1003
1008
) -> str :
1004
1009
if prefix is None :
1005
1010
prefix = gettext ('Usage: ' )
@@ -1053,7 +1058,7 @@ def _format_usage(
1053
1058
# End cmd2 customization
1054
1059
1055
1060
# helper for wrapping lines
1056
- def get_lines (parts : list [str ], indent : str , prefix : Optional [ str ] = None ) -> list [str ]:
1061
+ def get_lines (parts : list [str ], indent : str , prefix : str | None = None ) -> list [str ]:
1057
1062
lines : list [str ] = []
1058
1063
line : list [str ] = []
1059
1064
line_len = len (prefix ) - 1 if prefix is not None else len (indent ) - 1
@@ -1133,8 +1138,8 @@ def _format_action_invocation(self, action: argparse.Action) -> str:
1133
1138
def _determine_metavar (
1134
1139
self ,
1135
1140
action : argparse .Action ,
1136
- default_metavar : Union [ str , tuple [str , ...] ],
1137
- ) -> Union [ str , tuple [str , ...] ]:
1141
+ default_metavar : str | tuple [str , ...],
1142
+ ) -> str | tuple [str , ...]:
1138
1143
"""Determine what to use as the metavar value of an action."""
1139
1144
if action .metavar is not None :
1140
1145
result = action .metavar
@@ -1150,7 +1155,7 @@ def _determine_metavar(
1150
1155
def _metavar_formatter (
1151
1156
self ,
1152
1157
action : argparse .Action ,
1153
- default_metavar : Union [ str , tuple [str , ...] ],
1158
+ default_metavar : str | tuple [str , ...],
1154
1159
) -> Callable [[int ], tuple [str , ...]]:
1155
1160
metavar = self ._determine_metavar (action , default_metavar )
1156
1161
@@ -1161,7 +1166,7 @@ def format_tuple(tuple_size: int) -> tuple[str, ...]:
1161
1166
1162
1167
return format_tuple
1163
1168
1164
- def _format_args (self , action : argparse .Action , default_metavar : Union [ str , tuple [str , ...] ]) -> str :
1169
+ def _format_args (self , action : argparse .Action , default_metavar : str | tuple [str , ...]) -> str :
1165
1170
"""Handle ranged nargs and make other output less verbose."""
1166
1171
metavar = self ._determine_metavar (action , default_metavar )
1167
1172
metavar_formatter = self ._metavar_formatter (action , default_metavar )
@@ -1191,23 +1196,23 @@ class Cmd2ArgumentParser(argparse.ArgumentParser):
1191
1196
1192
1197
def __init__ (
1193
1198
self ,
1194
- prog : Optional [ str ] = None ,
1195
- usage : Optional [ str ] = None ,
1196
- description : Optional [ str ] = None ,
1197
- epilog : Optional [ str ] = None ,
1199
+ prog : str | None = None ,
1200
+ usage : str | None = None ,
1201
+ description : str | None = None ,
1202
+ epilog : str | None = None ,
1198
1203
parents : Sequence [argparse .ArgumentParser ] = (),
1199
1204
formatter_class : type [argparse .HelpFormatter ] = Cmd2HelpFormatter ,
1200
1205
prefix_chars : str = '-' ,
1201
- fromfile_prefix_chars : Optional [ str ] = None ,
1202
- argument_default : Optional [ str ] = None ,
1206
+ fromfile_prefix_chars : str | None = None ,
1207
+ argument_default : str | None = None ,
1203
1208
conflict_handler : str = 'error' ,
1204
1209
add_help : bool = True ,
1205
1210
allow_abbrev : bool = True ,
1206
1211
exit_on_error : bool = True ,
1207
1212
suggest_on_error : bool = False ,
1208
1213
color : bool = False ,
1209
1214
* ,
1210
- ap_completer_type : Optional [ type [' ArgparseCompleter' ]] = None ,
1215
+ ap_completer_type : type [ArgparseCompleter ] | None = None ,
1211
1216
) -> None :
1212
1217
"""Initialize the Cmd2ArgumentParser instance, a custom ArgumentParser added by cmd2.
1213
1218
@@ -1341,7 +1346,7 @@ def format_help(self) -> str:
1341
1346
# determine help from format above
1342
1347
return formatter .format_help () + '\n '
1343
1348
1344
- def _print_message (self , message : str , file : Optional [ IO [str ]] = None ) -> None : # type: ignore[override]
1349
+ def _print_message (self , message : str , file : IO [str ] | None = None ) -> None : # type: ignore[override]
1345
1350
# Override _print_message to use style_aware_write() since we use ANSI escape characters to support color
1346
1351
if message :
1347
1352
if file is None :
0 commit comments