12
12
from typing import (
13
13
IO ,
14
14
TYPE_CHECKING ,
15
- Optional ,
16
- Union ,
17
15
cast ,
18
16
)
19
17
@@ -105,8 +103,8 @@ class _ArgumentState:
105
103
106
104
def __init__ (self , arg_action : argparse .Action ) -> None :
107
105
self .action = arg_action
108
- self .min : Union [ int , str ]
109
- self .max : Union [ float , int , str ]
106
+ self .min : int | str
107
+ self .max : float | int | str
110
108
self .count = 0
111
109
self .is_remainder = self .action .nargs == argparse .REMAINDER
112
110
@@ -141,7 +139,7 @@ def __init__(self, flag_arg_state: _ArgumentState) -> None:
141
139
:param flag_arg_state: information about the unfinished flag action.
142
140
"""
143
141
arg = f'{ argparse ._get_action_name (flag_arg_state .action )} '
144
- err = f'{ generate_range_error (cast (int , flag_arg_state .min ), cast (Union [ int , float ] , flag_arg_state .max ))} '
142
+ err = f'{ generate_range_error (cast (int , flag_arg_state .min ), cast (int | float , flag_arg_state .max ))} '
145
143
error = f"Error: argument { arg } : { err } ({ flag_arg_state .count } entered)"
146
144
super ().__init__ (error )
147
145
@@ -163,7 +161,7 @@ class ArgparseCompleter:
163
161
"""Automatic command line tab completion based on argparse parameters."""
164
162
165
163
def __init__ (
166
- self , parser : argparse .ArgumentParser , cmd2_app : 'Cmd' , * , parent_tokens : Optional [ dict [str , list [str ]]] = None
164
+ self , parser : argparse .ArgumentParser , cmd2_app : 'Cmd' , * , parent_tokens : dict [str , list [str ]] | None = None
167
165
) -> None :
168
166
"""Create an ArgparseCompleter.
169
167
@@ -203,7 +201,7 @@ def __init__(
203
201
self ._subcommand_action = action
204
202
205
203
def complete (
206
- self , text : str , line : str , begidx : int , endidx : int , tokens : list [str ], * , cmd_set : Optional [ CommandSet ] = None
204
+ self , text : str , line : str , begidx : int , endidx : int , tokens : list [str ], * , cmd_set : CommandSet | None = None
207
205
) -> list [str ]:
208
206
"""Complete text using argparse metadata.
209
207
@@ -228,10 +226,10 @@ def complete(
228
226
skip_remaining_flags = False
229
227
230
228
# _ArgumentState of the current positional
231
- pos_arg_state : Optional [ _ArgumentState ] = None
229
+ pos_arg_state : _ArgumentState | None = None
232
230
233
231
# _ArgumentState of the current flag
234
- flag_arg_state : Optional [ _ArgumentState ] = None
232
+ flag_arg_state : _ArgumentState | None = None
235
233
236
234
# Non-reusable flags that we've parsed
237
235
matched_flags : list [str ] = []
@@ -523,7 +521,7 @@ def _complete_flags(self, text: str, line: str, begidx: int, endidx: int, matche
523
521
524
522
return matches
525
523
526
- def _format_completions (self , arg_state : _ArgumentState , completions : Union [ list [str ], list [CompletionItem ] ]) -> list [str ]:
524
+ def _format_completions (self , arg_state : _ArgumentState , completions : list [str ] | list [CompletionItem ]) -> list [str ]:
527
525
"""Format CompletionItems into hint table."""
528
526
# Nothing to do if we don't have at least 2 completions which are all CompletionItems
529
527
if len (completions ) < 2 or not all (isinstance (c , CompletionItem ) for c in completions ):
@@ -625,7 +623,7 @@ def complete_subcommand_help(self, text: str, line: str, begidx: int, endidx: in
625
623
break
626
624
return []
627
625
628
- def print_help (self , tokens : list [str ], file : Optional [ IO [str ]] = None ) -> None :
626
+ def print_help (self , tokens : list [str ], file : IO [str ] | None = None ) -> None :
629
627
"""Supports cmd2's help command in the printing of help text.
630
628
631
629
:param tokens: arguments passed to help command
@@ -636,7 +634,7 @@ def print_help(self, tokens: list[str], file: Optional[IO[str]] = None) -> None:
636
634
# If so, we will let the subcommand's parser handle the rest of the tokens via another ArgparseCompleter.
637
635
if tokens and self ._subcommand_action is not None :
638
636
parser = cast (
639
- Optional [ argparse .ArgumentParser ] ,
637
+ argparse .ArgumentParser | None ,
640
638
self ._subcommand_action .choices .get (tokens [0 ]),
641
639
)
642
640
@@ -657,15 +655,15 @@ def _complete_arg(
657
655
arg_state : _ArgumentState ,
658
656
consumed_arg_values : dict [str , list [str ]],
659
657
* ,
660
- cmd_set : Optional [ CommandSet ] = None ,
658
+ cmd_set : CommandSet | None = None ,
661
659
) -> list [str ]:
662
660
"""Tab completion routine for an argparse argument.
663
661
664
662
:return: list of completions
665
663
:raises CompletionError: if the completer or choices function this calls raises one.
666
664
"""
667
665
# Check if the arg provides choices to the user
668
- arg_choices : Union [ list [str ], ChoicesCallable ]
666
+ arg_choices : list [str ] | ChoicesCallable
669
667
if arg_state .action .choices is not None :
670
668
arg_choices = list (arg_state .action .choices )
671
669
if not arg_choices :
0 commit comments