33import re
44import sys
55import warnings
6- from argparse import SUPPRESS , _HelpAction , _SubParsersAction
6+ from argparse import SUPPRESS , _HelpAction , _SubParsersAction , _VersionAction
77from argparse import Action as ArgparseAction
88from contextlib import contextmanager
99from contextvars import ContextVar
1010from typing import Any , Dict , List , Optional , Tuple , Type , Union
1111
12- from ._common import Action , is_not_subclass_type , is_subclass , parser_context
12+ from ._common import Action , NonParsingAction , is_not_subclass_type , is_subclass , parser_context
1313from ._loaders_dumpers import get_loader_exceptions , load_value
1414from ._namespace import Namespace , NSKeyError , split_key , split_key_root
1515from ._optionals import _get_config_read_mode , ruamel_support
3636
3737def _is_branch_key (parser , key : str ) -> bool :
3838 root_key = split_key_root (key )[0 ]
39- for action in filter_default_actions (parser ._actions ):
39+ for action in filter_non_parsing_actions (parser ._actions ):
4040 if isinstance (action , _ActionSubCommands ) and root_key in action ._name_parser_map :
4141 subparser = action ._name_parser_map [root_key ]
4242 return _is_branch_key (subparser , split_key_root (key )[1 ])
@@ -59,7 +59,7 @@ def _find_action_and_subcommand(
5959 Returns:
6060 The action if found, otherwise None.
6161 """
62- actions = filter_default_actions (parser ._actions )
62+ actions = filter_non_parsing_actions (parser ._actions )
6363 if exclude is not None :
6464 actions = [a for a in actions if not isinstance (a , exclude )]
6565 fallback_action = None
@@ -138,13 +138,13 @@ def remove(actions):
138138 remove (action_group ._group_actions )
139139
140140
141- def filter_default_actions (actions ):
142- from ._completions import ShtabAction
141+ non_parsing_actions = (_HelpAction , _VersionAction , NonParsingAction )
143142
144- default = (_HelpAction , _ActionHelpClassPath , _ActionPrintConfig , ShtabAction )
143+
144+ def filter_non_parsing_actions (actions ):
145145 if isinstance (actions , list ):
146- return [a for a in actions if not isinstance (a , default )]
147- return {k : a for k , a in actions .items () if not isinstance (a , default )}
146+ return [a for a in actions if not isinstance (a , non_parsing_actions )]
147+ return {k : a for k , a in actions .items () if not isinstance (a , non_parsing_actions )}
148148
149149
150150class ActionConfigFile (Action ):
@@ -235,7 +235,7 @@ def previous_config_context(cfg):
235235print_config_skip : ContextVar = ContextVar ("print_config_skip" , default = False )
236236
237237
238- class _ActionPrintConfig (Action ):
238+ class _ActionPrintConfig (NonParsingAction ):
239239 def __init__ (
240240 self ,
241241 option_strings ,
@@ -343,7 +343,7 @@ def check_type(self, value, parser):
343343 return self ._load_config (value , parser )
344344
345345
346- class _ActionHelpClassPath (Action ):
346+ class _ActionHelpClassPath (NonParsingAction ):
347347 sub_add_kwargs : Dict [str , Any ] = {}
348348
349349 @classmethod
@@ -586,7 +586,7 @@ def add_prefix(key):
586586 required_args = {prefix + "." + x for x in subparser .required_args }
587587
588588 option_string_actions = {}
589- for key , action in filter_default_actions (subparser ._option_string_actions ).items ():
589+ for key , action in filter_non_parsing_actions (subparser ._option_string_actions ).items ():
590590 option_string_actions [add_prefix (key )] = action
591591
592592 isect = set (option_string_actions ).intersection (set (parser ._option_string_actions ))
@@ -595,7 +595,7 @@ def add_prefix(key):
595595
596596 actions = []
597597 dest = prefix .replace ("-" , "_" )
598- for action in filter_default_actions (subparser ._actions ):
598+ for action in filter_non_parsing_actions (subparser ._actions ):
599599 if isinstance (action , ActionYesNo ):
600600 action ._add_dest_prefix (prefix )
601601 else :
@@ -608,8 +608,8 @@ def add_prefix(key):
608608 if description is not None :
609609 base_action_group .description = description
610610 base_action_group .parser = parser
611- base_action_group ._actions = filter_default_actions (base_action_group ._actions )
612- base_action_group ._group_actions = filter_default_actions (base_action_group ._group_actions )
611+ base_action_group ._actions = filter_non_parsing_actions (base_action_group ._actions )
612+ base_action_group ._group_actions = filter_non_parsing_actions (base_action_group ._group_actions )
613613 extra_action_groups = subparser ._action_groups [2 :]
614614 for group in extra_action_groups :
615615 if group .dest is not None :
0 commit comments