2222Documentation: https://cmd2.readthedocs.io/
2323"""
2424
25- # This module has many imports, quite a few of which are only
26- # infrequently utilized. To reduce the initial overhead of
27- # import this module, many of these imports are lazy-loaded
28- # i.e. we only import the module when we use it.
25+ from __future__ import annotations
26+
2927import argparse
3028import contextlib
3129import copy
5654 dataclass ,
5755 field ,
5856)
59- from types import FrameType
6057from typing import (
6158 IO ,
6259 TYPE_CHECKING ,
6360 Any ,
6461 TextIO ,
62+ TypeAlias ,
6563 TypeVar ,
66- Union ,
6764 cast ,
6865)
6966
9693)
9794from . import rich_utils as ru
9895from . import string_utils as su
99- from .argparse_custom import Cmd2ArgumentParser
10096from .clipboard import (
10197 get_paste_buffer ,
10298 write_to_paste_buffer ,
147143 RichPrintKwargs ,
148144)
149145from .styles import Cmd2Style
150- from .types import (
151- ChoicesProviderUnbound ,
152- CmdOrSet ,
153- CompleterBound ,
154- CompleterUnbound ,
155- Matchable ,
156- )
157146
158147with contextlib .suppress (ImportError ):
159148 from IPython import start_ipython
@@ -198,6 +187,24 @@ def __init__(self, msg: str = '') -> None:
198187 suggest_similar ,
199188)
200189
190+ if TYPE_CHECKING : # pragma: no cover
191+ StaticArgParseBuilder = staticmethod [[], argparse .ArgumentParser ]
192+ ClassArgParseBuilder = classmethod ['Cmd' | CommandSet , [], argparse .ArgumentParser ]
193+ from types import FrameType
194+
195+ from .argparse_custom import Cmd2ArgumentParser
196+ from .types import (
197+ ChoicesProviderUnbound ,
198+ CmdOrSet ,
199+ CompleterBound ,
200+ CompleterUnbound ,
201+ Matchable ,
202+ )
203+
204+ else :
205+ StaticArgParseBuilder = staticmethod
206+ ClassArgParseBuilder = classmethod
207+
201208
202209class _SavedCmd2Env :
203210 """cmd2 environment settings that are backed up when entering an interactive Python shell."""
@@ -211,21 +218,13 @@ def __init__(self) -> None:
211218DisabledCommand = namedtuple ('DisabledCommand' , ['command_function' , 'help_function' , 'completer_function' ]) # noqa: PYI024
212219
213220
214- if TYPE_CHECKING : # pragma: no cover
215- StaticArgParseBuilder = staticmethod [[], argparse .ArgumentParser ]
216- ClassArgParseBuilder = classmethod ['Cmd' | CommandSet , [], argparse .ArgumentParser ]
217- else :
218- StaticArgParseBuilder = staticmethod
219- ClassArgParseBuilder = classmethod
220-
221-
222221class _CommandParsers :
223222 """Create and store all command method argument parsers for a given Cmd instance.
224223
225224 Parser creation and retrieval are accomplished through the get() method.
226225 """
227226
228- def __init__ (self , cmd : ' Cmd' ) -> None :
227+ def __init__ (self , cmd : Cmd ) -> None :
229228 self ._cmd = cmd
230229
231230 # Keyed by the fully qualified method names. This is more reliable than
@@ -1005,7 +1004,7 @@ def check_parser_uninstallable(parser: argparse.ArgumentParser) -> None:
10051004 if command_parser is not None :
10061005 check_parser_uninstallable (command_parser )
10071006
1008- def _register_subcommands (self , cmdset : Union [ CommandSet , ' Cmd' ] ) -> None :
1007+ def _register_subcommands (self , cmdset : CommandSet | Cmd ) -> None :
10091008 """Register subcommands with their base command.
10101009
10111010 :param cmdset: CommandSet or cmd2.Cmd subclass containing subcommands
@@ -1098,7 +1097,7 @@ def find_subcommand(
10981097
10991098 break
11001099
1101- def _unregister_subcommands (self , cmdset : Union [ CommandSet , ' Cmd' ] ) -> None :
1100+ def _unregister_subcommands (self , cmdset : CommandSet | Cmd ) -> None :
11021101 """Unregister subcommands from their base command.
11031102
11041103 :param cmdset: CommandSet containing subcommands
@@ -2195,8 +2194,8 @@ def _determine_ap_completer_type(parser: argparse.ArgumentParser) -> type[argpar
21952194 :param parser: the parser to examine
21962195 :return: type of ArgparseCompleter
21972196 """
2198- Completer = type [argparse_completer .ArgparseCompleter ] | None # noqa: N806
2199- completer_type : Completer = parser .get_ap_completer_type () # type: ignore[attr-defined]
2197+ CompleterType : TypeAlias = type [argparse_completer .ArgparseCompleter ] | None
2198+ completer_type : CompleterType = parser .get_ap_completer_type () # type: ignore[attr-defined]
22002199
22012200 if completer_type is None :
22022201 completer_type = argparse_completer .DEFAULT_AP_COMPLETER
@@ -5661,7 +5660,7 @@ def register_cmdfinalization_hook(
56615660 def _resolve_func_self (
56625661 self ,
56635662 cmd_support_func : Callable [..., Any ],
5664- cmd_self : Union [ CommandSet , ' Cmd' , None ] ,
5663+ cmd_self : CommandSet | Cmd | None ,
56655664 ) -> object | None :
56665665 """Attempt to resolve a candidate instance to pass as 'self'.
56675666
0 commit comments