2222Documentation: https://cmd2.readthedocs.io/
2323"""
2424
25- from __future__ import annotations
26-
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.
2729import argparse
2830import contextlib
2931import copy
5456 dataclass ,
5557 field ,
5658)
59+ from types import FrameType
5760from typing import (
5861 IO ,
5962 TYPE_CHECKING ,
6063 Any ,
6164 TextIO ,
62- TypeAlias ,
6365 TypeVar ,
66+ Union ,
6467 cast ,
6568)
6669
9396)
9497from . import rich_utils as ru
9598from . import string_utils as su
99+ from .argparse_custom import Cmd2ArgumentParser
96100from .clipboard import (
97101 get_paste_buffer ,
98102 write_to_paste_buffer ,
143147 RichPrintKwargs ,
144148)
145149from .styles import Cmd2Style
150+ from .types import (
151+ ChoicesProviderUnbound ,
152+ CmdOrSet ,
153+ CompleterBound ,
154+ CompleterUnbound ,
155+ Matchable ,
156+ )
146157
147158with contextlib .suppress (ImportError ):
148159 from IPython import start_ipython
@@ -187,24 +198,6 @@ def __init__(self, msg: str = '') -> None:
187198 suggest_similar ,
188199)
189200
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-
208201
209202class _SavedCmd2Env :
210203 """cmd2 environment settings that are backed up when entering an interactive Python shell."""
@@ -218,13 +211,21 @@ def __init__(self) -> None:
218211DisabledCommand = namedtuple ('DisabledCommand' , ['command_function' , 'help_function' , 'completer_function' ]) # noqa: PYI024
219212
220213
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+
221222class _CommandParsers :
222223 """Create and store all command method argument parsers for a given Cmd instance.
223224
224225 Parser creation and retrieval are accomplished through the get() method.
225226 """
226227
227- def __init__ (self , cmd : Cmd ) -> None :
228+ def __init__ (self , cmd : ' Cmd' ) -> None :
228229 self ._cmd = cmd
229230
230231 # Keyed by the fully qualified method names. This is more reliable than
@@ -1004,7 +1005,7 @@ def check_parser_uninstallable(parser: argparse.ArgumentParser) -> None:
10041005 if command_parser is not None :
10051006 check_parser_uninstallable (command_parser )
10061007
1007- def _register_subcommands (self , cmdset : CommandSet | Cmd ) -> None :
1008+ def _register_subcommands (self , cmdset : Union [ CommandSet , ' Cmd' ] ) -> None :
10081009 """Register subcommands with their base command.
10091010
10101011 :param cmdset: CommandSet or cmd2.Cmd subclass containing subcommands
@@ -1097,7 +1098,7 @@ def find_subcommand(
10971098
10981099 break
10991100
1100- def _unregister_subcommands (self , cmdset : CommandSet | Cmd ) -> None :
1101+ def _unregister_subcommands (self , cmdset : Union [ CommandSet , ' Cmd' ] ) -> None :
11011102 """Unregister subcommands from their base command.
11021103
11031104 :param cmdset: CommandSet containing subcommands
@@ -2194,8 +2195,8 @@ def _determine_ap_completer_type(parser: argparse.ArgumentParser) -> type[argpar
21942195 :param parser: the parser to examine
21952196 :return: type of ArgparseCompleter
21962197 """
2197- CompleterType : TypeAlias = type [argparse_completer .ArgparseCompleter ] | None
2198- completer_type : CompleterType = parser .get_ap_completer_type () # type: ignore[attr-defined]
2198+ Completer = type [argparse_completer .ArgparseCompleter ] | None # noqa: N806
2199+ completer_type : Completer = parser .get_ap_completer_type () # type: ignore[attr-defined]
21992200
22002201 if completer_type is None :
22012202 completer_type = argparse_completer .DEFAULT_AP_COMPLETER
@@ -5660,7 +5661,7 @@ def register_cmdfinalization_hook(
56605661 def _resolve_func_self (
56615662 self ,
56625663 cmd_support_func : Callable [..., Any ],
5663- cmd_self : CommandSet | Cmd | None ,
5664+ cmd_self : Union [ CommandSet , ' Cmd' , None ] ,
56645665 ) -> object | None :
56655666 """Attempt to resolve a candidate instance to pass as 'self'.
56665667
0 commit comments